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     HTMLElement* placeholderElement() const;
     57     void updatePlaceholderVisibility(bool);
     58 
     59     VisiblePosition visiblePositionForIndex(int) const;
     60     int indexForVisiblePosition(const VisiblePosition&) const;
     61     int selectionStart() const;
     62     int selectionEnd() const;
     63     const AtomicString& selectionDirection() const;
     64     void setSelectionStart(int);
     65     void setSelectionEnd(int);
     66     void setSelectionDirection(const String&);
     67     void select();
     68     virtual void setRangeText(const String& replacement, ExceptionState&);
     69     virtual void setRangeText(const String& replacement, unsigned start, unsigned end, const String& selectionMode, ExceptionState&);
     70     void setSelectionRange(int start, int end, const String& direction);
     71     void setSelectionRange(int start, int end, TextFieldSelectionDirection = SelectionHasNoDirection);
     72     PassRefPtr<Range> selection() const;
     73     String selectedText() const;
     74 
     75     virtual void dispatchFormControlChangeEvent();
     76 
     77     virtual int maxLength() const = 0;
     78     virtual String value() const = 0;
     79 
     80     HTMLElement* innerTextElement() const;
     81 
     82     void selectionChanged(bool userTriggered);
     83     bool lastChangeWasUserEdit() const;
     84     void setInnerTextValue(const String&);
     85     String innerTextValue() const;
     86 
     87     String directionForFormData() const;
     88 
     89     void setTextAsOfLastFormControlChangeEvent(const String& text) { m_textAsOfLastFormControlChangeEvent = text; }
     90 
     91 protected:
     92     HTMLTextFormControlElement(const QualifiedName&, Document&, HTMLFormElement*);
     93     bool isPlaceholderEmpty() const;
     94     virtual void updatePlaceholderText() = 0;
     95 
     96     virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE;
     97 
     98     void cacheSelection(int start, int end, TextFieldSelectionDirection direction)
     99     {
    100         m_cachedSelectionStart = start;
    101         m_cachedSelectionEnd = end;
    102         m_cachedSelectionDirection = direction;
    103     }
    104 
    105     void restoreCachedSelection();
    106     bool hasCachedSelection() const { return m_cachedSelectionStart >= 0; }
    107 
    108     virtual void defaultEventHandler(Event*);
    109     virtual void subtreeHasChanged() = 0;
    110 
    111     void setLastChangeWasNotUserEdit() { m_lastChangeWasUserEdit = false; }
    112 
    113     String valueWithHardLineBreaks() const;
    114 
    115 private:
    116     int computeSelectionStart() const;
    117     int computeSelectionEnd() const;
    118     TextFieldSelectionDirection computeSelectionDirection() const;
    119 
    120     virtual void dispatchFocusEvent(Element* oldFocusedElement, FocusDirection) OVERRIDE;
    121     virtual void dispatchBlurEvent(Element* newFocusedElement) OVERRIDE;
    122 
    123     // Returns true if user-editable value is empty. Used to check placeholder visibility.
    124     virtual bool isEmptyValue() const = 0;
    125     // Returns true if suggested value is empty. Used to check placeholder visibility.
    126     virtual bool isEmptySuggestedValue() const { return true; }
    127     // Called in dispatchFocusEvent(), after placeholder process, before calling parent's dispatchFocusEvent().
    128     virtual void handleFocusEvent(Element* /* oldFocusedNode */, FocusDirection) { }
    129     // Called in dispatchBlurEvent(), after placeholder process, before calling parent's dispatchBlurEvent().
    130     virtual void handleBlurEvent() { }
    131 
    132     String m_textAsOfLastFormControlChangeEvent;
    133     bool m_lastChangeWasUserEdit;
    134 
    135     int m_cachedSelectionStart;
    136     int m_cachedSelectionEnd;
    137     TextFieldSelectionDirection m_cachedSelectionDirection;
    138 };
    139 
    140 inline bool isHTMLTextFormControlElement(const Node* node)
    141 {
    142     return node->isElementNode() && toElement(node)->isTextFormControl();
    143 }
    144 
    145 inline bool isHTMLTextFormControlElement(const Node& node)
    146 {
    147     return node.isElementNode() && toElement(node).isTextFormControl();
    148 }
    149 
    150 DEFINE_NODE_TYPE_CASTS_WITH_FUNCTION(HTMLTextFormControlElement);
    151 
    152 HTMLTextFormControlElement* enclosingTextFormControl(const Position&);
    153 
    154 } // namespace
    155 
    156 #endif
    157