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.
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.
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.
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.
24 #ifndef HTMLFormControlElement_h
25 #define HTMLFormControlElement_h
27 #include "core/html/FormAssociatedElement.h"
28 #include "core/html/LabelableElement.h"
33 class HTMLFieldSetElement;
34 class HTMLFormElement;
35 class HTMLLegendElement;
36 class ValidationMessage;
39 // HTMLFormControlElement is the default implementation of FormAssociatedElement,
40 // and form-associated element implementations should use HTMLFormControlElement
41 // unless there is a special reason.
42 class HTMLFormControlElement : public LabelableElement, public FormAssociatedElement {
44 virtual ~HTMLFormControlElement();
46 HTMLFormElement* form() const { return FormAssociatedElement::form(); }
48 String formEnctype() const;
49 void setFormEnctype(const String&);
50 String formMethod() const;
51 void setFormMethod(const String&);
52 bool formNoValidate() const;
54 void ancestorDisabledStateWasChanged();
58 virtual bool formControlValueMatchesRenderer() const { return m_valueMatchesRenderer; }
59 virtual void setFormControlValueMatchesRenderer(bool b) { m_valueMatchesRenderer = b; }
61 virtual bool wasChangedSinceLastFormControlChangeEvent() const;
62 virtual void setChangedSinceLastFormControlChangeEvent(bool);
64 virtual void dispatchFormControlChangeEvent();
65 virtual void dispatchFormControlInputEvent();
67 virtual bool isDisabledFormControl() const OVERRIDE;
69 virtual bool isEnumeratable() const { return false; }
71 bool isRequired() const;
73 const AtomicString& type() const { return formControlType(); }
75 virtual const AtomicString& formControlType() const OVERRIDE = 0;
77 virtual bool canTriggerImplicitSubmission() const { return false; }
79 // Override in derived classes to get the encoded name=value pair for submitting.
80 // Return true for a successful control (see HTML4-17.13.2).
81 virtual bool appendFormData(FormDataList&, bool) { return false; }
82 virtual String resultForDialogSubmit();
84 virtual bool isSuccessfulSubmitButton() const { return false; }
85 virtual bool isActivatedSubmit() const { return false; }
86 virtual void setActivatedSubmit(bool) { }
88 enum CheckValidityDispatchEvents { CheckValidityDispatchEventsAllowed, CheckValidityDispatchEventsNone };
90 virtual bool willValidate() const;
91 void updateVisibleValidationMessage();
92 void hideVisibleValidationMessage();
93 bool checkValidity(Vector<RefPtr<FormAssociatedElement> >* unhandledInvalidControls = 0, CheckValidityDispatchEvents = CheckValidityDispatchEventsAllowed);
94 // This must be called when a validation constraint or control value is changed.
95 void setNeedsValidityCheck();
96 virtual void setCustomValidity(const String&) OVERRIDE;
98 bool isReadOnly() const { return m_isReadOnly; }
99 bool isDisabledOrReadOnly() const { return isDisabledFormControl() || m_isReadOnly; }
101 bool hasAutofocused() const { return m_hasAutofocused; }
102 void setAutofocused() { m_hasAutofocused = true; }
103 bool isAutofocusable() const;
105 bool isAutofilled() const { return m_isAutofilled; }
106 void setAutofilled(bool = true);
108 static HTMLFormControlElement* enclosingFormControlElement(Node*);
114 HTMLFormControlElement(const QualifiedName& tagName, Document&, HTMLFormElement*);
116 virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE;
117 virtual void requiredAttributeChanged();
118 virtual void disabledAttributeChanged();
119 virtual void attach(const AttachContext& = AttachContext()) OVERRIDE;
120 virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
121 virtual void removedFrom(ContainerNode*) OVERRIDE;
122 virtual void didMoveToNewDocument(Document& oldDocument) OVERRIDE;
124 virtual bool supportsFocus() const OVERRIDE;
125 virtual bool isKeyboardFocusable() const OVERRIDE;
126 virtual bool shouldShowFocusRingOnMouseFocus() const;
127 virtual bool shouldHaveFocusAppearance() const OVERRIDE;
128 virtual void dispatchFocusEvent(Element* oldFocusedElement, FocusDirection) OVERRIDE;
129 virtual void dispatchBlurEvent(Element* newFocusedElement) OVERRIDE;
130 virtual void willCallDefaultEventHandler(const Event&) OVERRIDE;
132 virtual void didRecalcStyle(StyleRecalcChange) OVERRIDE;
134 // This must be called any time the result of willValidate() has changed.
135 void setNeedsWillValidateCheck();
136 virtual bool recalcWillValidate() const;
138 virtual void resetImpl() { }
141 virtual void refFormAssociatedElement() { ref(); }
142 virtual void derefFormAssociatedElement() { deref(); }
144 virtual bool isFormControlElement() const { return true; }
145 virtual bool alwaysCreateUserAgentShadowRoot() const OVERRIDE { return true; }
147 virtual short tabIndex() const;
149 virtual HTMLFormElement* virtualForm() const;
150 virtual bool isDefaultButtonForForm() const;
151 virtual bool isValidFormControlElement();
152 void updateAncestorDisabledState() const;
154 OwnPtr<ValidationMessage> m_validationMessage;
156 bool m_isAutofilled : 1;
157 bool m_isReadOnly : 1;
158 bool m_isRequired : 1;
159 bool m_valueMatchesRenderer : 1;
161 enum AncestorDisabledState { AncestorDisabledStateUnknown, AncestorDisabledStateEnabled, AncestorDisabledStateDisabled };
162 mutable AncestorDisabledState m_ancestorDisabledState;
163 enum DataListAncestorState { Unknown, InsideDataList, NotInsideDataList };
164 mutable enum DataListAncestorState m_dataListAncestorState;
166 // The initial value of m_willValidate depends on the derived class. We can't
167 // initialize it with a virtual function in the constructor. m_willValidate
168 // is not deterministic as long as m_willValidateInitialized is false.
169 mutable bool m_willValidateInitialized: 1;
170 mutable bool m_willValidate : 1;
173 // But "candidate for constraint validation" doesn't affect m_isValid.
176 bool m_wasChangedSinceLastFormControlChangeEvent : 1;
177 bool m_wasFocusedByMouse : 1;
178 bool m_hasAutofocused : 1;
181 inline bool isHTMLFormControlElement(const Node& node)
183 return node.isElementNode() && toElement(node).isFormControlElement();
186 DEFINE_NODE_TYPE_CASTS_WITH_FUNCTION(HTMLFormControlElement);
188 inline HTMLFormControlElement* toHTMLFormControlElement(FormAssociatedElement* control)
190 ASSERT_WITH_SECURITY_IMPLICATION(!control || control->isFormControlElement());
191 return static_cast<HTMLFormControlElement*>(control);
194 inline HTMLFormControlElement& toHTMLFormControlElement(FormAssociatedElement& control)
196 ASSERT_WITH_SECURITY_IMPLICATION(control.isFormControlElement());
197 return static_cast<HTMLFormControlElement&>(control);
200 inline const HTMLFormControlElement* toHTMLFormControlElement(const FormAssociatedElement* control)
202 ASSERT_WITH_SECURITY_IMPLICATION(!control || control->isFormControlElement());
203 return static_cast<const HTMLFormControlElement*>(control);