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 Range;
     35 class RenderTextControl;
     36 class VisiblePosition;
     37 
     38 enum TextFieldSelectionDirection { SelectionHasNoDirection, SelectionHasForwardDirection, SelectionHasBackwardDirection };
     39 enum TextFieldEventBehavior { DispatchNoEvent, DispatchChangeEvent, DispatchInputAndChangeEvent };
     40 
     41 class HTMLTextFormControlElement : public HTMLFormControlElementWithState {
     42 public:
     43     // Common flag for HTMLInputElement::tooLong() and HTMLTextAreaElement::tooLong().
     44     enum NeedsToCheckDirtyFlag {CheckDirtyFlag, IgnoreDirtyFlag};
     45 
     46     virtual ~HTMLTextFormControlElement();
     47 
     48     void forwardEvent(Event*);
     49 
     50 
     51     virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
     52 
     53     // The derived class should return true if placeholder processing is needed.
     54     virtual bool supportsPlaceholder() const = 0;
     55     String strippedPlaceholder() const;
     56     bool placeholderShouldBeVisible() const;
     57     HTMLElement* placeholderElement() const;
     58     void updatePlaceholderVisibility(bool);
     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     PassRefPtrWillBeRawPtr<Range> selection() const;
     74 
     75     virtual void dispatchFormControlChangeEvent() OVERRIDE FINAL;
     76 
     77     virtual String value() const = 0;
     78 
     79     HTMLElement* innerEditorElement() const;
     80 
     81     void selectionChanged(bool userTriggered);
     82     bool lastChangeWasUserEdit() const;
     83     virtual void setInnerEditorValue(const String&);
     84     String innerEditorValue() const;
     85 
     86     String directionForFormData() const;
     87 
     88     void setTextAsOfLastFormControlChangeEvent(const String& text) { m_textAsOfLastFormControlChangeEvent = text; }
     89 
     90 protected:
     91     HTMLTextFormControlElement(const QualifiedName&, Document&, HTMLFormElement*);
     92     bool isPlaceholderEmpty() const;
     93     virtual void updatePlaceholderText() = 0;
     94 
     95     virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE;
     96 
     97     void cacheSelection(int start, int end, TextFieldSelectionDirection direction)
     98     {
     99         ASSERT(start >= 0);
    100         m_cachedSelectionStart = start;
    101         m_cachedSelectionEnd = end;
    102         m_cachedSelectionDirection = direction;
    103     }
    104 
    105     void restoreCachedSelection();
    106 
    107     virtual void defaultEventHandler(Event*) OVERRIDE;
    108     virtual void subtreeHasChanged() = 0;
    109 
    110     void setLastChangeWasNotUserEdit() { m_lastChangeWasUserEdit = false; }
    111 
    112     String valueWithHardLineBreaks() const;
    113 
    114     virtual bool shouldDispatchFormControlChangeEvent(String&, String&);
    115 
    116 private:
    117     int computeSelectionStart() const;
    118     int computeSelectionEnd() const;
    119     TextFieldSelectionDirection computeSelectionDirection() const;
    120 
    121     virtual void dispatchFocusEvent(Element* oldFocusedElement, FocusType) OVERRIDE FINAL;
    122     virtual void dispatchBlurEvent(Element* newFocusedElement) OVERRIDE FINAL;
    123 
    124     // Returns true if user-editable value is empty. Used to check placeholder visibility.
    125     virtual bool isEmptyValue() const = 0;
    126     // Returns true if suggested value is empty. Used to check placeholder visibility.
    127     virtual bool isEmptySuggestedValue() const { return true; }
    128     // Called in dispatchFocusEvent(), after placeholder process, before calling parent's dispatchFocusEvent().
    129     virtual void handleFocusEvent(Element* /* oldFocusedNode */, FocusType) { }
    130     // Called in dispatchBlurEvent(), after placeholder process, before calling parent's dispatchBlurEvent().
    131     virtual void handleBlurEvent() { }
    132 
    133     String m_textAsOfLastFormControlChangeEvent;
    134     bool m_lastChangeWasUserEdit;
    135 
    136     int m_cachedSelectionStart;
    137     int m_cachedSelectionEnd;
    138     TextFieldSelectionDirection m_cachedSelectionDirection;
    139 };
    140 
    141 inline bool isHTMLTextFormControlElement(const Element& element)
    142 {
    143     return element.isTextFormControl();
    144 }
    145 
    146 DEFINE_HTMLELEMENT_TYPE_CASTS_WITH_FUNCTION(HTMLTextFormControlElement);
    147 
    148 HTMLTextFormControlElement* enclosingTextFormControl(const Position&);
    149 HTMLTextFormControlElement* enclosingTextFormControl(Node*);
    150 
    151 } // namespace
    152 
    153 #endif
    154