Home | History | Annotate | Download | only in html
      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 HTMLFormElement_h
     25 #define HTMLFormElement_h
     26 
     27 #include "core/dom/CheckedRadioButtons.h"
     28 #include "core/html/HTMLElement.h"
     29 #include "core/html/HTMLFormControlElement.h"
     30 #include "core/loader/FormState.h"
     31 #include "core/loader/FormSubmission.h"
     32 #include "wtf/OwnPtr.h"
     33 
     34 namespace WTF{
     35 class TextEncoding;
     36 }
     37 
     38 namespace WebCore {
     39 
     40 class Event;
     41 class FormAssociatedElement;
     42 class FormData;
     43 class HTMLFormControlElement;
     44 class HTMLImageElement;
     45 class HTMLInputElement;
     46 
     47 class HTMLFormElement FINAL : public HTMLElement {
     48 public:
     49     static PassRefPtr<HTMLFormElement> create(Document*);
     50     static PassRefPtr<HTMLFormElement> create(const QualifiedName&, Document*);
     51     virtual ~HTMLFormElement();
     52 
     53     PassRefPtr<HTMLCollection> elements();
     54     void getNamedElements(const AtomicString&, Vector<RefPtr<Node> >&);
     55 
     56     unsigned length() const;
     57     Node* item(unsigned index);
     58 
     59     String enctype() const { return m_attributes.encodingType(); }
     60     void setEnctype(const String&);
     61 
     62     String encoding() const { return m_attributes.encodingType(); }
     63     void setEncoding(const String& value) { setEnctype(value); }
     64 
     65     bool shouldAutocomplete() const;
     66 
     67     // FIXME: Should rename these two functions to say "form control" or "form-associated element" instead of "form element".
     68     void registerFormElement(FormAssociatedElement*);
     69     void removeFormElement(FormAssociatedElement*);
     70 
     71     void registerImgElement(HTMLImageElement*);
     72     void removeImgElement(HTMLImageElement*);
     73 
     74     bool prepareForSubmission(Event*);
     75     void submit();
     76     void submitFromJavaScript();
     77     void reset();
     78 
     79     void setDemoted(bool);
     80 
     81     void submitImplicitly(Event*, bool fromImplicitSubmissionTrigger);
     82     bool formWouldHaveSecureSubmission(const String& url);
     83 
     84     String name() const;
     85 
     86     bool noValidate() const;
     87 
     88     String acceptCharset() const { return m_attributes.acceptCharset(); }
     89     void setAcceptCharset(const String&);
     90 
     91     String action() const;
     92     void setAction(const String&);
     93 
     94     String method() const;
     95     void setMethod(const String&);
     96 
     97     virtual String target() const;
     98 
     99     bool wasUserSubmitted() const;
    100 
    101     HTMLFormControlElement* defaultButton() const;
    102 
    103     bool checkValidity();
    104     bool checkValidityWithoutDispatchingEvents();
    105 
    106     enum AutocompleteResult {
    107         AutocompleteResultSuccess,
    108         AutocompleteResultErrorDisabled,
    109         AutocompleteResultErrorCancel,
    110         AutocompleteResultErrorInvalid,
    111     };
    112 
    113     void requestAutocomplete();
    114     void finishRequestAutocomplete(AutocompleteResult);
    115 
    116     DEFINE_ATTRIBUTE_EVENT_LISTENER(autocomplete);
    117     DEFINE_ATTRIBUTE_EVENT_LISTENER(autocompleteerror);
    118 
    119     Node* elementForAlias(const AtomicString&);
    120     void addElementAlias(Node*, const AtomicString& alias);
    121 
    122     CheckedRadioButtons& checkedRadioButtons() { return m_checkedRadioButtons; }
    123 
    124     const Vector<FormAssociatedElement*>& associatedElements() const { return m_associatedElements; }
    125     const Vector<HTMLImageElement*>& imageElements() const { return m_imageElements; }
    126 
    127     void getTextFieldValues(StringPairVector& fieldNamesAndValues) const;
    128     void anonymousNamedGetter(const AtomicString& name, bool&, RefPtr<NodeList>&, bool&, RefPtr<Node>&);
    129 
    130 private:
    131     HTMLFormElement(const QualifiedName&, Document*);
    132 
    133     virtual bool rendererIsNeeded(const NodeRenderingContext&);
    134     virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
    135     virtual void removedFrom(ContainerNode*) OVERRIDE;
    136     virtual void finishParsingChildren() OVERRIDE;
    137 
    138     virtual void handleLocalEvents(Event*);
    139 
    140     virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE;
    141     virtual bool isURLAttribute(const Attribute&) const OVERRIDE;
    142 
    143     virtual bool shouldRegisterAsNamedItem() const OVERRIDE { return true; }
    144 
    145     virtual void copyNonAttributePropertiesFromElement(const Element&) OVERRIDE;
    146 
    147     void submit(Event*, bool activateSubmitButton, bool processingUserGesture, FormSubmissionTrigger);
    148 
    149     unsigned formElementIndexWithFormAttribute(Element*, unsigned rangeStart, unsigned rangeEnd);
    150     unsigned formElementIndex(FormAssociatedElement*);
    151 
    152     // Returns true if the submission should proceed.
    153     bool validateInteractively(Event*);
    154 
    155     // Validates each of the controls, and stores controls of which 'invalid'
    156     // event was not canceled to the specified vector. Returns true if there
    157     // are any invalid controls in this form.
    158     bool checkInvalidControlsAndCollectUnhandled(Vector<RefPtr<FormAssociatedElement> >*, HTMLFormControlElement::CheckValidityDispatchEvents = HTMLFormControlElement::CheckValidityDispatchEventsAllowed);
    159 
    160     typedef HashMap<AtomicString, RefPtr<Node> > AliasMap;
    161 
    162     FormSubmission::Attributes m_attributes;
    163     OwnPtr<AliasMap> m_elementAliases;
    164 
    165     CheckedRadioButtons m_checkedRadioButtons;
    166 
    167     unsigned m_associatedElementsBeforeIndex;
    168     unsigned m_associatedElementsAfterIndex;
    169     Vector<FormAssociatedElement*> m_associatedElements;
    170     Vector<HTMLImageElement*> m_imageElements;
    171 
    172     bool m_wasUserSubmitted;
    173     bool m_isSubmittingOrPreparingForSubmission;
    174     bool m_shouldSubmit;
    175 
    176     bool m_isInResetFunction;
    177 
    178     bool m_wasDemoted;
    179 
    180     void requestAutocompleteTimerFired(Timer<HTMLFormElement>*);
    181 
    182     Vector<RefPtr<Event> > m_pendingAutocompleteEvents;
    183     Timer<HTMLFormElement> m_requestAutocompleteTimer;
    184 };
    185 
    186 inline HTMLFormElement* toHTMLFormElement(Node* node)
    187 {
    188     ASSERT_WITH_SECURITY_IMPLICATION(!node || node->hasTagName(HTMLNames::formTag));
    189     return static_cast<HTMLFormElement*>(node);
    190 }
    191 
    192 } // namespace WebCore
    193 
    194 #endif // HTMLFormElement_h
    195