initial import
[vuplus_webkit] / Source / WebCore / html / HTMLOptionElement.cpp
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4  *           (C) 2001 Dirk Mueller (mueller@kde.org)
5  *           (C) 2006 Alexey Proskuryakov (ap@nypop.com)
6  * Copyright (C) 2004, 2005, 2006, 2010 Apple Inc. All rights reserved.
7  * Copyright (C) 2010 Google Inc. All rights reserved.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public License
20  * along with this library; see the file COPYING.LIB.  If not, write to
21  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22  * Boston, MA 02110-1301, USA.
23  *
24  */
25
26 #include "config.h"
27 #include "HTMLOptionElement.h"
28
29 #include "Attribute.h"
30 #include "CSSStyleSelector.h"
31 #include "Document.h"
32 #include "ExceptionCode.h"
33 #include "HTMLNames.h"
34 #include "HTMLSelectElement.h"
35 #include "NodeRenderStyle.h"
36 #include "NodeRenderingContext.h"
37 #include "RenderMenuList.h"
38 #include "Text.h"
39 #include <wtf/StdLibExtras.h>
40 #include <wtf/Vector.h>
41
42 namespace WebCore {
43
44 using namespace HTMLNames;
45
46 HTMLOptionElement::HTMLOptionElement(const QualifiedName& tagName, Document* document, HTMLFormElement* form)
47     : HTMLFormControlElement(tagName, document, form)
48 {
49     ASSERT(hasTagName(optionTag));
50 }
51
52 PassRefPtr<HTMLOptionElement> HTMLOptionElement::create(Document* document, HTMLFormElement* form)
53 {
54     return adoptRef(new HTMLOptionElement(optionTag, document, form));
55 }
56
57 PassRefPtr<HTMLOptionElement> HTMLOptionElement::create(const QualifiedName& tagName, Document* document, HTMLFormElement* form)
58 {
59     return adoptRef(new HTMLOptionElement(tagName, document, form));
60 }
61
62 PassRefPtr<HTMLOptionElement> HTMLOptionElement::createForJSConstructor(Document* document, const String& data, const String& value,
63         bool defaultSelected, bool selected, ExceptionCode& ec)
64 {
65     RefPtr<HTMLOptionElement> element = adoptRef(new HTMLOptionElement(optionTag, document));
66
67     RefPtr<Text> text = Text::create(document, data.isNull() ? "" : data);
68
69     ec = 0;
70     element->appendChild(text.release(), ec);
71     if (ec)
72         return 0;
73
74     if (!value.isNull())
75         element->setValue(value);
76     element->setDefaultSelected(defaultSelected);
77     element->setSelected(selected);
78
79     return element.release();
80 }
81
82 void HTMLOptionElement::attach()
83 {
84     if (parentNode()->renderStyle())
85         setRenderStyle(styleForRenderer());
86     HTMLFormControlElement::attach();
87 }
88
89 void HTMLOptionElement::detach()
90 {
91     m_style.clear();
92     HTMLFormControlElement::detach();
93 }
94
95 bool HTMLOptionElement::supportsFocus() const
96 {
97     return HTMLElement::supportsFocus();
98 }
99
100 bool HTMLOptionElement::isFocusable() const
101 {
102     // Option elements do not have a renderer so we check the renderStyle instead.
103     return supportsFocus() && renderStyle() && renderStyle()->display() != NONE;
104 }
105
106 const AtomicString& HTMLOptionElement::formControlType() const
107 {
108     DEFINE_STATIC_LOCAL(const AtomicString, option, ("option"));
109     return option;
110 }
111
112 String HTMLOptionElement::text() const
113 {
114     return OptionElement::collectOptionLabelOrText(m_data, this);
115 }
116
117 void HTMLOptionElement::setText(const String &text, ExceptionCode& ec)
118 {
119     // Handle the common special case where there's exactly 1 child node, and it's a text node.
120     Node* child = firstChild();
121     if (child && child->isTextNode() && !child->nextSibling()) {
122         static_cast<Text *>(child)->setData(text, ec);
123         return;
124     }
125
126     removeChildren();
127     appendChild(Text::create(document(), text), ec);
128 }
129
130 void HTMLOptionElement::accessKeyAction(bool)
131 {
132     HTMLSelectElement* select = ownerSelectElement();
133     if (select)
134         select->accessKeySetSelectedIndex(index());
135 }
136
137 int HTMLOptionElement::index() const
138 {
139     return OptionElement::optionIndex(ownerSelectElement(), this);
140 }
141
142 void HTMLOptionElement::parseMappedAttribute(Attribute* attr)
143 {
144     if (attr->name() == selectedAttr)
145         m_data.setSelected(!attr->isNull());
146     else if (attr->name() == valueAttr)
147         m_data.setValue(attr->value());
148     else if (attr->name() == labelAttr)
149         m_data.setLabel(attr->value());
150     else
151         HTMLFormControlElement::parseMappedAttribute(attr);
152 }
153
154 String HTMLOptionElement::value() const
155 {
156     return OptionElement::collectOptionValue(m_data, this);
157 }
158
159 void HTMLOptionElement::setValue(const String& value)
160 {
161     setAttribute(valueAttr, value);
162 }
163
164 bool HTMLOptionElement::selected()
165 {
166     if (HTMLSelectElement* select = ownerSelectElement())
167         select->recalcListItemsIfNeeded();
168     return m_data.selected();
169 }
170
171 void HTMLOptionElement::setSelected(bool selected)
172 {
173     if (m_data.selected() == selected)
174         return;
175
176     OptionElement::setSelectedState(m_data, this, selected);
177
178     if (HTMLSelectElement* select = ownerSelectElement())
179         select->setSelectedIndex(selected ? index() : -1, false);
180 }
181
182 void HTMLOptionElement::setSelectedState(bool selected)
183 {
184     OptionElement::setSelectedState(m_data, this, selected);
185 }
186
187 void HTMLOptionElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
188 {
189     HTMLSelectElement* select = ownerSelectElement();
190     if (select)
191         select->childrenChanged(changedByParser);
192     HTMLFormControlElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
193 }
194
195 HTMLSelectElement* HTMLOptionElement::ownerSelectElement() const
196 {
197     ContainerNode* select = parentNode();
198     while (select && !select->hasTagName(selectTag))
199         select = select->parentNode();
200
201     if (!select)
202         return 0;
203
204     return static_cast<HTMLSelectElement*>(select);
205 }
206
207 bool HTMLOptionElement::defaultSelected() const
208 {
209     return fastHasAttribute(selectedAttr);
210 }
211
212 void HTMLOptionElement::setDefaultSelected(bool b)
213 {
214     setAttribute(selectedAttr, b ? "" : 0);
215 }
216
217 String HTMLOptionElement::label() const
218 {
219     return m_data.label();
220 }
221
222 void HTMLOptionElement::setRenderStyle(PassRefPtr<RenderStyle> newStyle)
223 {
224     m_style = newStyle;
225     if (HTMLSelectElement* select = ownerSelectElement())
226         if (RenderObject* renderer = select->renderer())
227             renderer->repaint();
228 }
229
230 RenderStyle* HTMLOptionElement::nonRendererRenderStyle() const
231 {
232     return m_style.get();
233 }
234
235 String HTMLOptionElement::textIndentedToRespectGroupLabel() const
236 {
237     return OptionElement::collectOptionTextRespectingGroupLabel(m_data, this);
238 }
239
240 bool HTMLOptionElement::disabled() const
241 {
242     return ownElementDisabled() || (parentNode() && static_cast<HTMLFormControlElement*>(parentNode())->disabled());
243 }
244
245 void HTMLOptionElement::insertedIntoTree(bool deep)
246 {
247     if (HTMLSelectElement* select = ownerSelectElement()) {
248         select->setRecalcListItems();
249         // Avoid our selected() getter since it will recalculate list items incorrectly for us.
250         if (m_data.selected())
251             select->setSelectedIndex(index(), false);
252         select->scrollToSelection();
253     }
254
255     HTMLFormControlElement::insertedIntoTree(deep);
256 }
257
258 } // namespace