1/**
2 * Copyright (C) 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB.  If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 *
19 */
20
21#include "config.h"
22
23#if ENABLE(WML)
24#include "WMLOptGroupElement.h"
25
26#include "Document.h"
27#include "MappedAttribute.h"
28#include "HTMLNames.h"
29#include "NodeRenderStyle.h"
30#include "RenderStyle.h"
31#include "WMLNames.h"
32#include "WMLSelectElement.h"
33
34namespace WebCore {
35
36using namespace WMLNames;
37
38WMLOptGroupElement::WMLOptGroupElement(const QualifiedName& tagName, Document* doc)
39    : WMLFormControlElement(tagName, doc)
40{
41}
42
43WMLOptGroupElement::~WMLOptGroupElement()
44{
45}
46
47const AtomicString& WMLOptGroupElement::formControlType() const
48{
49    DEFINE_STATIC_LOCAL(const AtomicString, optgroup, ("optgroup"));
50    return optgroup;
51}
52
53bool WMLOptGroupElement::insertBefore(PassRefPtr<Node> newChild, Node* refChild, ExceptionCode& ec, bool shouldLazyAttach)
54{
55    bool result = WMLFormControlElement::insertBefore(newChild, refChild, ec, shouldLazyAttach);
56    if (result)
57        recalcSelectOptions();
58    return result;
59}
60
61bool WMLOptGroupElement::replaceChild(PassRefPtr<Node> newChild, Node* oldChild, ExceptionCode& ec, bool shouldLazyAttach)
62{
63    bool result = WMLFormControlElement::replaceChild(newChild, oldChild, ec, shouldLazyAttach);
64    if (result)
65        recalcSelectOptions();
66    return result;
67}
68
69bool WMLOptGroupElement::removeChild(Node* oldChild, ExceptionCode& ec)
70{
71    bool result = WMLFormControlElement::removeChild(oldChild, ec);
72    if (result)
73        recalcSelectOptions();
74    return result;
75}
76
77bool WMLOptGroupElement::appendChild(PassRefPtr<Node> newChild, ExceptionCode& ec, bool shouldLazyAttach)
78{
79    bool result = WMLFormControlElement::appendChild(newChild, ec, shouldLazyAttach);
80    if (result)
81        recalcSelectOptions();
82    return result;
83}
84
85bool WMLOptGroupElement::removeChildren()
86{
87    bool result = WMLFormControlElement::removeChildren();
88    if (result)
89        recalcSelectOptions();
90    return result;
91}
92
93static inline WMLSelectElement* ownerSelectElement(Element* element)
94{
95    Node* select = element->parentNode();
96    while (select && !select->hasTagName(selectTag))
97        select = select->parentNode();
98
99    if (!select)
100        return 0;
101
102    return static_cast<WMLSelectElement*>(select);
103}
104
105void WMLOptGroupElement::accessKeyAction(bool)
106{
107    WMLSelectElement* select = ownerSelectElement(this);
108    if (!select || select->focused())
109        return;
110
111    // send to the parent to bring focus to the list box
112    select->accessKeyAction(false);
113}
114
115void WMLOptGroupElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
116{
117    recalcSelectOptions();
118    WMLFormControlElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
119}
120
121void WMLOptGroupElement::parseMappedAttribute(MappedAttribute* attr)
122{
123    WMLFormControlElement::parseMappedAttribute(attr);
124    recalcSelectOptions();
125}
126
127void WMLOptGroupElement::attach()
128{
129    if (parentNode()->renderStyle())
130        setRenderStyle(styleForRenderer());
131    WMLFormControlElement::attach();
132}
133
134void WMLOptGroupElement::detach()
135{
136    m_style.clear();
137    WMLFormControlElement::detach();
138}
139
140void WMLOptGroupElement::setRenderStyle(PassRefPtr<RenderStyle> style)
141{
142    m_style = style;
143}
144
145RenderStyle* WMLOptGroupElement::nonRendererRenderStyle() const
146{
147    return m_style.get();
148}
149
150String WMLOptGroupElement::groupLabelText() const
151{
152    String itemText = document()->displayStringModifiedByEncoding(title());
153
154    // In WinIE, leading and trailing whitespace is ignored in options and optgroups. We match this behavior.
155    itemText = itemText.stripWhiteSpace();
156    // We want to collapse our whitespace too.  This will match other browsers.
157    itemText = itemText.simplifyWhiteSpace();
158
159    return itemText;
160}
161
162void WMLOptGroupElement::recalcSelectOptions()
163{
164    if (WMLSelectElement* select = ownerSelectElement(this))
165        select->setRecalcListItems();
166}
167
168}
169
170#endif
171