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 VisiblePosition;
35
36enum TextFieldSelectionDirection { SelectionHasNoDirection, SelectionHasForwardDirection, SelectionHasBackwardDirection };
37enum TextFieldEventBehavior { DispatchNoEvent, DispatchChangeEvent, DispatchInputAndChangeEvent };
38
39class HTMLTextFormControlElement : public HTMLFormControlElementWithState {
40public:
41    // Common flag for HTMLInputElement::tooLong() and HTMLTextAreaElement::tooLong().
42    enum NeedsToCheckDirtyFlag {CheckDirtyFlag, IgnoreDirtyFlag};
43
44    virtual ~HTMLTextFormControlElement();
45
46    void forwardEvent(Event*);
47
48
49    virtual InsertionNotificationRequest insertedInto(ContainerNode*) OVERRIDE;
50
51    // The derived class should return true if placeholder processing is needed.
52    virtual bool supportsPlaceholder() const = 0;
53    String strippedPlaceholder() const;
54    bool placeholderShouldBeVisible() const;
55    virtual HTMLElement* placeholderElement() const = 0;
56    void updatePlaceholderVisibility(bool);
57    static void fixPlaceholderRenderer(HTMLElement* placeholder, HTMLElement* siblingElement);
58
59    int indexForVisiblePosition(const VisiblePosition&) const;
60    int selectionStart() const;
61    int selectionEnd() const;
62    const AtomicString& selectionDirection() const;
63    void setSelectionStart(int);
64    void setSelectionEnd(int);
65    void setSelectionDirection(const String&);
66    void select();
67    virtual void setRangeText(const String& replacement, ExceptionCode&);
68    virtual void setRangeText(const String& replacement, unsigned start, unsigned end, const String& selectionMode, ExceptionCode&);
69    void setSelectionRange(int start, int end, const String& direction);
70    void setSelectionRange(int start, int end, TextFieldSelectionDirection = SelectionHasNoDirection);
71    PassRefPtr<Range> selection() const;
72    String selectedText() const;
73
74    virtual void dispatchFormControlChangeEvent() OVERRIDE FINAL;
75
76    virtual int maxLength() const = 0;
77    virtual String value() const = 0;
78
79    virtual HTMLElement* innerTextElement() const = 0;
80
81    void selectionChanged(bool userTriggered);
82    bool lastChangeWasUserEdit() const;
83    void setInnerTextValue(const String&);
84    String innerTextValue() const;
85
86    String directionForFormData() const;
87
88    void setTextAsOfLastFormControlChangeEvent(const String& text) { m_textAsOfLastFormControlChangeEvent = text; }
89
90protected:
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        m_cachedSelectionStart = start;
100        m_cachedSelectionEnd = end;
101        m_cachedSelectionDirection = direction;
102    }
103
104    void restoreCachedSelection();
105    bool hasCachedSelection() const { return m_cachedSelectionStart >= 0; }
106
107    virtual void defaultEventHandler(Event*);
108    virtual void subtreeHasChanged() = 0;
109
110    void setLastChangeWasNotUserEdit() { m_lastChangeWasUserEdit = false; }
111
112    String valueWithHardLineBreaks() const;
113
114private:
115    int computeSelectionStart() const;
116    int computeSelectionEnd() const;
117    TextFieldSelectionDirection computeSelectionDirection() const;
118
119    virtual void dispatchFocusEvent(PassRefPtr<Element> oldFocusedElement, FocusDirection) OVERRIDE FINAL;
120    virtual void dispatchBlurEvent(PassRefPtr<Element> newFocusedElement) OVERRIDE FINAL;
121    virtual bool childShouldCreateRenderer(const NodeRenderingContext&) const 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(Node* /* oldFocusedNode */, FocusDirection) { }
129    // Called in dispatchBlurEvent(), after placeholder process, before calling parent's dispatchBlurEvent().
130    virtual void handleBlurEvent() { }
131
132    RenderTextControl* textRendererAfterUpdateLayout();
133
134    String m_textAsOfLastFormControlChangeEvent;
135    bool m_lastChangeWasUserEdit;
136
137    int m_cachedSelectionStart;
138    int m_cachedSelectionEnd;
139    TextFieldSelectionDirection m_cachedSelectionDirection;
140};
141
142inline bool isHTMLTextFormControlElement(const Node* node)
143{
144    return node->isElementNode() && toElement(node)->isTextFormControl();
145}
146
147inline HTMLTextFormControlElement* toHTMLTextFormControlElement(Node* node)
148{
149    ASSERT_WITH_SECURITY_IMPLICATION(!node || isHTMLTextFormControlElement(node));
150    return static_cast<HTMLTextFormControlElement*>(node);
151}
152
153HTMLTextFormControlElement* enclosingTextFormControl(const Position&);
154
155} // namespace
156
157#endif
158