1/*
2 * Copyright (C) 1999 Lars Knoll ([email protected])
3 * (C) 1999 Antti Koivisto ([email protected])
4 * (C) 2000 Dirk Mueller ([email protected])
5 * Copyright (C) 2004-2018 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#pragma once
26
27#include "HTMLFormControlElementWithState.h"
28
29namespace WebCore {
30
31class Position;
32class RenderTextControl;
33class TextControlInnerTextElement;
34class VisiblePosition;
35
36enum class AutoFillButtonType : uint8_t { None, Credentials, Contacts, StrongPassword, CreditCard };
37enum TextFieldSelectionDirection { SelectionHasNoDirection, SelectionHasForwardDirection, SelectionHasBackwardDirection };
38enum TextFieldEventBehavior { DispatchNoEvent, DispatchChangeEvent, DispatchInputAndChangeEvent };
39
40class HTMLTextFormControlElement : public HTMLFormControlElementWithState {
41 WTF_MAKE_ISO_ALLOCATED(HTMLTextFormControlElement);
42public:
43 // Common flag for HTMLInputElement::tooLong() / tooShort() and HTMLTextAreaElement::tooLong() / tooShort().
44 enum NeedsToCheckDirtyFlag {CheckDirtyFlag, IgnoreDirtyFlag};
45
46 virtual ~HTMLTextFormControlElement();
47
48 void didEditInnerTextValue();
49 void forwardEvent(Event&);
50
51 int maxLength() const { return m_maxLength; }
52 WEBCORE_EXPORT ExceptionOr<void> setMaxLength(int);
53 int minLength() const { return m_minLength; }
54 ExceptionOr<void> setMinLength(int);
55
56 InsertedIntoAncestorResult insertedIntoAncestor(InsertionType, ContainerNode&) override;
57
58 // The derived class should return true if placeholder processing is needed.
59 bool isPlaceholderVisible() const { return m_isPlaceholderVisible; }
60 virtual bool supportsPlaceholder() const = 0;
61 String strippedPlaceholder() const;
62 virtual HTMLElement* placeholderElement() const = 0;
63 void updatePlaceholderVisibility();
64
65 int indexForVisiblePosition(const VisiblePosition&) const;
66 WEBCORE_EXPORT VisiblePosition visiblePositionForIndex(int index) const;
67 WEBCORE_EXPORT int selectionStart() const;
68 WEBCORE_EXPORT int selectionEnd() const;
69 WEBCORE_EXPORT const AtomString& selectionDirection() const;
70 WEBCORE_EXPORT void setSelectionStart(int);
71 WEBCORE_EXPORT void setSelectionEnd(int);
72 WEBCORE_EXPORT void setSelectionDirection(const String&);
73 WEBCORE_EXPORT void select(SelectionRevealMode = SelectionRevealMode::DoNotReveal, const AXTextStateChangeIntent& = AXTextStateChangeIntent());
74 WEBCORE_EXPORT virtual ExceptionOr<void> setRangeText(const String& replacement);
75 WEBCORE_EXPORT virtual ExceptionOr<void> setRangeText(const String& replacement, unsigned start, unsigned end, const String& selectionMode);
76 void setSelectionRange(int start, int end, const String& direction, const AXTextStateChangeIntent& = AXTextStateChangeIntent());
77 WEBCORE_EXPORT void setSelectionRange(int start, int end, TextFieldSelectionDirection = SelectionHasNoDirection, SelectionRevealMode = SelectionRevealMode::DoNotReveal, const AXTextStateChangeIntent& = AXTextStateChangeIntent());
78 RefPtr<Range> selection() const;
79 String selectedText() const;
80
81 void dispatchFormControlChangeEvent() final;
82
83 virtual String value() const = 0;
84
85 virtual RefPtr<TextControlInnerTextElement> innerTextElement() const = 0;
86 virtual RenderStyle createInnerTextStyle(const RenderStyle&) = 0;
87
88 void selectionChanged(bool shouldFireSelectEvent);
89 WEBCORE_EXPORT bool lastChangeWasUserEdit() const;
90 void setInnerTextValue(const String&);
91 String innerTextValue() const;
92
93 String directionForFormData() const;
94
95 void setTextAsOfLastFormControlChangeEvent(const String& text) { m_textAsOfLastFormControlChangeEvent = text; }
96#if PLATFORM(IOS_FAMILY)
97 WEBCORE_EXPORT void hidePlaceholder();
98 WEBCORE_EXPORT void showPlaceholderIfNecessary();
99#endif
100
101 WEBCORE_EXPORT virtual bool isInnerTextElementEditable() const;
102
103protected:
104 HTMLTextFormControlElement(const QualifiedName&, Document&, HTMLFormElement*);
105 bool isPlaceholderEmpty() const;
106 virtual void updatePlaceholderText() = 0;
107
108 void parseAttribute(const QualifiedName&, const AtomString&) override;
109
110 void disabledStateChanged() override;
111 void readOnlyStateChanged() override;
112
113 void updateInnerTextElementEditability();
114
115 void cacheSelection(int start, int end, TextFieldSelectionDirection direction)
116 {
117 m_cachedSelectionStart = start;
118 m_cachedSelectionEnd = end;
119 m_cachedSelectionDirection = direction;
120 }
121
122 void restoreCachedSelection(SelectionRevealMode, const AXTextStateChangeIntent& = AXTextStateChangeIntent());
123 bool hasCachedSelection() const { return m_cachedSelectionStart >= 0; }
124
125 virtual void subtreeHasChanged() = 0;
126
127 void setLastChangeWasNotUserEdit() { m_lastChangeWasUserEdit = false; }
128
129 String valueWithHardLineBreaks() const;
130
131 void adjustInnerTextStyle(const RenderStyle& parentStyle, RenderStyle& textBlockStyle) const;
132
133 void internalSetMaxLength(int maxLength) { m_maxLength = maxLength; }
134 void internalSetMinLength(int minLength) { m_minLength = minLength; }
135
136private:
137 TextFieldSelectionDirection cachedSelectionDirection() const { return static_cast<TextFieldSelectionDirection>(m_cachedSelectionDirection); }
138
139 bool isTextFormControlElement() const final { return true; }
140
141 int computeSelectionStart() const;
142 int computeSelectionEnd() const;
143 TextFieldSelectionDirection computeSelectionDirection() const;
144
145 void dispatchFocusEvent(RefPtr<Element>&& oldFocusedElement, FocusDirection) final;
146 void dispatchBlurEvent(RefPtr<Element>&& newFocusedElement) final;
147 bool childShouldCreateRenderer(const Node&) const override;
148
149 unsigned indexForPosition(const Position&) const;
150
151 // Returns true if user-editable value is empty. Used to check placeholder visibility.
152 virtual bool isEmptyValue() const = 0;
153 // Called in dispatchFocusEvent(), after placeholder process, before calling parent's dispatchFocusEvent().
154 virtual void handleFocusEvent(Node* /* oldFocusedNode */, FocusDirection) { }
155 // Called in dispatchBlurEvent(), after placeholder process, before calling parent's dispatchBlurEvent().
156 virtual void handleBlurEvent() { }
157
158 bool placeholderShouldBeVisible() const;
159
160 unsigned m_cachedSelectionDirection : 2;
161 unsigned m_lastChangeWasUserEdit : 1;
162 unsigned m_isPlaceholderVisible : 1;
163
164 String m_textAsOfLastFormControlChangeEvent;
165
166 int m_cachedSelectionStart;
167 int m_cachedSelectionEnd;
168
169 int m_maxLength { -1 };
170 int m_minLength { -1 };
171};
172
173HTMLTextFormControlElement* enclosingTextFormControl(const Position&);
174
175} // namespace WebCore
176
177SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::HTMLTextFormControlElement)
178 static bool isType(const WebCore::Element& element) { return element.isTextFormControlElement(); }
179 static bool isType(const WebCore::Node& node) { return is<WebCore::Element>(node) && isType(downcast<WebCore::Element>(node)); }
180 static bool isType(const WebCore::EventTarget& target) { return is<WebCore::Node>(target) && isType(downcast<WebCore::Node>(target)); }
181SPECIALIZE_TYPE_TRAITS_END()
182