Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(181)

Side by Side Diff: chrome/browser/resources/settings/search_settings.js

Issue 2082793003: MD Settings: First iteration of searching within settings. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@cr_search_migration0
Patch Set: Recursive version Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 cr.define('settings', function() {
6 /** @const {string} */
7 var WRAPPER_CSS_CLASS = 'search-highlight-wrapper';
8
9 /** @const {string} */
10 var HIT_CSS_CLASS = 'search-highlight-hit';
11
12 /** @const {!RegExp} */
13 var SANITIZE_REGEX = /[-[\]{}()*+?.,\\^$|#\s]/g;
14
15 /**
16 * List of elements types that should not be searched at all.
17 * @const {!Set<string>}
18 */
19 IGNORED_ELEMENTS = new Set([
Dan Beam 2016/07/01 00:54:55 nit: var IGNORED_ELEMENTS
dpapad 2016/07/01 18:08:43 Oops, done.
20 'CONTENT',
21 'CR-EVENTS',
22 'IMG',
23 'IRON-ICON',
24 'PAPER-ICON-BUTTON',
25 /* TODO(dpapad): paper-item is used for dynamically populated dropdown
26 * menus. Perhaps a better approach is to mark the entire dropdown menu such
27 * that search algorithm can skip it as a whole instead.
28 */
29 'PAPER-ITEM',
30 'PAPER-RIPPLE',
31 'STYLE',
32 'TEMPLATE',
33 ]);
34
35
36 /**
37 * Finds all previous highlighted nodes under |node| (both within self and
38 * children's Shadow DOM) and removes the highlight.
39 * @param {!Node} node
40 * @private
41 */
42 function findAndRemoveHighligts_(node) {
43 removeHighlightUi_(node.shadowRoot);
44
45 // Finds all descendants of |node| that have their own Shadow DOM. It
46 // does NOT pierce through the Shadow DOM boundary (which is why
47 // |findAndRemoveHighligts_| has to be called recursively.
48 var children = [];
49 var walker = document.createTreeWalker(
dpapad 2016/06/30 21:27:44 I am still using TreeWalker for removing highlight
Dan Beam 2016/07/01 00:54:55 can we use queryEffectiveChildren('.search-highlig
dpapad 2016/07/01 18:08:43 search-highlight-hit and search-hightlight-wrapper
50 node.shadowRoot, NodeFilter.SHOW_ELEMENT, null, false);
51 var currentNode = null;
52 while ((currentNode = walker.nextNode()) !== null) {
53 if (!!currentNode.shadowRoot)
54 children.push(currentNode);
55 }
56
57 children.forEach(findAndRemoveHighligts_);
58 }
59
60 /**
61 * Applies the highlight UI (yellow rectangle) around all matches in |node|.
62 * param {!Node} node The text node to be highlighted. |node| ends up being
63 * removed from the DOM tree.
64 * @param {!Array<string>} tokens The string tokens that did not match.
65 * @private
66 */
67 function applyHighlightUi_(node, tokens) {
68 var wrapper = document.createElement('span');
69 wrapper.classList.add(WRAPPER_CSS_CLASS);
70 // Use existing node as placeholder to determine where to insert the
71 // replacement content.
72 node.parentNode.insertBefore(wrapper, node);
73
74 for (var i = 0; i < tokens.length; ++i) {
75 if (i % 2 == 0) {
76 wrapper.appendChild(document.createTextNode(tokens[i]));
77 } else {
78 var span = document.createElement('span');
79 span.classList.add(HIT_CSS_CLASS);
80 span.style['background-color'] = 'yellow';
81 span.textContent = tokens[i];
82 wrapper.appendChild(span);
83 }
84 }
85
86 node.remove();
87 }
88
89 /**
90 * Removes highlight UI (yellow rectangle) from previous matches in the given
91 * element and descendants. It does NOT pierce through Shadow DOM.
92 * @param {!Element} element
93 * @private
94 */
95 function removeHighlightUi_(element) {
96 var wrappers = element.querySelectorAll('.' + WRAPPER_CSS_CLASS);
97 for (var i = 0; i < wrappers.length; i++) {
98 var wrapper = wrappers[i];
99 var hitElements = wrapper.querySelectorAll('.' + HIT_CSS_CLASS);
100 // For each hit element, remove the highlighting.
101 for (var j = 0; j < hitElements.length; j++) {
102 var hitElement = hitElements[j];
103 wrapper.replaceChild(hitElement.firstChild, hitElement);
104 }
105
106 // Normalize so that adjacent text nodes will be combined.
107 wrapper.normalize();
108 // Restore the DOM structure as it was before the search occurred.
109 if (wrapper.previousSibling)
110 wrapper.textContent = ' ' + wrapper.textContent;
111 if (wrapper.nextSibling)
112 wrapper.textContent = wrapper.textContent + ' ';
113
114 wrapper.parentElement.insertBefore(
115 wrapper.firstChild, wrapper.nextSibling);
116
117 wrapper.remove();
118 }
119 }
120
121 /**
122 * Traverses the entire DOM (including Shadow DOM), finds text nodes that
123 * match the given regular expression and applies the highlight UI. It also
124 * ensures that <settings-section> instances become visible if any matches
125 * occurred under their subtree.
126 *
127 * @param {!Element} page The page to be searched, should be either
128 * <settings-basic-page> or <settings-advanced-page>.
129 * @param {!RegExp} regExp The regular expression to detect matches.
130 * @private
131 */
132 function findAndHighlightMatches_(page, regExp) {
133 function doSearch(node) {
134 if (IGNORED_ELEMENTS.has(node.tagName))
Dan Beam 2016/07/01 00:54:55 esprehn@ recommended just doing: function ignoreN
dpapad 2016/07/01 18:08:43 Looking up whether an item exists in a Set is adve
135 return;
136
137 if (node.nodeType == Node.TEXT_NODE) {
138 var textContent = node.nodeValue.trim();
139 if (textContent.length == 0)
140 return;
141 if (regExp.test(textContent)) {
Dan Beam 2016/07/01 00:54:55 it's not clear to me whether we should be using re
142 revealParentSection_(node);
143 applyHighlightUi_(node, textContent.split(regExp));
Dan Beam 2016/07/01 00:54:55 ah, if we use regex we don't have to make as many
dpapad 2016/07/01 18:08:43 True.
144 return;
145 }
146 }
147
148 var childNodes = node.childNodes;
149 for (var i = 0, length = childNodes.length; i < length; ++i) {
150 doSearch(childNodes[i]);
Dan Beam 2016/07/01 00:54:55 esprehn said using https://developer.mozilla.org/e
dpapad 2016/07/01 18:08:43 Done. There was a subtlety here though. If there i
dpapad 2016/07/01 18:21:11 Fixed subtlety at patchset 7. TEXT_NODE still have
151 }
152
153 var shadowRoot = node.shadowRoot;
154 if (shadowRoot)
155 doSearch(shadowRoot);
156 }
157
158 doSearch(page);
159 }
160
161 /**
162 * Finds and makes visible the <settings-section> parent of |node|.
163 * @param {!Node} node
164 */
165 function revealParentSection_(node) {
166 // Find corresponding SETTINGS-SECTION parent and make it visible.
167 var parent = node;
168 while (parent && parent.tagName !== 'SETTINGS-SECTION') {
169 parent = parent.nodeType == Node.DOCUMENT_FRAGMENT_NODE ?
170 parent.host : parent.parentNode;
Dan Beam 2016/07/01 00:54:55 worth using domHost? https://www.polymer-project.o
dpapad 2016/07/01 18:08:43 No. The reason is that domHost includes a few Poly
171 }
172 if (parent)
173 parent.style.display = '';
174 }
175
176 /**
177 * @param {!Element} page
178 * @param {boolean} visible
179 * @private
180 */
181 function setSectionsVisibility_(page, visible) {
182 var sections = Polymer.dom(page.root).querySelectorAll('settings-section');
183 for (var i = 0; i < sections.length; i++)
184 sections[i].style.display = visible ? '' : 'none';
Dan Beam 2016/07/01 00:54:55 can we use [hidden] instead?
dpapad 2016/07/01 18:08:43 Done.
185 }
186
187 /**
188 * Performs hierarchical search, starting at the given page element.
189 * @param {string} text
190 * @param {!Element} page Must be either <settings-basic-page> or
191 * <settings-advanced-page>.
192 */
193 function search(text, page) {
194 findAndRemoveHighligts_(page);
195
196 // Generate search text by applying lowercase and escaping any characters
197 // that would be problematic for regular expressions.
198 var searchText = text.trim().toLowerCase().replace(SANITIZE_REGEX, '\\$&');
Dan Beam 2016/07/01 00:54:55 why toLowerCase() if we're using 'i'?
dpapad 2016/07/01 18:08:43 Removed.
199 if (searchText.length == 0) {
200 setSectionsVisibility_(page, true);
201 return;
202 }
203
204 setSectionsVisibility_(page, false);
205 findAndHighlightMatches_(page, new RegExp('(' + searchText + ')', 'i'));
206 }
207
208 return {
209 search: search,
210 };
211 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698