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 * 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 "HTMLFormControlElementWithState.h"
29
30namespace WebCore {
31
32class Position;
33class RenderTextControl;
34class TextControlInnerTextElement;
35class VisiblePosition;
36
37enum TextFieldSelectionDirection { SelectionHasNoDirection, SelectionHasForwardDirection, SelectionHasBackwardDirection };
38enum TextFieldEventBehavior { DispatchNoEvent, DispatchChangeEvent, DispatchInputAndChangeEvent };
39
40class HTMLTextFormControlElement : public HTMLFormControlElementWithState {
41public:
42    // Common flag for HTMLInputElement::tooLong() and HTMLTextAreaElement::tooLong().
43    enum NeedsToCheckDirtyFlag {CheckDirtyFlag, IgnoreDirtyFlag};
44
45    virtual ~HTMLTextFormControlElement();
46
47    void didEditInnerTextValue();
48    void forwardEvent(Event*);
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
59    int indexForVisiblePosition(const VisiblePosition&) const;
60    VisiblePosition visiblePositionForIndex(int index) 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, ExceptionCode&);
69    virtual void setRangeText(const String& replacement, unsigned start, unsigned end, const String& selectionMode, ExceptionCode&);
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() override final;
76
77    virtual int maxLength() const = 0;
78    virtual String value() const = 0;
79
80    virtual TextControlInnerTextElement* innerTextElement() const = 0;
81
82    void selectionChanged(bool shouldFireSelectEvent);
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#if PLATFORM(IOS)
91    void hidePlaceholder();
92    void showPlaceholderIfNecessary();
93#endif
94
95protected:
96    HTMLTextFormControlElement(const QualifiedName&, Document&, HTMLFormElement*);
97    bool isPlaceholderEmpty() const;
98    virtual void updatePlaceholderText() = 0;
99
100    virtual void parseAttribute(const QualifiedName&, const AtomicString&) override;
101
102    virtual void disabledStateChanged() override;
103    virtual void readOnlyAttributeChanged() override;
104    void updateInnerTextElementEditability();
105
106    void cacheSelection(int start, int end, TextFieldSelectionDirection direction)
107    {
108        m_cachedSelectionStart = start;
109        m_cachedSelectionEnd = end;
110        m_cachedSelectionDirection = direction;
111    }
112
113    void restoreCachedSelection();
114    bool hasCachedSelection() const { return m_cachedSelectionStart >= 0; }
115
116    virtual void subtreeHasChanged() = 0;
117
118    void setLastChangeWasNotUserEdit() { m_lastChangeWasUserEdit = false; }
119
120    String valueWithHardLineBreaks() const;
121
122private:
123    int computeSelectionStart() const;
124    int computeSelectionEnd() const;
125    TextFieldSelectionDirection computeSelectionDirection() const;
126
127    virtual void dispatchFocusEvent(PassRefPtr<Element> oldFocusedElement, FocusDirection) override final;
128    virtual void dispatchBlurEvent(PassRefPtr<Element> newFocusedElement) override final;
129    virtual bool childShouldCreateRenderer(const Node&) const override;
130
131    unsigned indexForPosition(const Position&) const;
132
133    // Returns true if user-editable value is empty. Used to check placeholder visibility.
134    virtual bool isEmptyValue() const = 0;
135    // Returns true if suggested value is empty. Used to check placeholder visibility.
136    virtual bool isEmptySuggestedValue() const { return true; }
137    // Called in dispatchFocusEvent(), after placeholder process, before calling parent's dispatchFocusEvent().
138    virtual void handleFocusEvent(Node* /* oldFocusedNode */, FocusDirection) { }
139    // Called in dispatchBlurEvent(), after placeholder process, before calling parent's dispatchBlurEvent().
140    virtual void handleBlurEvent() { }
141
142    String m_textAsOfLastFormControlChangeEvent;
143    bool m_lastChangeWasUserEdit;
144
145    int m_cachedSelectionStart;
146    int m_cachedSelectionEnd;
147    TextFieldSelectionDirection m_cachedSelectionDirection;
148};
149
150void isHTMLTextFormControlElement(const HTMLTextFormControlElement&); // Catch unnecessary runtime check of type known at compile time.
151inline bool isHTMLTextFormControlElement(const Element& element) { return element.isTextFormControl(); }
152inline bool isHTMLTextFormControlElement(const Node& node) { return node.isElementNode() && toElement(node).isTextFormControl(); }
153NODE_TYPE_CASTS(HTMLTextFormControlElement)
154
155HTMLTextFormControlElement* enclosingTextFormControl(const Position&);
156
157} // namespace
158
159#endif
160