1 | /* |
2 | * Copyright (C) 2010 Google Inc. All rights reserved. |
3 | * Copyright (C) 2014-2018 Apple Inc. All rights reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions are |
7 | * met: |
8 | * |
9 | * * Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * * Redistributions in binary form must reproduce the above |
12 | * copyright notice, this list of conditions and the following disclaimer |
13 | * in the documentation and/or other materials provided with the |
14 | * distribution. |
15 | * * Neither the name of Google Inc. nor the names of its |
16 | * contributors may be used to endorse or promote products derived from |
17 | * this software without specific prior written permission. |
18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
20 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
23 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
31 | |
32 | #include "config.h" |
33 | #include "SearchInputType.h" |
34 | |
35 | #include "HTMLInputElement.h" |
36 | #include "HTMLNames.h" |
37 | #include "InputTypeNames.h" |
38 | #include "KeyboardEvent.h" |
39 | #include "RenderSearchField.h" |
40 | #include "ShadowRoot.h" |
41 | #include "TextControlInnerElements.h" |
42 | |
43 | namespace WebCore { |
44 | |
45 | using namespace HTMLNames; |
46 | |
47 | SearchInputType::SearchInputType(HTMLInputElement& element) |
48 | : BaseTextInputType(element) |
49 | , m_searchEventTimer(*this, &SearchInputType::searchEventTimerFired) |
50 | { |
51 | } |
52 | |
53 | void SearchInputType::addSearchResult() |
54 | { |
55 | #if !PLATFORM(IOS_FAMILY) |
56 | // Normally we've got the correct renderer by the time we get here. However when the input type changes |
57 | // we don't update the associated renderers until after the next tree update, so we could actually end up here |
58 | // with a mismatched renderer (e.g. through form submission). |
59 | ASSERT(element()); |
60 | if (is<RenderSearchField>(element()->renderer())) |
61 | downcast<RenderSearchField>(*element()->renderer()).addSearchResult(); |
62 | #endif |
63 | } |
64 | |
65 | static void updateResultButtonPseudoType(SearchFieldResultsButtonElement& resultButton, int maxResults) |
66 | { |
67 | if (!maxResults) |
68 | resultButton.setPseudo(AtomicString("-webkit-search-results-decoration" , AtomicString::ConstructFromLiteral)); |
69 | else if (maxResults < 0) |
70 | resultButton.setPseudo(AtomicString("-webkit-search-decoration" , AtomicString::ConstructFromLiteral)); |
71 | else |
72 | resultButton.setPseudo(AtomicString("-webkit-search-results-button" , AtomicString::ConstructFromLiteral)); |
73 | } |
74 | |
75 | void SearchInputType::attributeChanged(const QualifiedName& name) |
76 | { |
77 | if (name == resultsAttr) { |
78 | if (m_resultsButton) { |
79 | if (auto* element = this->element()) |
80 | updateResultButtonPseudoType(*m_resultsButton, element->maxResults()); |
81 | } |
82 | } |
83 | BaseTextInputType::attributeChanged(name); |
84 | } |
85 | |
86 | RenderPtr<RenderElement> SearchInputType::createInputRenderer(RenderStyle&& style) |
87 | { |
88 | ASSERT(element()); |
89 | return createRenderer<RenderSearchField>(*element(), WTFMove(style)); |
90 | } |
91 | |
92 | const AtomicString& SearchInputType::formControlType() const |
93 | { |
94 | return InputTypeNames::search(); |
95 | } |
96 | |
97 | bool SearchInputType::isSearchField() const |
98 | { |
99 | return true; |
100 | } |
101 | |
102 | bool SearchInputType::needsContainer() const |
103 | { |
104 | return true; |
105 | } |
106 | |
107 | void SearchInputType::createShadowSubtree() |
108 | { |
109 | ASSERT(!m_resultsButton); |
110 | ASSERT(!m_cancelButton); |
111 | |
112 | TextFieldInputType::createShadowSubtree(); |
113 | RefPtr<HTMLElement> container = containerElement(); |
114 | RefPtr<HTMLElement> textWrapper = innerBlockElement(); |
115 | ASSERT(container); |
116 | ASSERT(textWrapper); |
117 | |
118 | ASSERT(element()); |
119 | m_resultsButton = SearchFieldResultsButtonElement::create(element()->document()); |
120 | updateResultButtonPseudoType(*m_resultsButton, element()->maxResults()); |
121 | container->insertBefore(*m_resultsButton, textWrapper.get()); |
122 | |
123 | m_cancelButton = SearchFieldCancelButtonElement::create(element()->document()); |
124 | container->insertBefore(*m_cancelButton, textWrapper->nextSibling()); |
125 | } |
126 | |
127 | HTMLElement* SearchInputType::resultsButtonElement() const |
128 | { |
129 | return m_resultsButton.get(); |
130 | } |
131 | |
132 | HTMLElement* SearchInputType::cancelButtonElement() const |
133 | { |
134 | return m_cancelButton.get(); |
135 | } |
136 | |
137 | auto SearchInputType::handleKeydownEvent(KeyboardEvent& event) -> ShouldCallBaseEventHandler |
138 | { |
139 | ASSERT(element()); |
140 | if (element()->isDisabledOrReadOnly()) |
141 | return TextFieldInputType::handleKeydownEvent(event); |
142 | |
143 | const String& key = event.keyIdentifier(); |
144 | if (key == "U+001B" ) { |
145 | Ref<HTMLInputElement> protectedInputElement(*element()); |
146 | protectedInputElement->setValueForUser(emptyString()); |
147 | protectedInputElement->onSearch(); |
148 | event.setDefaultHandled(); |
149 | return ShouldCallBaseEventHandler::Yes; |
150 | } |
151 | return TextFieldInputType::handleKeydownEvent(event); |
152 | } |
153 | |
154 | void SearchInputType::destroyShadowSubtree() |
155 | { |
156 | TextFieldInputType::destroyShadowSubtree(); |
157 | m_resultsButton = nullptr; |
158 | m_cancelButton = nullptr; |
159 | } |
160 | |
161 | void SearchInputType::startSearchEventTimer() |
162 | { |
163 | ASSERT(element()); |
164 | ASSERT(element()->renderer()); |
165 | unsigned length = element()->innerTextValue().length(); |
166 | |
167 | if (!length) { |
168 | m_searchEventTimer.startOneShot(0_ms); |
169 | return; |
170 | } |
171 | |
172 | // After typing the first key, we wait 0.5 seconds. |
173 | // After the second key, 0.4 seconds, then 0.3, then 0.2 from then on. |
174 | m_searchEventTimer.startOneShot(std::max(200_ms, 600_ms - 100_ms * length)); |
175 | } |
176 | |
177 | void SearchInputType::stopSearchEventTimer() |
178 | { |
179 | m_searchEventTimer.stop(); |
180 | } |
181 | |
182 | void SearchInputType::searchEventTimerFired() |
183 | { |
184 | ASSERT(element()); |
185 | element()->onSearch(); |
186 | } |
187 | |
188 | bool SearchInputType::searchEventsShouldBeDispatched() const |
189 | { |
190 | ASSERT(element()); |
191 | return element()->hasAttributeWithoutSynchronization(incrementalAttr); |
192 | } |
193 | |
194 | void SearchInputType::didSetValueByUserEdit() |
195 | { |
196 | ASSERT(element()); |
197 | if (m_cancelButton && is<RenderSearchField>(element()->renderer())) |
198 | downcast<RenderSearchField>(*element()->renderer()).updateCancelButtonVisibility(); |
199 | // If the incremental attribute is set, then dispatch the search event |
200 | if (searchEventsShouldBeDispatched()) |
201 | startSearchEventTimer(); |
202 | |
203 | TextFieldInputType::didSetValueByUserEdit(); |
204 | } |
205 | |
206 | bool SearchInputType::sizeShouldIncludeDecoration(int, int& preferredSize) const |
207 | { |
208 | ASSERT(element()); |
209 | preferredSize = element()->size(); |
210 | return true; |
211 | } |
212 | |
213 | float SearchInputType::decorationWidth() const |
214 | { |
215 | float width = 0; |
216 | if (m_resultsButton) |
217 | width += m_resultsButton->computedStyle()->logicalWidth().value(); |
218 | if (m_cancelButton) |
219 | width += m_cancelButton->computedStyle()->logicalWidth().value(); |
220 | return width; |
221 | } |
222 | |
223 | } // namespace WebCore |
224 | |