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 * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc.
6 * (C) 2006 Alexey Proskuryakov (ap@nypop.com)
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 *
23 */
24
25 #include "config.h"
26 #include "HTMLOptionElement.h"
27
28 #include "CSSStyleSelector.h"
29 #include "Document.h"
30 #include "ExceptionCode.h"
31 #include "HTMLNames.h"
32 #include "HTMLSelectElement.h"
33 #include "MappedAttribute.h"
34 #include "NodeRenderStyle.h"
35 #include "RenderMenuList.h"
36 #include "Text.h"
37 #include <wtf/StdLibExtras.h>
38 #include <wtf/Vector.h>
39
40 namespace WebCore {
41
42 using namespace HTMLNames;
43
HTMLOptionElement(const QualifiedName & tagName,Document * doc,HTMLFormElement * f)44 HTMLOptionElement::HTMLOptionElement(const QualifiedName& tagName, Document* doc, HTMLFormElement* f)
45 : HTMLFormControlElement(tagName, doc, f)
46 , m_style(0)
47 {
48 ASSERT(hasTagName(optionTag));
49 }
50
checkDTD(const Node * newChild)51 bool HTMLOptionElement::checkDTD(const Node* newChild)
52 {
53 return newChild->isTextNode() || newChild->hasTagName(scriptTag);
54 }
55
attach()56 void HTMLOptionElement::attach()
57 {
58 if (parentNode()->renderStyle())
59 setRenderStyle(styleForRenderer());
60 HTMLFormControlElement::attach();
61 }
62
detach()63 void HTMLOptionElement::detach()
64 {
65 m_style.clear();
66 HTMLFormControlElement::detach();
67 }
68
isFocusable() const69 bool HTMLOptionElement::isFocusable() const
70 {
71 return HTMLElement::isFocusable();
72 }
73
formControlType() const74 const AtomicString& HTMLOptionElement::formControlType() const
75 {
76 DEFINE_STATIC_LOCAL(const AtomicString, option, ("option"));
77 return option;
78 }
79
text() const80 String HTMLOptionElement::text() const
81 {
82 return OptionElement::collectOptionLabelOrText(m_data, this);
83 }
84
setText(const String & text,ExceptionCode & ec)85 void HTMLOptionElement::setText(const String &text, ExceptionCode& ec)
86 {
87 // Handle the common special case where there's exactly 1 child node, and it's a text node.
88 Node* child = firstChild();
89 if (child && child->isTextNode() && !child->nextSibling()) {
90 static_cast<Text *>(child)->setData(text, ec);
91 return;
92 }
93
94 removeChildren();
95 appendChild(new Text(document(), text), ec);
96 }
97
accessKeyAction(bool)98 void HTMLOptionElement::accessKeyAction(bool)
99 {
100 HTMLSelectElement* select = ownerSelectElement();
101 if (select)
102 select->accessKeySetSelectedIndex(index());
103 }
104
index() const105 int HTMLOptionElement::index() const
106 {
107 return OptionElement::optionIndex(ownerSelectElement(), this);
108 }
109
parseMappedAttribute(MappedAttribute * attr)110 void HTMLOptionElement::parseMappedAttribute(MappedAttribute *attr)
111 {
112 if (attr->name() == selectedAttr)
113 m_data.setSelected(!attr->isNull());
114 else if (attr->name() == valueAttr)
115 m_data.setValue(attr->value());
116 else if (attr->name() == labelAttr)
117 m_data.setLabel(attr->value());
118 else
119 HTMLFormControlElement::parseMappedAttribute(attr);
120 }
121
value() const122 String HTMLOptionElement::value() const
123 {
124 return OptionElement::collectOptionValue(m_data, this);
125 }
126
setValue(const String & value)127 void HTMLOptionElement::setValue(const String& value)
128 {
129 setAttribute(valueAttr, value);
130 }
131
selected() const132 bool HTMLOptionElement::selected() const
133 {
134 return m_data.selected();
135 }
136
setSelected(bool selected)137 void HTMLOptionElement::setSelected(bool selected)
138 {
139 if (m_data.selected() == selected)
140 return;
141
142 OptionElement::setSelectedState(m_data, this, selected);
143
144 if (HTMLSelectElement* select = ownerSelectElement())
145 select->setSelectedIndex(selected ? index() : -1, false);
146 }
147
setSelectedState(bool selected)148 void HTMLOptionElement::setSelectedState(bool selected)
149 {
150 OptionElement::setSelectedState(m_data, this, selected);
151 }
152
childrenChanged(bool changedByParser,Node * beforeChange,Node * afterChange,int childCountDelta)153 void HTMLOptionElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
154 {
155 HTMLSelectElement* select = ownerSelectElement();
156 if (select)
157 select->childrenChanged(changedByParser);
158 HTMLFormControlElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
159 }
160
ownerSelectElement() const161 HTMLSelectElement* HTMLOptionElement::ownerSelectElement() const
162 {
163 Node* select = parentNode();
164 #ifdef ANDROID_FIX
165 while (select && !(select->hasTagName(selectTag) || select->hasTagName(keygenTag)))
166 #else
167 while (select && !select->hasTagName(selectTag))
168 #endif
169 select = select->parentNode();
170
171 if (!select)
172 return 0;
173
174 return static_cast<HTMLSelectElement*>(select);
175 }
176
defaultSelected() const177 bool HTMLOptionElement::defaultSelected() const
178 {
179 return !getAttribute(selectedAttr).isNull();
180 }
181
setDefaultSelected(bool b)182 void HTMLOptionElement::setDefaultSelected(bool b)
183 {
184 setAttribute(selectedAttr, b ? "" : 0);
185 }
186
label() const187 String HTMLOptionElement::label() const
188 {
189 return m_data.label();
190 }
191
setLabel(const String & value)192 void HTMLOptionElement::setLabel(const String& value)
193 {
194 setAttribute(labelAttr, value);
195 }
196
setRenderStyle(PassRefPtr<RenderStyle> newStyle)197 void HTMLOptionElement::setRenderStyle(PassRefPtr<RenderStyle> newStyle)
198 {
199 m_style = newStyle;
200 }
201
nonRendererRenderStyle() const202 RenderStyle* HTMLOptionElement::nonRendererRenderStyle() const
203 {
204 return m_style.get();
205 }
206
textIndentedToRespectGroupLabel() const207 String HTMLOptionElement::textIndentedToRespectGroupLabel() const
208 {
209 return OptionElement::collectOptionTextRespectingGroupLabel(m_data, this);
210 }
211
disabled() const212 bool HTMLOptionElement::disabled() const
213 {
214 return HTMLFormControlElement::disabled() || (parentNode() && static_cast<HTMLFormControlElement*>(parentNode())->disabled());
215 }
216
insertedIntoTree(bool deep)217 void HTMLOptionElement::insertedIntoTree(bool deep)
218 {
219 if (HTMLSelectElement* select = ownerSelectElement()) {
220 select->setRecalcListItems();
221 if (selected())
222 select->setSelectedIndex(index(), false);
223 select->scrollToSelection();
224 }
225
226 HTMLFormControlElement::insertedIntoTree(deep);
227 }
228
229 } // namespace
230