1/*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4 *           (C) 2000 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2004, 2005, 2006, 2010, 2011 Apple Inc. All rights reserved.
6 * Copyright (C) 2010 Google Inc. All rights reserved.
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#ifndef HTMLOptionElement_h
26#define HTMLOptionElement_h
27
28#include "core/html/HTMLElement.h"
29
30namespace WebCore {
31
32class ExceptionState;
33class HTMLDataListElement;
34class HTMLSelectElement;
35
36class HTMLOptionElement FINAL : public HTMLElement {
37public:
38    static PassRefPtr<HTMLOptionElement> create(Document*);
39    static PassRefPtr<HTMLOptionElement> create(const QualifiedName&, Document*);
40    static PassRefPtr<HTMLOptionElement> createForJSConstructor(Document*, const String& data, const String& value,
41        bool defaultSelected, bool selected, ExceptionState&);
42
43    virtual String text() const;
44    void setText(const String&, ExceptionState&);
45
46    int index() const;
47
48    String value() const;
49    void setValue(const String&);
50
51    bool selected();
52    void setSelected(bool);
53
54    HTMLDataListElement* ownerDataListElement() const;
55    HTMLSelectElement* ownerSelectElement() const;
56
57    String label() const;
58    void setLabel(const String&);
59
60    bool ownElementDisabled() const { return m_disabled; }
61
62    virtual bool isDisabledFormControl() const OVERRIDE;
63
64    String textIndentedToRespectGroupLabel() const;
65
66    void setSelectedState(bool);
67
68private:
69    HTMLOptionElement(const QualifiedName&, Document*);
70
71    virtual bool rendererIsFocusable() const OVERRIDE;
72    virtual bool rendererIsNeeded(const NodeRenderingContext&) { return false; }
73    virtual void attach(const AttachContext& = AttachContext()) OVERRIDE;
74    virtual void detach(const AttachContext& = AttachContext()) OVERRIDE;
75
76    virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE;
77
78    virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
79    virtual void accessKeyAction(bool);
80
81    virtual void childrenChanged(bool changedByParser = false, Node* beforeChange = 0, Node* afterChange = 0, int childCountDelta = 0);
82
83    // <option> never has a renderer so we manually manage a cached style.
84    void updateNonRenderStyle();
85    virtual RenderStyle* nonRendererStyle() const OVERRIDE;
86    virtual PassRefPtr<RenderStyle> customStyleForRenderer() OVERRIDE;
87
88    void didRecalcStyle(StyleChange) OVERRIDE;
89
90    String collectOptionInnerText() const;
91
92    bool m_disabled;
93    bool m_isSelected;
94    RefPtr<RenderStyle> m_style;
95};
96
97void toHTMLOptionElement(const HTMLOptionElement*); // This overload will catch anyone doing an unnecessary cast.
98
99inline HTMLOptionElement* toHTMLOptionElement(Node* node)
100{
101    ASSERT_WITH_SECURITY_IMPLICATION(!node || node->hasTagName(HTMLNames::optionTag));
102    return static_cast<HTMLOptionElement*>(node);
103}
104
105inline const HTMLOptionElement* toHTMLOptionElement(const Node* node)
106{
107    ASSERT_WITH_SECURITY_IMPLICATION(!node || node->hasTagName(HTMLNames::optionTag));
108    return static_cast<const HTMLOptionElement*>(node);
109}
110
111} // namespace WebCore
112
113#endif
114