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 FormAssociatedElement_h
25#define FormAssociatedElement_h
26
27#include "FormNamedItem.h"
28#include <wtf/text/WTFString.h>
29
30namespace WebCore {
31
32class ContainerNode;
33class Document;
34class FormAttributeTargetObserver;
35class FormDataList;
36class HTMLElement;
37class HTMLFormElement;
38class Node;
39class ValidityState;
40
41class FormAssociatedElement : public FormNamedItem {
42public:
43    virtual ~FormAssociatedElement();
44
45    void ref() { refFormAssociatedElement(); }
46    void deref() { derefFormAssociatedElement(); }
47
48    static HTMLFormElement* findAssociatedForm(const HTMLElement*, HTMLFormElement*);
49    HTMLFormElement* form() const { return m_form; }
50    ValidityState* validity();
51
52    virtual bool isFormControlElement() const = 0;
53    virtual bool isFormControlElementWithState() const;
54    virtual bool isEnumeratable() const = 0;
55
56    // Returns the 'name' attribute value. If this element has no name
57    // attribute, it returns an empty string instead of null string.
58    // Note that the 'name' IDL attribute doesn't use this function.
59    virtual const AtomicString& name() const;
60
61    // Override in derived classes to get the encoded name=value pair for submitting.
62    // Return true for a successful control (see HTML4-17.13.2).
63    virtual bool appendFormData(FormDataList&, bool) { return false; }
64
65    void formWillBeDestroyed();
66
67    void resetFormOwner();
68
69    void formRemovedFromTree(const Node* formRoot);
70
71    // ValidityState attribute implementations
72    bool badInput() const { return hasBadInput(); }
73    bool customError() const;
74
75    // Implementations of patternMismatch, rangeOverflow, rangerUnderflow, stepMismatch, tooLong and valueMissing must call willValidate.
76    virtual bool hasBadInput() const;
77    virtual bool patternMismatch() const;
78    virtual bool rangeOverflow() const;
79    virtual bool rangeUnderflow() const;
80    virtual bool stepMismatch() const;
81    virtual bool tooLong() const;
82    virtual bool typeMismatch() const;
83    virtual bool valueMissing() const;
84    virtual String validationMessage() const;
85    bool valid() const;
86    virtual void setCustomValidity(const String&);
87
88    void formAttributeTargetChanged();
89
90protected:
91    FormAssociatedElement();
92
93    void insertedInto(ContainerNode&);
94    void removedFrom(ContainerNode&);
95    void didMoveToNewDocument(Document* oldDocument);
96
97    void setForm(HTMLFormElement*);
98    void formAttributeChanged();
99
100    // If you add an override of willChangeForm() or didChangeForm() to a class
101    // derived from this one, you will need to add a call to setForm(0) to the
102    // destructor of that class.
103    virtual void willChangeForm();
104    virtual void didChangeForm();
105
106    String customValidationMessage() const;
107
108private:
109    virtual void refFormAssociatedElement() = 0;
110    virtual void derefFormAssociatedElement() = 0;
111
112    void resetFormAttributeTargetObserver();
113
114    virtual bool isFormAssociatedElement() const override final { return true; }
115
116    std::unique_ptr<FormAttributeTargetObserver> m_formAttributeTargetObserver;
117    HTMLFormElement* m_form;
118    String m_customValidationMessage;
119};
120
121#define FORM_ASSOCIATED_ELEMENT_TYPE_CASTS(ToClassName, predicate) \
122    TYPE_CASTS_BASE(ToClassName, FormAssociatedElement, element, element->predicate, element.predicate)
123
124} // namespace
125
126#endif // FormAssociatedElement_h
127