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  * Copyright (C) 2009, 2010, 2011 Google Inc. All rights reserved.
      7  *
      8  * This library is free software; you can redistribute it and/or
      9  * modify it under the terms of the GNU Library General Public
     10  * License as published by the Free Software Foundation; either
     11  * version 2 of the License, or (at your option) any later version.
     12  *
     13  * This library is distributed in the hope that it will be useful,
     14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     16  * Library General Public License for more details.
     17  *
     18  * You should have received a copy of the GNU Library General Public License
     19  * along with this library; see the file COPYING.LIB.  If not, write to
     20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     21  * Boston, MA 02110-1301, USA.
     22  *
     23  */
     24 
     25 #ifndef HTMLTextFormControlElement_h
     26 #define HTMLTextFormControlElement_h
     27 
     28 #include "core/html/HTMLFormControlElementWithState.h"
     29 
     30 namespace WebCore {
     31 
     32 class ExceptionState;
     33 class Position;
     34 class RenderTextControl;
     35 class VisiblePosition;
     36 
     37 enum TextFieldSelectionDirection { SelectionHasNoDirection, SelectionHasForwardDirection, SelectionHasBackwardDirection };
     38 enum TextFieldEventBehavior { DispatchNoEvent, DispatchChangeEvent, DispatchInputAndChangeEvent };
     39 
     40 class HTMLTextFormControlElement : public HTMLFormControlElementWithState {
     41 public:
     42     // Common flag for HTMLInputElement::tooLong() and HTMLTextAreaElement::tooLong().
     43     enum NeedsToCheckDirtyFlag {CheckDirtyFlag, IgnoreDirtyFlag};
     44 
     45     virtual ~HTMLTextFormControlElement();
     46 
     47     void forwardEvent(Event*);
     48 
     49 
     50     virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
     51 
     52     // The derived class should return true if placeholder processing is needed.
     53     virtual bool supportsPlaceholder() const = 0;
     54     String strippedPlaceholder() const;
     55     bool placeholderShouldBeVisible() const;
     56     virtual HTMLElement* placeholderElement() const = 0;
     57     void updatePlaceholderVisibility(bool);
     58     static void fixPlaceholderRenderer(HTMLElement* placeholder, HTMLElement* siblingElement);
     59 
     60     VisiblePosition visiblePositionForIndex(int) const;
     61     int indexForVisiblePosition(const VisiblePosition&) const;
     62     int selectionStart() const;
     63     int selectionEnd() const;
     64     const AtomicString& selectionDirection() const;
     65     void setSelectionStart(int);
     66     void setSelectionEnd(int);
     67     void setSelectionDirection(const String&);
     68     void select();
     69     virtual void setRangeText(const String& replacement, ExceptionState&);
     70     virtual void setRangeText(const String& replacement, unsigned start, unsigned end, const String& selectionMode, ExceptionState&);
     71     void setSelectionRange(int start, int end, const String& direction);
     72     void setSelectionRange(int start, int end, TextFieldSelectionDirection = SelectionHasNoDirection);
     73     PassRefPtr<Range> selection() const;
     74     String selectedText() const;
     75 
     76     virtual void dispatchFormControlChangeEvent();
     77 
     78     virtual int maxLength() const = 0;
     79     virtual String value() const = 0;
     80 
     81     virtual HTMLElement* innerTextElement() const = 0;
     82 
     83     void selectionChanged(bool userTriggered);
     84     bool lastChangeWasUserEdit() const;
     85     void setInnerTextValue(const String&);
     86     String innerTextValue() const;
     87 
     88     String directionForFormData() const;
     89 
     90     void setTextAsOfLastFormControlChangeEvent(const String& text) { m_textAsOfLastFormControlChangeEvent = text; }
     91 
     92 protected:
     93     HTMLTextFormControlElement(const QualifiedName&, Document*, HTMLFormElement*);
     94     bool isPlaceholderEmpty() const;
     95     virtual void updatePlaceholderText() = 0;
     96 
     97     virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE;
     98 
     99     void cacheSelection(int start, int end, TextFieldSelectionDirection direction)
    100     {
    101         m_cachedSelectionStart = start;
    102         m_cachedSelectionEnd = end;
    103         m_cachedSelectionDirection = direction;
    104     }
    105 
    106     void restoreCachedSelection();
    107     bool hasCachedSelection() const { return m_cachedSelectionStart >= 0; }
    108 
    109     virtual void defaultEventHandler(Event*);
    110     virtual void subtreeHasChanged() = 0;
    111 
    112     void setLastChangeWasNotUserEdit() { m_lastChangeWasUserEdit = false; }
    113 
    114     String valueWithHardLineBreaks() const;
    115 
    116 private:
    117     int computeSelectionStart() const;
    118     int computeSelectionEnd() const;
    119     TextFieldSelectionDirection computeSelectionDirection() const;
    120 
    121     // FIXME: Author shadows should be allowed
    122     // https://bugs.webkit.org/show_bug.cgi?id=92608
    123     virtual bool areAuthorShadowsAllowed() const OVERRIDE { return false; }
    124 
    125     virtual void dispatchFocusEvent(Element* oldFocusedElement, FocusDirection) OVERRIDE;
    126     virtual void dispatchBlurEvent(Element* newFocusedElement) OVERRIDE;
    127 
    128     // Returns true if user-editable value is empty. Used to check placeholder visibility.
    129     virtual bool isEmptyValue() const = 0;
    130     // Returns true if suggested value is empty. Used to check placeholder visibility.
    131     virtual bool isEmptySuggestedValue() const { return true; }
    132     // Called in dispatchFocusEvent(), after placeholder process, before calling parent's dispatchFocusEvent().
    133     virtual void handleFocusEvent(Element* /* oldFocusedNode */, FocusDirection) { }
    134     // Called in dispatchBlurEvent(), after placeholder process, before calling parent's dispatchBlurEvent().
    135     virtual void handleBlurEvent() { }
    136 
    137     String m_textAsOfLastFormControlChangeEvent;
    138     bool m_lastChangeWasUserEdit;
    139 
    140     int m_cachedSelectionStart;
    141     int m_cachedSelectionEnd;
    142     TextFieldSelectionDirection m_cachedSelectionDirection;
    143 };
    144 
    145 inline bool isHTMLTextFormControlElement(const Node* node)
    146 {
    147     return node->isElementNode() && toElement(node)->isTextFormControl();
    148 }
    149 
    150 inline HTMLTextFormControlElement* toHTMLTextFormControlElement(Node* node)
    151 {
    152     ASSERT_WITH_SECURITY_IMPLICATION(!node || isHTMLTextFormControlElement(node));
    153     return static_cast<HTMLTextFormControlElement*>(node);
    154 }
    155 
    156 HTMLTextFormControlElement* enclosingTextFormControl(const Position&);
    157 
    158 } // namespace
    159 
    160 #endif
    161