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
type() const52 const AtomicString& HTMLOptGroupElement::type() 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 if (result)
62 recalcSelectOptions();
63 return result;
64 }
65
replaceChild(PassRefPtr<Node> newChild,Node * oldChild,ExceptionCode & ec,bool shouldLazyAttach)66 bool HTMLOptGroupElement::replaceChild(PassRefPtr<Node> newChild, Node* oldChild, ExceptionCode& ec, bool shouldLazyAttach)
67 {
68 bool result = HTMLFormControlElement::replaceChild(newChild, oldChild, ec, shouldLazyAttach);
69 if (result)
70 recalcSelectOptions();
71 return result;
72 }
73
removeChild(Node * oldChild,ExceptionCode & ec)74 bool HTMLOptGroupElement::removeChild(Node* oldChild, ExceptionCode& ec)
75 {
76 bool result = HTMLFormControlElement::removeChild(oldChild, ec);
77 if (result)
78 recalcSelectOptions();
79 return result;
80 }
81
appendChild(PassRefPtr<Node> newChild,ExceptionCode & ec,bool shouldLazyAttach)82 bool HTMLOptGroupElement::appendChild(PassRefPtr<Node> newChild, ExceptionCode& ec, bool shouldLazyAttach)
83 {
84 bool result = HTMLFormControlElement::appendChild(newChild, ec, shouldLazyAttach);
85 if (result)
86 recalcSelectOptions();
87 return result;
88 }
89
removeChildren()90 bool HTMLOptGroupElement::removeChildren()
91 {
92 bool result = HTMLFormControlElement::removeChildren();
93 if (result)
94 recalcSelectOptions();
95 return result;
96 }
97
childrenChanged(bool changedByParser,Node * beforeChange,Node * afterChange,int childCountDelta)98 void HTMLOptGroupElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
99 {
100 recalcSelectOptions();
101 HTMLFormControlElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
102 }
103
parseMappedAttribute(MappedAttribute * attr)104 void HTMLOptGroupElement::parseMappedAttribute(MappedAttribute* attr)
105 {
106 HTMLFormControlElement::parseMappedAttribute(attr);
107 recalcSelectOptions();
108 }
109
recalcSelectOptions()110 void HTMLOptGroupElement::recalcSelectOptions()
111 {
112 Node* select = parentNode();
113 while (select && !select->hasTagName(selectTag))
114 select = select->parentNode();
115 if (select)
116 static_cast<HTMLSelectElement*>(select)->setRecalcListItems();
117 }
118
label() const119 String HTMLOptGroupElement::label() const
120 {
121 return getAttribute(labelAttr);
122 }
123
setLabel(const String & value)124 void HTMLOptGroupElement::setLabel(const String &value)
125 {
126 setAttribute(labelAttr, value);
127 }
128
checkDTD(const Node * newChild)129 bool HTMLOptGroupElement::checkDTD(const Node* newChild)
130 {
131 // Make sure to keep this in sync with <select> (other than not allowing an optgroup).
132 return newChild->isTextNode() || newChild->hasTagName(HTMLNames::optionTag) || newChild->hasTagName(HTMLNames::hrTag) || newChild->hasTagName(HTMLNames::scriptTag);
133 }
134
attach()135 void HTMLOptGroupElement::attach()
136 {
137 if (parentNode()->renderStyle())
138 setRenderStyle(styleForRenderer());
139 HTMLFormControlElement::attach();
140 }
141
detach()142 void HTMLOptGroupElement::detach()
143 {
144 m_style.clear();
145 HTMLFormControlElement::detach();
146 }
147
setRenderStyle(PassRefPtr<RenderStyle> newStyle)148 void HTMLOptGroupElement::setRenderStyle(PassRefPtr<RenderStyle> newStyle)
149 {
150 m_style = newStyle;
151 }
152
nonRendererRenderStyle() const153 RenderStyle* HTMLOptGroupElement::nonRendererRenderStyle() const
154 {
155 return m_style.get();
156 }
157
groupLabelText() const158 String HTMLOptGroupElement::groupLabelText() const
159 {
160 String itemText = document()->displayStringModifiedByEncoding(getAttribute(labelAttr));
161
162 // In WinIE, leading and trailing whitespace is ignored in options and optgroups. We match this behavior.
163 itemText = itemText.stripWhiteSpace();
164 // We want to collapse our whitespace too. This will match other browsers.
165 itemText = itemText.simplifyWhiteSpace();
166
167 return itemText;
168 }
169
ownerSelectElement() const170 HTMLSelectElement* HTMLOptGroupElement::ownerSelectElement() const
171 {
172 Node* select = parentNode();
173 while (select && !select->hasTagName(selectTag))
174 select = select->parentNode();
175
176 if (!select)
177 return 0;
178
179 return static_cast<HTMLSelectElement*>(select);
180 }
181
accessKeyAction(bool)182 void HTMLOptGroupElement::accessKeyAction(bool)
183 {
184 HTMLSelectElement* select = ownerSelectElement();
185 // send to the parent to bring focus to the list box
186 if (select && !select->focused())
187 select->accessKeyAction(false);
188 }
189
190 } // namespace
191