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 "HTMLOptGroupElement.h"
29 #include "HTMLNames.h"
30 #include "HTMLSelectElement.h"
31 #include "RenderMenuList.h"
32 #include "NodeRenderStyle.h"
33 #include "NodeRenderingContext.h"
34 #include "StyleResolver.h"
35 #include <wtf/StdLibExtras.h>
39 using namespace HTMLNames;
41 inline HTMLOptGroupElement::HTMLOptGroupElement(const QualifiedName& tagName, Document* document)
42 : HTMLElement(tagName, document)
44 ASSERT(hasTagName(optgroupTag));
45 setHasCustomCallbacks();
48 PassRefPtr<HTMLOptGroupElement> HTMLOptGroupElement::create(const QualifiedName& tagName, Document* document)
50 return adoptRef(new HTMLOptGroupElement(tagName, document));
53 bool HTMLOptGroupElement::disabled() const
55 return fastHasAttribute(disabledAttr);
58 bool HTMLOptGroupElement::supportsFocus() const
60 return HTMLElement::supportsFocus();
63 bool HTMLOptGroupElement::isFocusable() const
65 // Optgroup elements do not have a renderer so we check the renderStyle instead.
66 return supportsFocus() && renderStyle() && renderStyle()->display() != NONE;
69 const AtomicString& HTMLOptGroupElement::formControlType() const
71 DEFINE_STATIC_LOCAL(const AtomicString, optgroup, ("optgroup", AtomicString::ConstructFromLiteral));
75 void HTMLOptGroupElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
77 recalcSelectOptions();
78 HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
81 void HTMLOptGroupElement::parseAttribute(const Attribute& attribute)
83 HTMLElement::parseAttribute(attribute);
84 recalcSelectOptions();
86 if (attribute.name() == disabledAttr)
87 invalidateParentDistributionIfNecessary(this, SelectRuleFeatureSet::RuleFeatureDisabled | SelectRuleFeatureSet::RuleFeatureEnabled);
90 void HTMLOptGroupElement::recalcSelectOptions()
92 ContainerNode* select = parentNode();
93 while (select && !select->hasTagName(selectTag))
94 select = select->parentNode();
96 toHTMLSelectElement(select)->setRecalcListItems();
99 void HTMLOptGroupElement::attach()
101 HTMLElement::attach();
102 // If after attaching nothing called styleForRenderer() on this node we
103 // manually cache the value. This happens if our parent doesn't have a
104 // renderer like <optgroup> or if it doesn't allow children like <select>.
105 if (!m_style && parentNode()->renderStyle())
106 updateNonRenderStyle();
109 void HTMLOptGroupElement::detach()
112 HTMLElement::detach();
115 void HTMLOptGroupElement::updateNonRenderStyle()
117 m_style = document()->styleResolver()->styleForElement(this);
120 RenderStyle* HTMLOptGroupElement::nonRendererStyle() const
122 return m_style.get();
125 PassRefPtr<RenderStyle> HTMLOptGroupElement::customStyleForRenderer()
127 // styleForRenderer is called whenever a new style should be associated
128 // with an Element so now is a good time to update our cached style.
129 updateNonRenderStyle();
133 String HTMLOptGroupElement::groupLabelText() const
135 String itemText = document()->displayStringModifiedByEncoding(getAttribute(labelAttr));
137 // In WinIE, leading and trailing whitespace is ignored in options and optgroups. We match this behavior.
138 itemText = itemText.stripWhiteSpace();
139 // We want to collapse our whitespace too. This will match other browsers.
140 itemText = itemText.simplifyWhiteSpace();
145 HTMLSelectElement* HTMLOptGroupElement::ownerSelectElement() const
147 ContainerNode* select = parentNode();
148 while (select && !select->hasTagName(selectTag))
149 select = select->parentNode();
154 return toHTMLSelectElement(select);
157 void HTMLOptGroupElement::accessKeyAction(bool)
159 HTMLSelectElement* select = ownerSelectElement();
160 // send to the parent to bring focus to the list box
161 if (select && !select->focused())
162 select->accessKeyAction(false);