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, 2010 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 "core/html/HTMLOptGroupElement.h"
28 #include "HTMLNames.h"
29 #include "core/dom/Document.h"
30 #include "core/dom/NodeRenderStyle.h"
31 #include "core/html/HTMLSelectElement.h"
32 #include "wtf/StdLibExtras.h"
36 using namespace HTMLNames;
38 inline HTMLOptGroupElement::HTMLOptGroupElement(Document& document)
39 : HTMLElement(optgroupTag, document)
41 setHasCustomStyleCallbacks();
42 ScriptWrappable::init(this);
45 PassRefPtr<HTMLOptGroupElement> HTMLOptGroupElement::create(Document& document)
47 return adoptRef(new HTMLOptGroupElement(document));
50 bool HTMLOptGroupElement::isDisabledFormControl() const
52 return fastHasAttribute(disabledAttr);
55 bool HTMLOptGroupElement::rendererIsFocusable() const
57 // Optgroup elements do not have a renderer so we check the renderStyle instead.
58 return renderStyle() && renderStyle()->display() != NONE;
61 void HTMLOptGroupElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
63 recalcSelectOptions();
64 HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
67 void HTMLOptGroupElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
69 HTMLElement::parseAttribute(name, value);
70 recalcSelectOptions();
72 if (name == disabledAttr)
73 didAffectSelector(AffectedSelectorDisabled | AffectedSelectorEnabled);
76 void HTMLOptGroupElement::recalcSelectOptions()
78 ContainerNode* select = parentNode();
79 while (select && !select->hasTagName(selectTag))
80 select = select->parentNode();
82 toHTMLSelectElement(select)->setRecalcListItems();
85 void HTMLOptGroupElement::attach(const AttachContext& context)
87 updateNonRenderStyle();
88 HTMLElement::attach(context);
91 void HTMLOptGroupElement::detach(const AttachContext& context)
94 HTMLElement::detach(context);
97 void HTMLOptGroupElement::updateNonRenderStyle()
99 m_style = originalStyleForRenderer();
102 RenderStyle* HTMLOptGroupElement::nonRendererStyle() const
104 return m_style.get();
107 PassRefPtr<RenderStyle> HTMLOptGroupElement::customStyleForRenderer()
112 void HTMLOptGroupElement::willRecalcStyle(StyleRecalcChange change)
114 if (!needsAttach() && (needsStyleRecalc() || change >= Inherit))
115 updateNonRenderStyle();
118 String HTMLOptGroupElement::groupLabelText() const
120 String itemText = getAttribute(labelAttr);
122 // In WinIE, leading and trailing whitespace is ignored in options and optgroups. We match this behavior.
123 itemText = itemText.stripWhiteSpace();
124 // We want to collapse our whitespace too. This will match other browsers.
125 itemText = itemText.simplifyWhiteSpace();
130 HTMLSelectElement* HTMLOptGroupElement::ownerSelectElement() const
132 ContainerNode* select = parentNode();
133 while (select && !select->hasTagName(selectTag))
134 select = select->parentNode();
139 return toHTMLSelectElement(select);
142 void HTMLOptGroupElement::accessKeyAction(bool)
144 HTMLSelectElement* select = ownerSelectElement();
145 // send to the parent to bring focus to the list box
146 if (select && !select->focused())
147 select->accessKeyAction(false);