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 "Attribute.h"
27#include "Document.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
43PassRefPtr<WMLOptGroupElement> WMLOptGroupElement::create(const QualifiedName& tagName, Document* document)
44{
45    return adoptRef(new WMLOptGroupElement(tagName, document));
46}
47
48WMLOptGroupElement::~WMLOptGroupElement()
49{
50}
51
52const AtomicString& WMLOptGroupElement::formControlType() const
53{
54    DEFINE_STATIC_LOCAL(const AtomicString, optgroup, ("optgroup"));
55    return optgroup;
56}
57
58static inline WMLSelectElement* ownerSelectElement(Element* element)
59{
60    ContainerNode* select = element->parentNode();
61    while (select && !select->hasTagName(selectTag))
62        select = select->parentNode();
63
64    if (!select)
65        return 0;
66
67    return static_cast<WMLSelectElement*>(select);
68}
69
70void WMLOptGroupElement::accessKeyAction(bool)
71{
72    WMLSelectElement* select = ownerSelectElement(this);
73    if (!select || select->focused())
74        return;
75
76    // send to the parent to bring focus to the list box
77    select->accessKeyAction(false);
78}
79
80void WMLOptGroupElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
81{
82    recalcSelectOptions();
83    WMLFormControlElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
84}
85
86void WMLOptGroupElement::parseMappedAttribute(Attribute* attr)
87{
88    WMLFormControlElement::parseMappedAttribute(attr);
89    recalcSelectOptions();
90}
91
92void WMLOptGroupElement::attach()
93{
94    if (parentNode()->renderStyle())
95        setRenderStyle(styleForRenderer());
96    WMLFormControlElement::attach();
97}
98
99void WMLOptGroupElement::detach()
100{
101    m_style.clear();
102    WMLFormControlElement::detach();
103}
104
105void WMLOptGroupElement::setRenderStyle(PassRefPtr<RenderStyle> style)
106{
107    m_style = style;
108}
109
110RenderStyle* WMLOptGroupElement::nonRendererRenderStyle() const
111{
112    return m_style.get();
113}
114
115String WMLOptGroupElement::groupLabelText() const
116{
117    String itemText = document()->displayStringModifiedByEncoding(title());
118
119    // In WinIE, leading and trailing whitespace is ignored in options and optgroups. We match this behavior.
120    itemText = itemText.stripWhiteSpace();
121    // We want to collapse our whitespace too.  This will match other browsers.
122    itemText = itemText.simplifyWhiteSpace();
123
124    return itemText;
125}
126
127void WMLOptGroupElement::recalcSelectOptions()
128{
129    if (WMLSelectElement* select = ownerSelectElement(this))
130        select->setRecalcListItems();
131}
132
133}
134
135#endif
136