Upstream version 7.36.149.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 PassRefPtrWillBeRawPtr<HTMLOptGroupElement> HTMLOptGroupElement::create(Document& document)
46 {
47     return adoptRefWillBeRefCountedGarbageCollected(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     if (HTMLSelectElement* select = Traversal<HTMLSelectElement>::firstAncestor(*this))
79         select->setRecalcListItems();
80 }
81
82 void HTMLOptGroupElement::attach(const AttachContext& context)
83 {
84     if (context.resolvedStyle) {
85         ASSERT(!m_style || m_style == context.resolvedStyle);
86         m_style = context.resolvedStyle;
87     }
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     bool oldDisplayNoneStatus = isDisplayNone();
100     m_style = originalStyleForRenderer();
101     if (oldDisplayNoneStatus != isDisplayNone()) {
102         if (HTMLSelectElement* select = ownerSelectElement())
103             select->updateListOnRenderer();
104     }
105 }
106
107 RenderStyle* HTMLOptGroupElement::nonRendererStyle() const
108 {
109     return m_style.get();
110 }
111
112 PassRefPtr<RenderStyle> HTMLOptGroupElement::customStyleForRenderer()
113 {
114     updateNonRenderStyle();
115     return m_style;
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     return Traversal<HTMLSelectElement>::firstAncestor(*this);
133 }
134
135 void HTMLOptGroupElement::accessKeyAction(bool)
136 {
137     HTMLSelectElement* select = ownerSelectElement();
138     // send to the parent to bring focus to the list box
139     if (select && !select->focused())
140         select->accessKeyAction(false);
141 }
142
143 bool HTMLOptGroupElement::isDisplayNone() const
144 {
145     RenderStyle* style = nonRendererStyle();
146     return style && style->display() == NONE;
147 }
148
149 } // namespace