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.
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.
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.
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.
25 #ifndef HTMLTextFormControlElement_h
26 #define HTMLTextFormControlElement_h
28 #include "core/CoreExport.h"
29 #include "core/editing/VisiblePosition.h"
30 #include "core/html/HTMLFormControlElementWithState.h"
37 enum TextFieldSelectionDirection
{ SelectionHasNoDirection
, SelectionHasForwardDirection
, SelectionHasBackwardDirection
};
38 enum TextFieldEventBehavior
{ DispatchNoEvent
, DispatchChangeEvent
, DispatchInputAndChangeEvent
};
39 enum NeedToDispatchSelectEvent
{ DispatchSelectEvent
, NotDispatchSelectEvent
};
41 class CORE_EXPORT HTMLTextFormControlElement
: public HTMLFormControlElementWithState
{
43 // Common flag for HTMLInputElement::tooLong(), HTMLTextAreaElement::tooLong(),
44 // HTMLInputElement::tooShort() and HTMLTextAreaElement::tooShort().
45 enum NeedsToCheckDirtyFlag
{CheckDirtyFlag
, IgnoreDirtyFlag
};
46 // Option of setSelectionRange.
47 enum SelectionOption
{
49 ChangeSelectionAndFocus
,
50 ChangeSelectionIfFocused
,
54 ~HTMLTextFormControlElement() override
;
56 void forwardEvent(Event
*);
59 InsertionNotificationRequest
insertedInto(ContainerNode
*) override
;
61 // The derived class should return true if placeholder processing is needed.
62 virtual bool isPlaceholderVisible() const = 0;
63 virtual void setPlaceholderVisibility(bool) = 0;
64 virtual bool supportsPlaceholder() const = 0;
65 String
strippedPlaceholder() const;
66 HTMLElement
* placeholderElement() const;
67 void updatePlaceholderVisibility();
69 VisiblePosition
visiblePositionForIndex(int) const;
70 int indexForVisiblePosition(const VisiblePosition
&) const;
71 int selectionStart() const;
72 int selectionEnd() const;
73 const AtomicString
& selectionDirection() const;
74 void setSelectionStart(int);
75 void setSelectionEnd(int);
76 void setSelectionDirection(const String
&);
77 void select(NeedToDispatchSelectEvent
= DispatchSelectEvent
);
78 virtual void setRangeText(const String
& replacement
, ExceptionState
&);
79 virtual void setRangeText(const String
& replacement
, unsigned start
, unsigned end
, const String
& selectionMode
, ExceptionState
&);
80 void setSelectionRange(int start
, int end
, const String
& direction
);
81 void setSelectionRange(int start
, int end
, TextFieldSelectionDirection
= SelectionHasNoDirection
, NeedToDispatchSelectEvent
= DispatchSelectEvent
, SelectionOption
= ChangeSelection
);
82 PassRefPtrWillBeRawPtr
<Range
> selection() const;
84 virtual bool supportsAutocapitalize() const = 0;
85 virtual const AtomicString
& defaultAutocapitalize() const = 0;
86 const AtomicString
& autocapitalize() const;
87 void setAutocapitalize(const AtomicString
&);
89 void dispatchFormControlChangeEvent() final
;
91 virtual String
value() const = 0;
93 HTMLElement
* innerEditorElement() const;
95 void selectionChanged(bool userTriggered
);
96 bool lastChangeWasUserEdit() const;
97 virtual void setInnerEditorValue(const String
&);
98 String
innerEditorValue() const;
100 String
directionForFormData() const;
102 void setTextAsOfLastFormControlChangeEvent(const String
& text
) { m_textAsOfLastFormControlChangeEvent
= text
; }
104 // These functions don't cause synchronous layout and SpellChecker uses
105 // them to improve performance.
106 // Passed |Position| must point inside of a text form control.
107 static Position
startOfWord(const Position
&);
108 static Position
endOfWord(const Position
&);
109 static Position
startOfSentence(const Position
&);
110 static Position
endOfSentence(const Position
&);
113 HTMLTextFormControlElement(const QualifiedName
&, Document
&, HTMLFormElement
*);
114 bool isPlaceholderEmpty() const;
115 virtual void updatePlaceholderText() = 0;
117 void parseAttribute(const QualifiedName
&, const AtomicString
&) override
;
119 void cacheSelection(int start
, int end
, TextFieldSelectionDirection direction
)
122 m_cachedSelectionStart
= start
;
123 m_cachedSelectionEnd
= end
;
124 m_cachedSelectionDirection
= direction
;
127 void restoreCachedSelection();
129 void defaultEventHandler(Event
*) override
;
130 virtual void subtreeHasChanged() = 0;
132 void setLastChangeWasNotUserEdit() { m_lastChangeWasUserEdit
= false; }
134 String
valueWithHardLineBreaks() const;
136 virtual bool shouldDispatchFormControlChangeEvent(String
&, String
&);
137 void copyNonAttributePropertiesFromElement(const Element
&) override
;
140 int computeSelectionStart() const;
141 int computeSelectionEnd() const;
142 TextFieldSelectionDirection
computeSelectionDirection() const;
144 void dispatchFocusEvent(Element
* oldFocusedElement
, WebFocusType
, InputDeviceCapabilities
* sourceCapabilities
) final
;
145 void dispatchBlurEvent(Element
* newFocusedElement
, WebFocusType
, InputDeviceCapabilities
* sourceCapabilities
) final
;
146 void scheduleSelectEvent();
148 // Returns true if user-editable value is empty. Used to check placeholder visibility.
149 virtual bool isEmptyValue() const = 0;
150 // Returns true if suggested value is empty. Used to check placeholder visibility.
151 virtual bool isEmptySuggestedValue() const { return true; }
152 // Called in dispatchFocusEvent(), after placeholder process, before calling parent's dispatchFocusEvent().
153 virtual void handleFocusEvent(Element
* /* oldFocusedNode */, WebFocusType
) { }
154 // Called in dispatchBlurEvent(), after placeholder process, before calling parent's dispatchBlurEvent().
155 virtual void handleBlurEvent() { }
157 bool placeholderShouldBeVisible() const;
159 String m_textAsOfLastFormControlChangeEvent
;
160 bool m_lastChangeWasUserEdit
;
162 int m_cachedSelectionStart
;
163 int m_cachedSelectionEnd
;
164 TextFieldSelectionDirection m_cachedSelectionDirection
;
167 inline bool isHTMLTextFormControlElement(const Element
& element
)
169 return element
.isTextFormControl();
172 DEFINE_HTMLELEMENT_TYPE_CASTS_WITH_FUNCTION(HTMLTextFormControlElement
);
174 HTMLTextFormControlElement
* enclosingTextFormControl(const Position
&);
175 HTMLTextFormControlElement
* enclosingTextFormControl(Node
*);