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"
28 #include "CSSStyleSelector.h"
30 #include "HTMLNames.h"
31 #include "HTMLSelectElement.h"
32 #include "RenderMenuList.h"
33 #include "NodeRenderStyle.h"
34 #include "NodeRenderingContext.h"
35 #include <wtf/StdLibExtras.h>
39 using namespace HTMLNames;
41 inline HTMLOptGroupElement::HTMLOptGroupElement(const QualifiedName& tagName, Document* document, HTMLFormElement* form)
42 : HTMLFormControlElement(tagName, document, form)
44 ASSERT(hasTagName(optgroupTag));
47 PassRefPtr<HTMLOptGroupElement> HTMLOptGroupElement::create(const QualifiedName& tagName, Document* document, HTMLFormElement* form)
49 return adoptRef(new HTMLOptGroupElement(tagName, document, form));
52 bool HTMLOptGroupElement::supportsFocus() const
54 return HTMLElement::supportsFocus();
57 bool HTMLOptGroupElement::isFocusable() const
59 // Optgroup elements do not have a renderer so we check the renderStyle instead.
60 return supportsFocus() && renderStyle() && renderStyle()->display() != NONE;
63 const AtomicString& HTMLOptGroupElement::formControlType() const
65 DEFINE_STATIC_LOCAL(const AtomicString, optgroup, ("optgroup"));
69 void HTMLOptGroupElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
71 recalcSelectOptions();
72 HTMLFormControlElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
75 void HTMLOptGroupElement::parseMappedAttribute(Attribute* attr)
77 HTMLFormControlElement::parseMappedAttribute(attr);
78 recalcSelectOptions();
81 void HTMLOptGroupElement::recalcSelectOptions()
83 ContainerNode* select = parentNode();
84 while (select && !select->hasTagName(selectTag))
85 select = select->parentNode();
87 toHTMLSelectElement(select)->setRecalcListItems();
90 void HTMLOptGroupElement::attach()
92 if (parentNode()->renderStyle())
93 setRenderStyle(styleForRenderer());
94 HTMLFormControlElement::attach();
97 void HTMLOptGroupElement::detach()
100 HTMLFormControlElement::detach();
103 void HTMLOptGroupElement::setRenderStyle(PassRefPtr<RenderStyle> newStyle)
108 RenderStyle* HTMLOptGroupElement::nonRendererRenderStyle() const
110 return m_style.get();
113 String HTMLOptGroupElement::groupLabelText() const
115 String itemText = document()->displayStringModifiedByEncoding(getAttribute(labelAttr));
117 // In WinIE, leading and trailing whitespace is ignored in options and optgroups. We match this behavior.
118 itemText = itemText.stripWhiteSpace();
119 // We want to collapse our whitespace too. This will match other browsers.
120 itemText = itemText.simplifyWhiteSpace();
125 HTMLSelectElement* HTMLOptGroupElement::ownerSelectElement() const
127 ContainerNode* select = parentNode();
128 while (select && !select->hasTagName(selectTag))
129 select = select->parentNode();
134 return toHTMLSelectElement(select);
137 void HTMLOptGroupElement::accessKeyAction(bool)
139 HTMLSelectElement* select = ownerSelectElement();
140 // send to the parent to bring focus to the list box
141 if (select && !select->focused())
142 select->accessKeyAction(false);