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.
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.
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.
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.
27 #include "HTMLOptionElement.h"
29 #include "Attribute.h"
30 #include "CSSStyleSelector.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"
39 #include <wtf/StdLibExtras.h>
40 #include <wtf/Vector.h>
44 using namespace HTMLNames;
46 HTMLOptionElement::HTMLOptionElement(const QualifiedName& tagName, Document* document, HTMLFormElement* form)
47 : HTMLFormControlElement(tagName, document, form)
49 ASSERT(hasTagName(optionTag));
52 PassRefPtr<HTMLOptionElement> HTMLOptionElement::create(Document* document, HTMLFormElement* form)
54 return adoptRef(new HTMLOptionElement(optionTag, document, form));
57 PassRefPtr<HTMLOptionElement> HTMLOptionElement::create(const QualifiedName& tagName, Document* document, HTMLFormElement* form)
59 return adoptRef(new HTMLOptionElement(tagName, document, form));
62 PassRefPtr<HTMLOptionElement> HTMLOptionElement::createForJSConstructor(Document* document, const String& data, const String& value,
63 bool defaultSelected, bool selected, ExceptionCode& ec)
65 RefPtr<HTMLOptionElement> element = adoptRef(new HTMLOptionElement(optionTag, document));
67 RefPtr<Text> text = Text::create(document, data.isNull() ? "" : data);
70 element->appendChild(text.release(), ec);
75 element->setValue(value);
76 element->setDefaultSelected(defaultSelected);
77 element->setSelected(selected);
79 return element.release();
82 void HTMLOptionElement::attach()
84 if (parentNode()->renderStyle())
85 setRenderStyle(styleForRenderer());
86 HTMLFormControlElement::attach();
89 void HTMLOptionElement::detach()
92 HTMLFormControlElement::detach();
95 bool HTMLOptionElement::supportsFocus() const
97 return HTMLElement::supportsFocus();
100 bool HTMLOptionElement::isFocusable() const
102 // Option elements do not have a renderer so we check the renderStyle instead.
103 return supportsFocus() && renderStyle() && renderStyle()->display() != NONE;
106 const AtomicString& HTMLOptionElement::formControlType() const
108 DEFINE_STATIC_LOCAL(const AtomicString, option, ("option"));
112 String HTMLOptionElement::text() const
114 return OptionElement::collectOptionLabelOrText(m_data, this);
117 void HTMLOptionElement::setText(const String &text, ExceptionCode& ec)
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);
127 appendChild(Text::create(document(), text), ec);
130 void HTMLOptionElement::accessKeyAction(bool)
132 HTMLSelectElement* select = ownerSelectElement();
134 select->accessKeySetSelectedIndex(index());
137 int HTMLOptionElement::index() const
139 return OptionElement::optionIndex(ownerSelectElement(), this);
142 void HTMLOptionElement::parseMappedAttribute(Attribute* attr)
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());
151 HTMLFormControlElement::parseMappedAttribute(attr);
154 String HTMLOptionElement::value() const
156 return OptionElement::collectOptionValue(m_data, this);
159 void HTMLOptionElement::setValue(const String& value)
161 setAttribute(valueAttr, value);
164 bool HTMLOptionElement::selected()
166 if (HTMLSelectElement* select = ownerSelectElement())
167 select->recalcListItemsIfNeeded();
168 return m_data.selected();
171 void HTMLOptionElement::setSelected(bool selected)
173 if (m_data.selected() == selected)
176 OptionElement::setSelectedState(m_data, this, selected);
178 if (HTMLSelectElement* select = ownerSelectElement())
179 select->setSelectedIndex(selected ? index() : -1, false);
182 void HTMLOptionElement::setSelectedState(bool selected)
184 OptionElement::setSelectedState(m_data, this, selected);
187 void HTMLOptionElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
189 HTMLSelectElement* select = ownerSelectElement();
191 select->childrenChanged(changedByParser);
192 HTMLFormControlElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
195 HTMLSelectElement* HTMLOptionElement::ownerSelectElement() const
197 ContainerNode* select = parentNode();
198 while (select && !select->hasTagName(selectTag))
199 select = select->parentNode();
204 return static_cast<HTMLSelectElement*>(select);
207 bool HTMLOptionElement::defaultSelected() const
209 return fastHasAttribute(selectedAttr);
212 void HTMLOptionElement::setDefaultSelected(bool b)
214 setAttribute(selectedAttr, b ? "" : 0);
217 String HTMLOptionElement::label() const
219 return m_data.label();
222 void HTMLOptionElement::setRenderStyle(PassRefPtr<RenderStyle> newStyle)
225 if (HTMLSelectElement* select = ownerSelectElement())
226 if (RenderObject* renderer = select->renderer())
230 RenderStyle* HTMLOptionElement::nonRendererRenderStyle() const
232 return m_style.get();
235 String HTMLOptionElement::textIndentedToRespectGroupLabel() const
237 return OptionElement::collectOptionTextRespectingGroupLabel(m_data, this);
240 bool HTMLOptionElement::disabled() const
242 return ownElementDisabled() || (parentNode() && static_cast<HTMLFormControlElement*>(parentNode())->disabled());
245 void HTMLOptionElement::insertedIntoTree(bool deep)
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();
255 HTMLFormControlElement::insertedIntoTree(deep);