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 PassRefPtrWillBeRawPtr<HTMLOptGroupElement> HTMLOptGroupElement::create(Document& document)
47 return adoptRefWillBeRefCountedGarbageCollected(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 if (HTMLSelectElement* select = Traversal<HTMLSelectElement>::firstAncestor(*this))
79 select->setRecalcListItems();
82 void HTMLOptGroupElement::attach(const AttachContext& context)
84 if (context.resolvedStyle) {
85 ASSERT(!m_style || m_style == context.resolvedStyle);
86 m_style = context.resolvedStyle;
88 HTMLElement::attach(context);
91 void HTMLOptGroupElement::detach(const AttachContext& context)
94 HTMLElement::detach(context);
97 void HTMLOptGroupElement::updateNonRenderStyle()
99 bool oldDisplayNoneStatus = isDisplayNone();
100 m_style = originalStyleForRenderer();
101 if (oldDisplayNoneStatus != isDisplayNone()) {
102 if (HTMLSelectElement* select = ownerSelectElement())
103 select->updateListOnRenderer();
107 RenderStyle* HTMLOptGroupElement::nonRendererStyle() const
109 return m_style.get();
112 PassRefPtr<RenderStyle> HTMLOptGroupElement::customStyleForRenderer()
114 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 return Traversal<HTMLSelectElement>::firstAncestor(*this);
135 void HTMLOptGroupElement::accessKeyAction(bool)
137 HTMLSelectElement* select = ownerSelectElement();
138 // send to the parent to bring focus to the list box
139 if (select && !select->focused())
140 select->accessKeyAction(false);
143 bool HTMLOptGroupElement::isDisplayNone() const
145 RenderStyle* style = nonRendererStyle();
146 return style && style->display() == NONE;