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, 2007, 2008 Apple Inc. All rights reserved.
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 "HTMLOptGroupElement.h"
28 #include "CSSStyleSelector.h"
30 #include "HTMLNames.h"
31 #include "HTMLSelectElement.h"
32 #include "RenderMenuList.h"
33 #include "NodeRenderStyle.h"
34 #include <wtf/StdLibExtras.h>
38 using namespace HTMLNames
;
40 HTMLOptGroupElement::HTMLOptGroupElement(const QualifiedName
& tagName
, Document
* doc
, HTMLFormElement
* f
)
41 : HTMLFormControlElement(tagName
, doc
, f
)
44 ASSERT(hasTagName(optgroupTag
));
47 bool HTMLOptGroupElement::supportsFocus() const
49 return HTMLElement::supportsFocus();
52 bool HTMLOptGroupElement::isFocusable() const
54 // Optgroup elements do not have a renderer so we check the renderStyle instead.
55 return supportsFocus() && renderStyle() && renderStyle()->display() != NONE
;
58 const AtomicString
& HTMLOptGroupElement::formControlType() const
60 DEFINE_STATIC_LOCAL(const AtomicString
, optgroup
, ("optgroup"));
64 bool HTMLOptGroupElement::insertBefore(PassRefPtr
<Node
> newChild
, Node
* refChild
, ExceptionCode
& ec
, bool shouldLazyAttach
)
66 bool result
= HTMLFormControlElement::insertBefore(newChild
, refChild
, ec
, shouldLazyAttach
);
70 bool HTMLOptGroupElement::replaceChild(PassRefPtr
<Node
> newChild
, Node
* oldChild
, ExceptionCode
& ec
, bool shouldLazyAttach
)
72 bool result
= HTMLFormControlElement::replaceChild(newChild
, oldChild
, ec
, shouldLazyAttach
);
76 bool HTMLOptGroupElement::removeChild(Node
* oldChild
, ExceptionCode
& ec
)
78 bool result
= HTMLFormControlElement::removeChild(oldChild
, ec
);
82 bool HTMLOptGroupElement::appendChild(PassRefPtr
<Node
> newChild
, ExceptionCode
& ec
, bool shouldLazyAttach
)
84 bool result
= HTMLFormControlElement::appendChild(newChild
, ec
, shouldLazyAttach
);
88 bool HTMLOptGroupElement::removeChildren()
90 bool result
= HTMLFormControlElement::removeChildren();
94 void HTMLOptGroupElement::childrenChanged(bool changedByParser
, Node
* beforeChange
, Node
* afterChange
, int childCountDelta
)
96 recalcSelectOptions();
97 HTMLFormControlElement::childrenChanged(changedByParser
, beforeChange
, afterChange
, childCountDelta
);
100 void HTMLOptGroupElement::parseMappedAttribute(MappedAttribute
* attr
)
102 HTMLFormControlElement::parseMappedAttribute(attr
);
103 recalcSelectOptions();
106 void HTMLOptGroupElement::recalcSelectOptions()
108 Node
* select
= parentNode();
109 while (select
&& !select
->hasTagName(selectTag
))
110 select
= select
->parentNode();
112 static_cast<HTMLSelectElement
*>(select
)->setRecalcListItems();
115 String
HTMLOptGroupElement::label() const
117 return getAttribute(labelAttr
);
120 void HTMLOptGroupElement::setLabel(const String
&value
)
122 setAttribute(labelAttr
, value
);
125 bool HTMLOptGroupElement::checkDTD(const Node
* newChild
)
127 // Make sure to keep this in sync with <select> (other than not allowing an optgroup).
128 return newChild
->isTextNode() || newChild
->hasTagName(HTMLNames::optionTag
) || newChild
->hasTagName(HTMLNames::hrTag
) || newChild
->hasTagName(HTMLNames::scriptTag
);
131 void HTMLOptGroupElement::attach()
133 if (parentNode()->renderStyle())
134 setRenderStyle(styleForRenderer());
135 HTMLFormControlElement::attach();
138 void HTMLOptGroupElement::detach()
141 HTMLFormControlElement::detach();
144 void HTMLOptGroupElement::setRenderStyle(PassRefPtr
<RenderStyle
> newStyle
)
149 RenderStyle
* HTMLOptGroupElement::nonRendererRenderStyle() const
151 return m_style
.get();
154 String
HTMLOptGroupElement::groupLabelText() const
156 String itemText
= document()->displayStringModifiedByEncoding(getAttribute(labelAttr
));
158 // In WinIE, leading and trailing whitespace is ignored in options and optgroups. We match this behavior.
159 itemText
= itemText
.stripWhiteSpace();
160 // We want to collapse our whitespace too. This will match other browsers.
161 itemText
= itemText
.simplifyWhiteSpace();
166 HTMLSelectElement
* HTMLOptGroupElement::ownerSelectElement() const
168 Node
* select
= parentNode();
169 while (select
&& !select
->hasTagName(selectTag
))
170 select
= select
->parentNode();
175 return static_cast<HTMLSelectElement
*>(select
);
178 void HTMLOptGroupElement::accessKeyAction(bool)
180 HTMLSelectElement
* select
= ownerSelectElement();
181 // send to the parent to bring focus to the list box
182 if (select
&& !select
->focused())
183 select
->accessKeyAction(false);