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)
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.
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.
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.
26 #include "HTMLOptionElement.h"
28 #include "CSSStyleSelector.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"
37 #include <wtf/StdLibExtras.h>
38 #include <wtf/Vector.h>
42 using namespace HTMLNames
;
44 HTMLOptionElement::HTMLOptionElement(const QualifiedName
& tagName
, Document
* doc
, HTMLFormElement
* f
)
45 : HTMLFormControlElement(tagName
, doc
, f
)
48 ASSERT(hasTagName(optionTag
));
51 bool HTMLOptionElement::checkDTD(const Node
* newChild
)
53 return newChild
->isTextNode() || newChild
->hasTagName(scriptTag
);
56 void HTMLOptionElement::attach()
58 if (parentNode()->renderStyle())
59 setRenderStyle(styleForRenderer());
60 HTMLFormControlElement::attach();
63 void HTMLOptionElement::detach()
66 HTMLFormControlElement::detach();
69 bool HTMLOptionElement::supportsFocus() const
71 return HTMLElement::supportsFocus();
74 bool HTMLOptionElement::isFocusable() const
76 // Option elements do not have a renderer so we check the renderStyle instead.
77 return supportsFocus() && renderStyle() && renderStyle()->display() != NONE
;
80 const AtomicString
& HTMLOptionElement::formControlType() const
82 DEFINE_STATIC_LOCAL(const AtomicString
, option
, ("option"));
86 String
HTMLOptionElement::text() const
88 return OptionElement::collectOptionLabelOrText(m_data
, this);
91 void HTMLOptionElement::setText(const String
&text
, ExceptionCode
& ec
)
93 // Handle the common special case where there's exactly 1 child node, and it's a text node.
94 Node
* child
= firstChild();
95 if (child
&& child
->isTextNode() && !child
->nextSibling()) {
96 static_cast<Text
*>(child
)->setData(text
, ec
);
101 appendChild(Text::create(document(), text
), ec
);
104 void HTMLOptionElement::accessKeyAction(bool)
106 HTMLSelectElement
* select
= ownerSelectElement();
108 select
->accessKeySetSelectedIndex(index());
111 int HTMLOptionElement::index() const
113 return OptionElement::optionIndex(ownerSelectElement(), this);
116 void HTMLOptionElement::parseMappedAttribute(MappedAttribute
*attr
)
118 if (attr
->name() == selectedAttr
)
119 m_data
.setSelected(!attr
->isNull());
120 else if (attr
->name() == valueAttr
)
121 m_data
.setValue(attr
->value());
122 else if (attr
->name() == labelAttr
)
123 m_data
.setLabel(attr
->value());
125 HTMLFormControlElement::parseMappedAttribute(attr
);
128 String
HTMLOptionElement::value() const
130 return OptionElement::collectOptionValue(m_data
, this);
133 void HTMLOptionElement::setValue(const String
& value
)
135 setAttribute(valueAttr
, value
);
138 bool HTMLOptionElement::selected() const
140 return m_data
.selected();
143 void HTMLOptionElement::setSelected(bool selected
)
145 if (m_data
.selected() == selected
)
148 OptionElement::setSelectedState(m_data
, this, selected
);
150 if (HTMLSelectElement
* select
= ownerSelectElement())
151 select
->setSelectedIndex(selected
? index() : -1, false);
154 void HTMLOptionElement::setSelectedState(bool selected
)
156 OptionElement::setSelectedState(m_data
, this, selected
);
159 void HTMLOptionElement::childrenChanged(bool changedByParser
, Node
* beforeChange
, Node
* afterChange
, int childCountDelta
)
161 HTMLSelectElement
* select
= ownerSelectElement();
163 select
->childrenChanged(changedByParser
);
164 HTMLFormControlElement::childrenChanged(changedByParser
, beforeChange
, afterChange
, childCountDelta
);
167 HTMLSelectElement
* HTMLOptionElement::ownerSelectElement() const
169 Node
* select
= parentNode();
170 while (select
&& !select
->hasTagName(selectTag
))
171 select
= select
->parentNode();
176 return static_cast<HTMLSelectElement
*>(select
);
179 bool HTMLOptionElement::defaultSelected() const
181 return !getAttribute(selectedAttr
).isNull();
184 void HTMLOptionElement::setDefaultSelected(bool b
)
186 setAttribute(selectedAttr
, b
? "" : 0);
189 String
HTMLOptionElement::label() const
191 return m_data
.label();
194 void HTMLOptionElement::setLabel(const String
& value
)
196 setAttribute(labelAttr
, value
);
199 void HTMLOptionElement::setRenderStyle(PassRefPtr
<RenderStyle
> newStyle
)
204 RenderStyle
* HTMLOptionElement::nonRendererRenderStyle() const
206 return m_style
.get();
209 String
HTMLOptionElement::textIndentedToRespectGroupLabel() const
211 return OptionElement::collectOptionTextRespectingGroupLabel(m_data
, this);
214 bool HTMLOptionElement::disabled() const
216 return HTMLFormControlElement::disabled() || (parentNode() && static_cast<HTMLFormControlElement
*>(parentNode())->disabled());
219 void HTMLOptionElement::insertedIntoTree(bool deep
)
221 if (HTMLSelectElement
* select
= ownerSelectElement()) {
222 select
->setRecalcListItems();
224 select
->setSelectedIndex(index(), false);
225 select
->scrollToSelection();
228 HTMLFormControlElement::insertedIntoTree(deep
);