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, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB.  If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 *
22 */
23
24#ifndef HTMLFormControlElement_h
25#define HTMLFormControlElement_h
26
27#include "core/html/FormAssociatedElement.h"
28#include "core/html/LabelableElement.h"
29
30namespace blink {
31
32class FormDataList;
33class HTMLFieldSetElement;
34class HTMLFormElement;
35class HTMLLegendElement;
36class ValidationMessageClient;
37class ValidityState;
38
39// HTMLFormControlElement is the default implementation of FormAssociatedElement,
40// and form-associated element implementations should use HTMLFormControlElement
41// unless there is a special reason.
42class HTMLFormControlElement : public LabelableElement, public FormAssociatedElement {
43    WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(HTMLFormControlElement);
44
45public:
46    virtual ~HTMLFormControlElement();
47    virtual void trace(Visitor*) OVERRIDE;
48
49    String formEnctype() const;
50    void setFormEnctype(const AtomicString&);
51    String formMethod() const;
52    void setFormMethod(const AtomicString&);
53    bool formNoValidate() const;
54
55    void ancestorDisabledStateWasChanged();
56
57    void reset();
58
59    bool wasChangedSinceLastFormControlChangeEvent() const { return m_wasChangedSinceLastFormControlChangeEvent; }
60    void setChangedSinceLastFormControlChangeEvent(bool);
61
62    virtual void dispatchFormControlChangeEvent();
63    void dispatchChangeEvent();
64    void dispatchFormControlInputEvent();
65
66    virtual HTMLFormElement* formOwner() const OVERRIDE FINAL;
67
68    virtual bool isDisabledFormControl() const OVERRIDE;
69
70    virtual bool isEnumeratable() const OVERRIDE { return false; }
71
72    bool isRequired() const;
73
74    const AtomicString& type() const { return formControlType(); }
75
76    virtual const AtomicString& formControlType() const = 0;
77
78    virtual bool canTriggerImplicitSubmission() const { return false; }
79
80    // Override in derived classes to get the encoded name=value pair for submitting.
81    // Return true for a successful control (see HTML4-17.13.2).
82    virtual bool appendFormData(FormDataList&, bool) OVERRIDE { return false; }
83    virtual String resultForDialogSubmit();
84
85    virtual bool canBeSuccessfulSubmitButton() const { return false; }
86    bool isSuccessfulSubmitButton() const;
87    virtual bool isActivatedSubmit() const { return false; }
88    virtual void setActivatedSubmit(bool) { }
89
90    virtual bool willValidate() const OVERRIDE;
91    void updateVisibleValidationMessage();
92    void hideVisibleValidationMessage();
93    bool checkValidity(WillBeHeapVector<RefPtrWillBeMember<FormAssociatedElement> >* unhandledInvalidControls = 0);
94    // This must be called when a validation constraint or control value is changed.
95    void setNeedsValidityCheck();
96    virtual void setCustomValidity(const String&) OVERRIDE FINAL;
97    void findCustomValidationMessageTextDirection(const String& message, TextDirection &messageDir, String& subMessage, TextDirection& subMessageDir);
98
99    bool isReadOnly() const { return m_isReadOnly; }
100    bool isDisabledOrReadOnly() const { return isDisabledFormControl() || m_isReadOnly; }
101
102    bool isAutofocusable() const;
103
104    bool isAutofilled() const { return m_isAutofilled; }
105    void setAutofilled(bool = true);
106
107    static HTMLFormControlElement* enclosingFormControlElement(Node*);
108
109    String nameForAutofill() const;
110
111    virtual void setFocus(bool flag) OVERRIDE;
112
113#if !ENABLE(OILPAN)
114    using Node::ref;
115    using Node::deref;
116#endif
117
118protected:
119    HTMLFormControlElement(const QualifiedName& tagName, Document&, HTMLFormElement*);
120
121    virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE;
122    virtual void requiredAttributeChanged();
123    virtual void disabledAttributeChanged();
124    virtual void attach(const AttachContext& = AttachContext()) OVERRIDE;
125    virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
126    virtual void removedFrom(ContainerNode*) OVERRIDE;
127    virtual void didMoveToNewDocument(Document& oldDocument) OVERRIDE;
128
129    virtual bool supportsFocus() const OVERRIDE;
130    virtual bool isKeyboardFocusable() const OVERRIDE;
131    virtual bool shouldShowFocusRingOnMouseFocus() const;
132    virtual bool shouldHaveFocusAppearance() const OVERRIDE FINAL;
133    virtual void dispatchBlurEvent(Element* newFocusedElement) OVERRIDE;
134    virtual void dispatchFocusEvent(Element* oldFocusedElement, FocusType) OVERRIDE;
135    virtual void willCallDefaultEventHandler(const Event&) OVERRIDE FINAL;
136
137    virtual void didRecalcStyle(StyleRecalcChange) OVERRIDE FINAL;
138
139    // This must be called any time the result of willValidate() has changed.
140    void setNeedsWillValidateCheck();
141    virtual bool recalcWillValidate() const;
142
143    virtual void resetImpl() { }
144    virtual bool supportsAutofocus() const;
145
146private:
147#if !ENABLE(OILPAN)
148    virtual void refFormAssociatedElement() OVERRIDE FINAL { ref(); }
149    virtual void derefFormAssociatedElement() OVERRIDE FINAL { deref(); }
150#endif
151
152    virtual bool isFormControlElement() const OVERRIDE FINAL { return true; }
153    virtual bool alwaysCreateUserAgentShadowRoot() const OVERRIDE { return true; }
154
155    virtual short tabIndex() const OVERRIDE FINAL;
156
157    virtual bool isDefaultButtonForForm() const OVERRIDE FINAL;
158    virtual bool isValidFormControlElement() OVERRIDE FINAL;
159    void updateAncestorDisabledState() const;
160
161    bool isValidationMessageVisible() const;
162    ValidationMessageClient* validationMessageClient() const;
163
164    bool m_disabled : 1;
165    bool m_isAutofilled : 1;
166    bool m_isReadOnly : 1;
167    bool m_isRequired : 1;
168    bool m_hasValidationMessage : 1;
169
170    enum AncestorDisabledState { AncestorDisabledStateUnknown, AncestorDisabledStateEnabled, AncestorDisabledStateDisabled };
171    mutable AncestorDisabledState m_ancestorDisabledState;
172    enum DataListAncestorState { Unknown, InsideDataList, NotInsideDataList };
173    mutable enum DataListAncestorState m_dataListAncestorState;
174
175    // The initial value of m_willValidate depends on the derived class. We can't
176    // initialize it with a virtual function in the constructor. m_willValidate
177    // is not deterministic as long as m_willValidateInitialized is false.
178    mutable bool m_willValidateInitialized: 1;
179    mutable bool m_willValidate : 1;
180
181    // Cache of valid().
182    // But "candidate for constraint validation" doesn't affect m_isValid.
183    bool m_isValid : 1;
184
185    bool m_wasChangedSinceLastFormControlChangeEvent : 1;
186    bool m_wasFocusedByMouse : 1;
187};
188
189inline bool isHTMLFormControlElement(const Element& element)
190{
191    return element.isFormControlElement();
192}
193
194DEFINE_HTMLELEMENT_TYPE_CASTS_WITH_FUNCTION(HTMLFormControlElement);
195DEFINE_TYPE_CASTS(HTMLFormControlElement, FormAssociatedElement, control, control->isFormControlElement(), control.isFormControlElement());
196
197} // namespace
198
199#endif
200