1 /*
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)
7 *
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.
12 *
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.
17 *
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.
22 *
23 */
24
25 #include "config.h"
26 #include "HTMLOptGroupElement.h"
27
28 #include "CSSStyleSelector.h"
29 #include "Document.h"
30 #include "HTMLNames.h"
31 #include "HTMLSelectElement.h"
32 #include "RenderMenuList.h"
33 #include "NodeRenderStyle.h"
34 #include <wtf/StdLibExtras.h>
35
36 namespace WebCore {
37
38 using namespace HTMLNames;
39
HTMLOptGroupElement(const QualifiedName & tagName,Document * doc,HTMLFormElement * f)40 HTMLOptGroupElement::HTMLOptGroupElement(const QualifiedName& tagName, Document* doc, HTMLFormElement* f)
41 : HTMLFormControlElement(tagName, doc, f)
42 , m_style(0)
43 {
44 ASSERT(hasTagName(optgroupTag));
45 }
46
isFocusable() const47 bool HTMLOptGroupElement::isFocusable() const
48 {
49 return HTMLElement::isFocusable();
50 }
51
formControlType() const52 const AtomicString& HTMLOptGroupElement::formControlType() const
53 {
54 DEFINE_STATIC_LOCAL(const AtomicString, optgroup, ("optgroup"));
55 return optgroup;
56 }
57
insertBefore(PassRefPtr<Node> newChild,Node * refChild,ExceptionCode & ec,bool shouldLazyAttach)58 bool HTMLOptGroupElement::insertBefore(PassRefPtr<Node> newChild, Node* refChild, ExceptionCode& ec, bool shouldLazyAttach)
59 {
60 bool result = HTMLFormControlElement::insertBefore(newChild, refChild, ec, shouldLazyAttach);
61 return result;
62 }
63
replaceChild(PassRefPtr<Node> newChild,Node * oldChild,ExceptionCode & ec,bool shouldLazyAttach)64 bool HTMLOptGroupElement::replaceChild(PassRefPtr<Node> newChild, Node* oldChild, ExceptionCode& ec, bool shouldLazyAttach)
65 {
66 bool result = HTMLFormControlElement::replaceChild(newChild, oldChild, ec, shouldLazyAttach);
67 return result;
68 }
69
removeChild(Node * oldChild,ExceptionCode & ec)70 bool HTMLOptGroupElement::removeChild(Node* oldChild, ExceptionCode& ec)
71 {
72 bool result = HTMLFormControlElement::removeChild(oldChild, ec);
73 return result;
74 }
75
appendChild(PassRefPtr<Node> newChild,ExceptionCode & ec,bool shouldLazyAttach)76 bool HTMLOptGroupElement::appendChild(PassRefPtr<Node> newChild, ExceptionCode& ec, bool shouldLazyAttach)
77 {
78 bool result = HTMLFormControlElement::appendChild(newChild, ec, shouldLazyAttach);
79 return result;
80 }
81
removeChildren()82 bool HTMLOptGroupElement::removeChildren()
83 {
84 bool result = HTMLFormControlElement::removeChildren();
85 return result;
86 }
87
childrenChanged(bool changedByParser,Node * beforeChange,Node * afterChange,int childCountDelta)88 void HTMLOptGroupElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
89 {
90 recalcSelectOptions();
91 HTMLFormControlElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
92 }
93
parseMappedAttribute(MappedAttribute * attr)94 void HTMLOptGroupElement::parseMappedAttribute(MappedAttribute* attr)
95 {
96 HTMLFormControlElement::parseMappedAttribute(attr);
97 recalcSelectOptions();
98 }
99
recalcSelectOptions()100 void HTMLOptGroupElement::recalcSelectOptions()
101 {
102 Node* select = parentNode();
103 while (select && !select->hasTagName(selectTag))
104 select = select->parentNode();
105 if (select)
106 static_cast<HTMLSelectElement*>(select)->setRecalcListItems();
107 }
108
label() const109 String HTMLOptGroupElement::label() const
110 {
111 return getAttribute(labelAttr);
112 }
113
setLabel(const String & value)114 void HTMLOptGroupElement::setLabel(const String &value)
115 {
116 setAttribute(labelAttr, value);
117 }
118
checkDTD(const Node * newChild)119 bool HTMLOptGroupElement::checkDTD(const Node* newChild)
120 {
121 // Make sure to keep this in sync with <select> (other than not allowing an optgroup).
122 return newChild->isTextNode() || newChild->hasTagName(HTMLNames::optionTag) || newChild->hasTagName(HTMLNames::hrTag) || newChild->hasTagName(HTMLNames::scriptTag);
123 }
124
attach()125 void HTMLOptGroupElement::attach()
126 {
127 if (parentNode()->renderStyle())
128 setRenderStyle(styleForRenderer());
129 HTMLFormControlElement::attach();
130 }
131
detach()132 void HTMLOptGroupElement::detach()
133 {
134 m_style.clear();
135 HTMLFormControlElement::detach();
136 }
137
setRenderStyle(PassRefPtr<RenderStyle> newStyle)138 void HTMLOptGroupElement::setRenderStyle(PassRefPtr<RenderStyle> newStyle)
139 {
140 m_style = newStyle;
141 }
142
nonRendererRenderStyle() const143 RenderStyle* HTMLOptGroupElement::nonRendererRenderStyle() const
144 {
145 return m_style.get();
146 }
147
groupLabelText() const148 String HTMLOptGroupElement::groupLabelText() const
149 {
150 String itemText = document()->displayStringModifiedByEncoding(getAttribute(labelAttr));
151
152 // In WinIE, leading and trailing whitespace is ignored in options and optgroups. We match this behavior.
153 itemText = itemText.stripWhiteSpace();
154 // We want to collapse our whitespace too. This will match other browsers.
155 itemText = itemText.simplifyWhiteSpace();
156
157 return itemText;
158 }
159
ownerSelectElement() const160 HTMLSelectElement* HTMLOptGroupElement::ownerSelectElement() const
161 {
162 Node* select = parentNode();
163 while (select && !select->hasTagName(selectTag))
164 select = select->parentNode();
165
166 if (!select)
167 return 0;
168
169 return static_cast<HTMLSelectElement*>(select);
170 }
171
accessKeyAction(bool)172 void HTMLOptGroupElement::accessKeyAction(bool)
173 {
174 HTMLSelectElement* select = ownerSelectElement();
175 // send to the parent to bring focus to the list box
176 if (select && !select->focused())
177 select->accessKeyAction(false);
178 }
179
180 } // namespace
181