1 /* 2 * Copyright (C) 1999 Lars Knoll (knoll (at) kde.org) 3 * (C) 1999 Antti Koivisto (koivisto (at) kde.org) 4 * (C) 2000 Dirk Mueller (mueller (at) 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 30 namespace WebCore { 31 32 class FormDataList; 33 class HTMLFieldSetElement; 34 class HTMLFormElement; 35 class HTMLLegendElement; 36 class ValidationMessage; 37 class 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. 42 class HTMLFormControlElement : public LabelableElement, public FormAssociatedElement { 43 public: 44 virtual ~HTMLFormControlElement(); 45 46 HTMLFormElement* form() const { return FormAssociatedElement::form(); } 47 48 String formEnctype() const; 49 void setFormEnctype(const String&); 50 String formMethod() const; 51 void setFormMethod(const String&); 52 bool formNoValidate() const; 53 54 void ancestorDisabledStateWasChanged(); 55 56 virtual void reset() { } 57 58 virtual bool formControlValueMatchesRenderer() const { return m_valueMatchesRenderer; } 59 virtual void setFormControlValueMatchesRenderer(bool b) { m_valueMatchesRenderer = b; } 60 61 virtual bool wasChangedSinceLastFormControlChangeEvent() const; 62 virtual void setChangedSinceLastFormControlChangeEvent(bool); 63 64 virtual void dispatchFormControlChangeEvent(); 65 virtual void dispatchFormControlInputEvent(); 66 67 virtual bool isDisabledFormControl() const OVERRIDE; 68 69 virtual bool isEnumeratable() const { return false; } 70 71 bool isRequired() const; 72 73 const AtomicString& type() const { return formControlType(); } 74 75 virtual const AtomicString& formControlType() const OVERRIDE = 0; 76 77 virtual bool canTriggerImplicitSubmission() const { return false; } 78 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 83 virtual bool isSuccessfulSubmitButton() const { return false; } 84 virtual bool isActivatedSubmit() const { return false; } 85 virtual void setActivatedSubmit(bool) { } 86 87 enum CheckValidityDispatchEvents { CheckValidityDispatchEventsAllowed, CheckValidityDispatchEventsNone }; 88 89 virtual bool willValidate() const; 90 void updateVisibleValidationMessage(); 91 void hideVisibleValidationMessage(); 92 bool checkValidity(Vector<RefPtr<FormAssociatedElement> >* unhandledInvalidControls = 0, CheckValidityDispatchEvents = CheckValidityDispatchEventsAllowed); 93 // This must be called when a validation constraint or control value is changed. 94 void setNeedsValidityCheck(); 95 virtual void setCustomValidity(const String&) OVERRIDE; 96 97 bool isReadOnly() const { return m_isReadOnly; } 98 bool isDisabledOrReadOnly() const { return isDisabledFormControl() || m_isReadOnly; } 99 100 bool hasAutofocused() { return m_hasAutofocused; } 101 void setAutofocused() { m_hasAutofocused = true; } 102 103 static HTMLFormControlElement* enclosingFormControlElement(Node*); 104 105 using Node::ref; 106 using Node::deref; 107 108 protected: 109 HTMLFormControlElement(const QualifiedName& tagName, Document*, HTMLFormElement*); 110 111 virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE; 112 virtual void requiredAttributeChanged(); 113 virtual void disabledAttributeChanged(); 114 virtual void attach(const AttachContext& = AttachContext()) OVERRIDE; 115 virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE; 116 virtual void removedFrom(ContainerNode*) OVERRIDE; 117 virtual void didMoveToNewDocument(Document* oldDocument) OVERRIDE; 118 119 virtual bool supportsFocus() const OVERRIDE; 120 virtual bool rendererIsFocusable() const OVERRIDE; 121 virtual bool isKeyboardFocusable() const OVERRIDE; 122 virtual bool shouldShowFocusRingOnMouseFocus() const; 123 virtual bool shouldHaveFocusAppearance() const OVERRIDE; 124 virtual void dispatchFocusEvent(Element* oldFocusedElement, FocusDirection) OVERRIDE; 125 virtual void dispatchBlurEvent(Element* newFocusedElement) OVERRIDE; 126 virtual void willCallDefaultEventHandler(const Event&) OVERRIDE; 127 128 virtual void didRecalcStyle(StyleChange) OVERRIDE; 129 130 // This must be called any time the result of willValidate() has changed. 131 void setNeedsWillValidateCheck(); 132 virtual bool recalcWillValidate() const; 133 134 private: 135 virtual void refFormAssociatedElement() { ref(); } 136 virtual void derefFormAssociatedElement() { deref(); } 137 138 virtual bool isFormControlElement() const { return true; } 139 virtual bool alwaysCreateUserAgentShadowRoot() const OVERRIDE { return true; } 140 141 virtual short tabIndex() const; 142 143 virtual HTMLFormElement* virtualForm() const; 144 virtual bool isDefaultButtonForForm() const; 145 virtual bool isValidFormControlElement(); 146 void updateAncestorDisabledState() const; 147 148 OwnPtr<ValidationMessage> m_validationMessage; 149 bool m_disabled : 1; 150 bool m_isReadOnly : 1; 151 bool m_isRequired : 1; 152 bool m_valueMatchesRenderer : 1; 153 154 enum AncestorDisabledState { AncestorDisabledStateUnknown, AncestorDisabledStateEnabled, AncestorDisabledStateDisabled }; 155 mutable AncestorDisabledState m_ancestorDisabledState; 156 enum DataListAncestorState { Unknown, InsideDataList, NotInsideDataList }; 157 mutable enum DataListAncestorState m_dataListAncestorState; 158 159 // The initial value of m_willValidate depends on the derived class. We can't 160 // initialize it with a virtual function in the constructor. m_willValidate 161 // is not deterministic as long as m_willValidateInitialized is false. 162 mutable bool m_willValidateInitialized: 1; 163 mutable bool m_willValidate : 1; 164 165 // Cache of validity()->valid(). 166 // But "candidate for constraint validation" doesn't affect m_isValid. 167 bool m_isValid : 1; 168 169 bool m_wasChangedSinceLastFormControlChangeEvent : 1; 170 bool m_wasFocusedByMouse : 1; 171 bool m_hasAutofocused : 1; 172 }; 173 174 inline HTMLFormControlElement* toHTMLFormControlElement(Node* node) 175 { 176 ASSERT_WITH_SECURITY_IMPLICATION(!node || (node->isElementNode() && toElement(node)->isFormControlElement())); 177 return static_cast<HTMLFormControlElement*>(node); 178 } 179 180 inline HTMLFormControlElement* toHTMLFormControlElement(FormAssociatedElement* control) 181 { 182 ASSERT_WITH_SECURITY_IMPLICATION(!control || control->isFormControlElement()); 183 return static_cast<HTMLFormControlElement*>(control); 184 } 185 186 } // namespace 187 188 #endif 189