1 | /**
|
---|
2 | * Copyright (C) 2006, 2007, 2010, 2015 Apple Inc. All rights reserved.
|
---|
3 | * (C) 2008 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
|
---|
4 | * Copyright (C) 2010 Google Inc. All rights reserved.
|
---|
5 | * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
---|
6 | *
|
---|
7 | * This library is free software; you can redistribute it and/or
|
---|
8 | * modify it under the terms of the GNU Library General Public
|
---|
9 | * License as published by the Free Software Foundation; either
|
---|
10 | * version 2 of the License, or (at your option) any later version.
|
---|
11 | *
|
---|
12 | * This library is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
15 | * Library General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU Library General Public License
|
---|
18 | * along with this library; see the file COPYING.LIB. If not, write to
|
---|
19 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
---|
20 | * Boston, MA 02110-1301, USA.
|
---|
21 | *
|
---|
22 | */
|
---|
23 |
|
---|
24 | #include "config.h"
|
---|
25 | #include "RenderSearchField.h"
|
---|
26 |
|
---|
27 | #include "CSSFontSelector.h"
|
---|
28 | #include "CSSValueKeywords.h"
|
---|
29 | #include "Chrome.h"
|
---|
30 | #include "ElementInlines.h"
|
---|
31 | #include "Font.h"
|
---|
32 | #include "Frame.h"
|
---|
33 | #include "FrameSelection.h"
|
---|
34 | #include "FrameView.h"
|
---|
35 | #include "HTMLInputElement.h"
|
---|
36 | #include "HTMLNames.h"
|
---|
37 | #include "HitTestResult.h"
|
---|
38 | #include "LocalizedStrings.h"
|
---|
39 | #include "Page.h"
|
---|
40 | #include "PopupMenu.h"
|
---|
41 | #include "RenderLayer.h"
|
---|
42 | #include "RenderScrollbar.h"
|
---|
43 | #include "RenderTheme.h"
|
---|
44 | #include "RenderView.h"
|
---|
45 | #include "StyleResolver.h"
|
---|
46 | #include "TextControlInnerElements.h"
|
---|
47 | #include <wtf/IsoMallocInlines.h>
|
---|
48 |
|
---|
49 | namespace WebCore {
|
---|
50 |
|
---|
51 | using namespace HTMLNames;
|
---|
52 |
|
---|
53 | WTF_MAKE_ISO_ALLOCATED_IMPL(RenderSearchField);
|
---|
54 |
|
---|
55 | RenderSearchField::RenderSearchField(HTMLInputElement& element, RenderStyle&& style)
|
---|
56 | : RenderTextControlSingleLine(element, WTFMove(style))
|
---|
57 | , m_searchPopupIsVisible(false)
|
---|
58 | , m_searchPopup(nullptr)
|
---|
59 | {
|
---|
60 | ASSERT(element.isSearchField());
|
---|
61 | }
|
---|
62 |
|
---|
63 | RenderSearchField::~RenderSearchField()
|
---|
64 | {
|
---|
65 | // Do not add any code here. Add it to willBeDestroyed() instead.
|
---|
66 | }
|
---|
67 |
|
---|
68 | void RenderSearchField::willBeDestroyed()
|
---|
69 | {
|
---|
70 | if (m_searchPopup) {
|
---|
71 | m_searchPopup->popupMenu()->disconnectClient();
|
---|
72 | m_searchPopup = nullptr;
|
---|
73 | }
|
---|
74 |
|
---|
75 | RenderTextControlSingleLine::willBeDestroyed();
|
---|
76 | }
|
---|
77 |
|
---|
78 | inline HTMLElement* RenderSearchField::resultsButtonElement() const
|
---|
79 | {
|
---|
80 | return inputElement().resultsButtonElement();
|
---|
81 | }
|
---|
82 |
|
---|
83 | inline HTMLElement* RenderSearchField::cancelButtonElement() const
|
---|
84 | {
|
---|
85 | return inputElement().cancelButtonElement();
|
---|
86 | }
|
---|
87 |
|
---|
88 | void RenderSearchField::addSearchResult()
|
---|
89 | {
|
---|
90 | if (inputElement().maxResults() <= 0)
|
---|
91 | return;
|
---|
92 |
|
---|
93 | String value = inputElement().value();
|
---|
94 | if (value.isEmpty())
|
---|
95 | return;
|
---|
96 |
|
---|
97 | if (page().usesEphemeralSession())
|
---|
98 | return;
|
---|
99 |
|
---|
100 | m_recentSearches.removeAllMatching([value] (const RecentSearch& recentSearch) {
|
---|
101 | return recentSearch.string == value;
|
---|
102 | });
|
---|
103 |
|
---|
104 | RecentSearch recentSearch = { value, WallTime::now() };
|
---|
105 | m_recentSearches.insert(0, recentSearch);
|
---|
106 | while (static_cast<int>(m_recentSearches.size()) > inputElement().maxResults())
|
---|
107 | m_recentSearches.removeLast();
|
---|
108 |
|
---|
109 | const AtomString& name = autosaveName();
|
---|
110 | if (!m_searchPopup)
|
---|
111 | m_searchPopup = page().chrome().createSearchPopupMenu(*this);
|
---|
112 |
|
---|
113 | m_searchPopup->saveRecentSearches(name, m_recentSearches);
|
---|
114 | }
|
---|
115 |
|
---|
116 | void RenderSearchField::showPopup()
|
---|
117 | {
|
---|
118 | if (m_searchPopupIsVisible)
|
---|
119 | return;
|
---|
120 |
|
---|
121 | if (!m_searchPopup)
|
---|
122 | m_searchPopup = page().chrome().createSearchPopupMenu(*this);
|
---|
123 |
|
---|
124 | if (!m_searchPopup->enabled())
|
---|
125 | return;
|
---|
126 |
|
---|
127 | m_searchPopupIsVisible = true;
|
---|
128 |
|
---|
129 | const AtomString& name = autosaveName();
|
---|
130 | m_searchPopup->loadRecentSearches(name, m_recentSearches);
|
---|
131 |
|
---|
132 | // Trim the recent searches list if the maximum size has changed since we last saved.
|
---|
133 | if (static_cast<int>(m_recentSearches.size()) > inputElement().maxResults()) {
|
---|
134 | do {
|
---|
135 | m_recentSearches.removeLast();
|
---|
136 | } while (static_cast<int>(m_recentSearches.size()) > inputElement().maxResults());
|
---|
137 |
|
---|
138 | m_searchPopup->saveRecentSearches(name, m_recentSearches);
|
---|
139 | }
|
---|
140 |
|
---|
141 | FloatPoint absTopLeft = localToAbsolute(FloatPoint(), UseTransforms);
|
---|
142 | IntRect absBounds = absoluteBoundingBoxRectIgnoringTransforms();
|
---|
143 | absBounds.setLocation(roundedIntPoint(absTopLeft));
|
---|
144 | m_searchPopup->popupMenu()->show(absBounds, &view().frameView(), -1);
|
---|
145 | }
|
---|
146 |
|
---|
147 | void RenderSearchField::hidePopup()
|
---|
148 | {
|
---|
149 | if (m_searchPopup)
|
---|
150 | m_searchPopup->popupMenu()->hide();
|
---|
151 | }
|
---|
152 |
|
---|
153 | LayoutUnit RenderSearchField::computeControlLogicalHeight(LayoutUnit lineHeight, LayoutUnit nonContentHeight) const
|
---|
154 | {
|
---|
155 | HTMLElement* resultsButton = resultsButtonElement();
|
---|
156 | if (RenderBox* resultsRenderer = resultsButton ? resultsButton->renderBox() : 0) {
|
---|
157 | resultsRenderer->updateLogicalHeight();
|
---|
158 | nonContentHeight = std::max(nonContentHeight, resultsRenderer->borderAndPaddingLogicalHeight() + resultsRenderer->marginLogicalHeight());
|
---|
159 | lineHeight = std::max(lineHeight, resultsRenderer->logicalHeight());
|
---|
160 | }
|
---|
161 | HTMLElement* cancelButton = cancelButtonElement();
|
---|
162 | if (RenderBox* cancelRenderer = cancelButton ? cancelButton->renderBox() : 0) {
|
---|
163 | cancelRenderer->updateLogicalHeight();
|
---|
164 | nonContentHeight = std::max(nonContentHeight, cancelRenderer->borderAndPaddingLogicalHeight() + cancelRenderer->marginLogicalHeight());
|
---|
165 | lineHeight = std::max(lineHeight, cancelRenderer->logicalHeight());
|
---|
166 | }
|
---|
167 |
|
---|
168 | return lineHeight + nonContentHeight;
|
---|
169 | }
|
---|
170 |
|
---|
171 | void RenderSearchField::updateFromElement()
|
---|
172 | {
|
---|
173 | RenderTextControlSingleLine::updateFromElement();
|
---|
174 |
|
---|
175 | if (cancelButtonElement())
|
---|
176 | updateCancelButtonVisibility();
|
---|
177 |
|
---|
178 | if (m_searchPopupIsVisible)
|
---|
179 | m_searchPopup->popupMenu()->updateFromElement();
|
---|
180 | }
|
---|
181 |
|
---|
182 | void RenderSearchField::updateCancelButtonVisibility() const
|
---|
183 | {
|
---|
184 | RenderElement* cancelButtonRenderer = cancelButtonElement()->renderer();
|
---|
185 | if (!cancelButtonRenderer)
|
---|
186 | return;
|
---|
187 |
|
---|
188 | const RenderStyle& curStyle = cancelButtonRenderer->style();
|
---|
189 | Visibility buttonVisibility = visibilityForCancelButton();
|
---|
190 | if (curStyle.visibility() == buttonVisibility)
|
---|
191 | return;
|
---|
192 |
|
---|
193 | auto cancelButtonStyle = RenderStyle::clone(curStyle);
|
---|
194 | cancelButtonStyle.setVisibility(buttonVisibility);
|
---|
195 | cancelButtonRenderer->setStyle(WTFMove(cancelButtonStyle));
|
---|
196 | }
|
---|
197 |
|
---|
198 | Visibility RenderSearchField::visibilityForCancelButton() const
|
---|
199 | {
|
---|
200 | return (style().visibility() == Visibility::Hidden || inputElement().value().isEmpty()) ? Visibility::Hidden : Visibility::Visible;
|
---|
201 | }
|
---|
202 |
|
---|
203 | const AtomString& RenderSearchField::autosaveName() const
|
---|
204 | {
|
---|
205 | return inputElement().attributeWithoutSynchronization(autosaveAttr);
|
---|
206 | }
|
---|
207 |
|
---|
208 | // PopupMenuClient methods
|
---|
209 | void RenderSearchField::valueChanged(unsigned listIndex, bool fireEvents)
|
---|
210 | {
|
---|
211 | ASSERT(static_cast<int>(listIndex) < listSize());
|
---|
212 | if (static_cast<int>(listIndex) == (listSize() - 1)) {
|
---|
213 | if (fireEvents) {
|
---|
214 | m_recentSearches.clear();
|
---|
215 | const AtomString& name = autosaveName();
|
---|
216 | if (!name.isEmpty()) {
|
---|
217 | if (!m_searchPopup)
|
---|
218 | m_searchPopup = page().chrome().createSearchPopupMenu(*this);
|
---|
219 | m_searchPopup->saveRecentSearches(name, m_recentSearches);
|
---|
220 | }
|
---|
221 | }
|
---|
222 | } else {
|
---|
223 | inputElement().setValue(itemText(listIndex));
|
---|
224 | if (fireEvents)
|
---|
225 | inputElement().onSearch();
|
---|
226 | inputElement().select();
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | String RenderSearchField::itemText(unsigned listIndex) const
|
---|
231 | {
|
---|
232 | #if !PLATFORM(IOS_FAMILY)
|
---|
233 | int size = listSize();
|
---|
234 | if (size == 1) {
|
---|
235 | ASSERT(!listIndex);
|
---|
236 | return searchMenuNoRecentSearchesText();
|
---|
237 | }
|
---|
238 | if (!listIndex)
|
---|
239 | return searchMenuRecentSearchesText();
|
---|
240 | #endif
|
---|
241 | if (itemIsSeparator(listIndex))
|
---|
242 | return String();
|
---|
243 | #if !PLATFORM(IOS_FAMILY)
|
---|
244 | if (static_cast<int>(listIndex) == (size - 1))
|
---|
245 | return searchMenuClearRecentSearchesText();
|
---|
246 | #endif
|
---|
247 | return m_recentSearches[listIndex - 1].string;
|
---|
248 | }
|
---|
249 |
|
---|
250 | String RenderSearchField::itemLabel(unsigned) const
|
---|
251 | {
|
---|
252 | return String();
|
---|
253 | }
|
---|
254 |
|
---|
255 | String RenderSearchField::itemIcon(unsigned) const
|
---|
256 | {
|
---|
257 | return String();
|
---|
258 | }
|
---|
259 |
|
---|
260 | bool RenderSearchField::itemIsEnabled(unsigned listIndex) const
|
---|
261 | {
|
---|
262 | if (!listIndex || itemIsSeparator(listIndex))
|
---|
263 | return false;
|
---|
264 | return true;
|
---|
265 | }
|
---|
266 |
|
---|
267 | PopupMenuStyle RenderSearchField::itemStyle(unsigned) const
|
---|
268 | {
|
---|
269 | return menuStyle();
|
---|
270 | }
|
---|
271 |
|
---|
272 | PopupMenuStyle RenderSearchField::menuStyle() const
|
---|
273 | {
|
---|
274 | return PopupMenuStyle(style().visitedDependentColorWithColorFilter(CSSPropertyColor), style().visitedDependentColorWithColorFilter(CSSPropertyBackgroundColor), style().fontCascade(), style().visibility() == Visibility::Visible,
|
---|
275 | style().display() == DisplayType::None, true, style().textIndent(), style().direction(), isOverride(style().unicodeBidi()), PopupMenuStyle::CustomBackgroundColor);
|
---|
276 | }
|
---|
277 |
|
---|
278 | int RenderSearchField::clientInsetLeft() const
|
---|
279 | {
|
---|
280 | // Inset the menu by the radius of the cap on the left so that
|
---|
281 | // it only runs along the straight part of the bezel.
|
---|
282 | return height() / 2;
|
---|
283 | }
|
---|
284 |
|
---|
285 | int RenderSearchField::clientInsetRight() const
|
---|
286 | {
|
---|
287 | // Inset the menu by the radius of the cap on the right so that
|
---|
288 | // it only runs along the straight part of the bezel (unless it needs
|
---|
289 | // to be wider).
|
---|
290 | return height() / 2;
|
---|
291 | }
|
---|
292 |
|
---|
293 | LayoutUnit RenderSearchField::clientPaddingLeft() const
|
---|
294 | {
|
---|
295 | LayoutUnit padding = paddingLeft();
|
---|
296 | if (RenderBox* box = innerBlockElement() ? innerBlockElement()->renderBox() : 0)
|
---|
297 | padding += box->x();
|
---|
298 | return padding;
|
---|
299 | }
|
---|
300 |
|
---|
301 | LayoutUnit RenderSearchField::clientPaddingRight() const
|
---|
302 | {
|
---|
303 | LayoutUnit padding = paddingRight();
|
---|
304 | if (RenderBox* containerBox = containerElement() ? containerElement()->renderBox() : 0) {
|
---|
305 | if (RenderBox* innerBlockBox = innerBlockElement() ? innerBlockElement()->renderBox() : 0)
|
---|
306 | padding += containerBox->width() - (innerBlockBox->x() + innerBlockBox->width());
|
---|
307 | }
|
---|
308 | return padding;
|
---|
309 | }
|
---|
310 |
|
---|
311 | int RenderSearchField::listSize() const
|
---|
312 | {
|
---|
313 | // If there are no recent searches, then our menu will have 1 "No recent searches" item.
|
---|
314 | if (!m_recentSearches.size())
|
---|
315 | return 1;
|
---|
316 | // Otherwise, leave room in the menu for a header, a separator, and the "Clear recent searches" item.
|
---|
317 | return m_recentSearches.size() + 3;
|
---|
318 | }
|
---|
319 |
|
---|
320 | int RenderSearchField::selectedIndex() const
|
---|
321 | {
|
---|
322 | return -1;
|
---|
323 | }
|
---|
324 |
|
---|
325 | void RenderSearchField::popupDidHide()
|
---|
326 | {
|
---|
327 | m_searchPopupIsVisible = false;
|
---|
328 | }
|
---|
329 |
|
---|
330 | bool RenderSearchField::itemIsSeparator(unsigned listIndex) const
|
---|
331 | {
|
---|
332 | // The separator will be the second to last item in our list.
|
---|
333 | return static_cast<int>(listIndex) == (listSize() - 2);
|
---|
334 | }
|
---|
335 |
|
---|
336 | bool RenderSearchField::itemIsLabel(unsigned listIndex) const
|
---|
337 | {
|
---|
338 | return !listIndex;
|
---|
339 | }
|
---|
340 |
|
---|
341 | bool RenderSearchField::itemIsSelected(unsigned) const
|
---|
342 | {
|
---|
343 | return false;
|
---|
344 | }
|
---|
345 |
|
---|
346 | void RenderSearchField::setTextFromItem(unsigned listIndex)
|
---|
347 | {
|
---|
348 | inputElement().setValue(itemText(listIndex));
|
---|
349 | }
|
---|
350 |
|
---|
351 | FontSelector* RenderSearchField::fontSelector() const
|
---|
352 | {
|
---|
353 | return &document().fontSelector();
|
---|
354 | }
|
---|
355 |
|
---|
356 | HostWindow* RenderSearchField::hostWindow() const
|
---|
357 | {
|
---|
358 | return RenderTextControlSingleLine::hostWindow();
|
---|
359 | }
|
---|
360 |
|
---|
361 | Ref<Scrollbar> RenderSearchField::createScrollbar(ScrollableArea& scrollableArea, ScrollbarOrientation orientation, ScrollbarControlSize controlSize)
|
---|
362 | {
|
---|
363 | bool hasCustomScrollbarStyle = style().hasPseudoStyle(PseudoId::Scrollbar);
|
---|
364 | if (hasCustomScrollbarStyle)
|
---|
365 | return RenderScrollbar::createCustomScrollbar(scrollableArea, orientation, &inputElement());
|
---|
366 | return Scrollbar::createNativeScrollbar(scrollableArea, orientation, controlSize);
|
---|
367 | }
|
---|
368 |
|
---|
369 | }
|
---|