Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / HTMLOptGroupElement.cpp
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, 2010 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 "core/html/HTMLOptGroupElement.h"
27
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"
33
34 namespace WebCore {
35
36 using namespace HTMLNames;
37
38 inline HTMLOptGroupElement::HTMLOptGroupElement(Document& document)
39     : HTMLElement(optgroupTag, document)
40 {
41     setHasCustomStyleCallbacks();
42     ScriptWrappable::init(this);
43 }
44
45 PassRefPtr<HTMLOptGroupElement> HTMLOptGroupElement::create(Document& document)
46 {
47     return adoptRef(new HTMLOptGroupElement(document));
48 }
49
50 bool HTMLOptGroupElement::isDisabledFormControl() const
51 {
52     return fastHasAttribute(disabledAttr);
53 }
54
55 bool HTMLOptGroupElement::rendererIsFocusable() const
56 {
57     // Optgroup elements do not have a renderer so we check the renderStyle instead.
58     return renderStyle() && renderStyle()->display() != NONE;
59 }
60
61 void HTMLOptGroupElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
62 {
63     recalcSelectOptions();
64     HTMLElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
65 }
66
67 void HTMLOptGroupElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
68 {
69     HTMLElement::parseAttribute(name, value);
70     recalcSelectOptions();
71
72     if (name == disabledAttr)
73         didAffectSelector(AffectedSelectorDisabled | AffectedSelectorEnabled);
74 }
75
76 void HTMLOptGroupElement::recalcSelectOptions()
77 {
78     ContainerNode* select = parentNode();
79     while (select && !select->hasTagName(selectTag))
80         select = select->parentNode();
81     if (select)
82         toHTMLSelectElement(select)->setRecalcListItems();
83 }
84
85 void HTMLOptGroupElement::attach(const AttachContext& context)
86 {
87     updateNonRenderStyle();
88     HTMLElement::attach(context);
89 }
90
91 void HTMLOptGroupElement::detach(const AttachContext& context)
92 {
93     m_style.clear();
94     HTMLElement::detach(context);
95 }
96
97 void HTMLOptGroupElement::updateNonRenderStyle()
98 {
99     m_style = originalStyleForRenderer();
100 }
101
102 RenderStyle* HTMLOptGroupElement::nonRendererStyle() const
103 {
104     return m_style.get();
105 }
106
107 PassRefPtr<RenderStyle> HTMLOptGroupElement::customStyleForRenderer()
108 {
109     return m_style;
110 }
111
112 void HTMLOptGroupElement::willRecalcStyle(StyleRecalcChange change)
113 {
114     if (!needsAttach() && (needsStyleRecalc() || change >= Inherit))
115         updateNonRenderStyle();
116 }
117
118 String HTMLOptGroupElement::groupLabelText() const
119 {
120     String itemText = getAttribute(labelAttr);
121
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();
126
127     return itemText;
128 }
129
130 HTMLSelectElement* HTMLOptGroupElement::ownerSelectElement() const
131 {
132     ContainerNode* select = parentNode();
133     while (select && !select->hasTagName(selectTag))
134         select = select->parentNode();
135
136     if (!select)
137        return 0;
138
139     return toHTMLSelectElement(select);
140 }
141
142 void HTMLOptGroupElement::accessKeyAction(bool)
143 {
144     HTMLSelectElement* select = ownerSelectElement();
145     // send to the parent to bring focus to the list box
146     if (select && !select->focused())
147         select->accessKeyAction(false);
148 }
149
150 } // namespace