Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 var IGNORED_ELEMENTS = new Set([ | |
| 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 * Finds all previous highlighted nodes under |node| (both within self and | |
| 37 * children's Shadow DOM) and removes the highlight (yellow rectangle). | |
| 38 * @param {!Node} node | |
| 39 * @private | |
| 40 */ | |
| 41 function findAndRemoveHighligts_(node) { | |
|
Dan Beam
2016/07/01 21:32:34
fix typo
dpapad
2016/07/01 21:59:42
Done.
| |
| 42 var wrappers = node.querySelectorAll('* /deep/ .' + WRAPPER_CSS_CLASS); | |
| 43 | |
| 44 for (var i = 0; i < wrappers.length; i++) { | |
| 45 var wrapper = wrappers[i]; | |
| 46 var hitElements = wrapper.querySelectorAll('.' + HIT_CSS_CLASS); | |
| 47 // For each hit element, remove the highlighting. | |
| 48 for (var j = 0; j < hitElements.length; j++) { | |
| 49 var hitElement = hitElements[j]; | |
| 50 wrapper.replaceChild(hitElement.firstChild, hitElement); | |
| 51 } | |
| 52 | |
| 53 // Normalize so that adjacent text nodes will be combined. | |
| 54 wrapper.normalize(); | |
| 55 // Restore the DOM structure as it was before the search occurred. | |
| 56 if (wrapper.previousSibling) | |
| 57 wrapper.textContent = ' ' + wrapper.textContent; | |
| 58 if (wrapper.nextSibling) | |
| 59 wrapper.textContent = wrapper.textContent + ' '; | |
| 60 | |
| 61 wrapper.parentElement.insertBefore( | |
| 62 wrapper.firstChild, wrapper.nextSibling); | |
| 63 | |
| 64 wrapper.remove(); | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 /** | |
| 69 * Applies the highlight UI (yellow rectangle) around all matches in |node|. | |
| 70 * param {!Node} node The text node to be highlighted. |node| ends up being | |
| 71 * removed from the DOM tree. | |
| 72 * @param {!Array<string>} tokens The string tokens that did not match. | |
| 73 * @private | |
| 74 */ | |
| 75 function applyHighlightUi_(node, tokens) { | |
|
Dan Beam
2016/07/01 21:32:35
i don't think you need 2 verbs here, i.e. just hig
dpapad
2016/07/01 21:59:42
Done.
| |
| 76 var wrapper = document.createElement('span'); | |
| 77 wrapper.classList.add(WRAPPER_CSS_CLASS); | |
| 78 // Use existing node as placeholder to determine where to insert the | |
| 79 // replacement content. | |
| 80 node.parentNode.insertBefore(wrapper, node); | |
| 81 | |
| 82 for (var i = 0; i < tokens.length; ++i) { | |
| 83 if (i % 2 == 0) { | |
| 84 wrapper.appendChild(document.createTextNode(tokens[i])); | |
| 85 } else { | |
| 86 var span = document.createElement('span'); | |
| 87 span.classList.add(HIT_CSS_CLASS); | |
| 88 span.style['background-color'] = 'yellow'; | |
| 89 span.textContent = tokens[i]; | |
| 90 wrapper.appendChild(span); | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 node.remove(); | |
| 95 } | |
| 96 | |
| 97 /** | |
| 98 * Traverses the entire DOM (including Shadow DOM), finds text nodes that | |
| 99 * match the given regular expression and applies the highlight UI. It also | |
| 100 * ensures that <settings-section> instances become visible if any matches | |
| 101 * occurred under their subtree. | |
| 102 * | |
| 103 * @param {!Element} page The page to be searched, should be either | |
| 104 * <settings-basic-page> or <settings-advanced-page>. | |
| 105 * @param {!RegExp} regExp The regular expression to detect matches. | |
| 106 * @private | |
| 107 */ | |
| 108 function findAndHighlightMatches_(page, regExp) { | |
| 109 function doSearch(node) { | |
| 110 if (IGNORED_ELEMENTS.has(node.tagName)) | |
| 111 return; | |
| 112 | |
| 113 if (node.nodeType == Node.TEXT_NODE) { | |
| 114 var textContent = node.nodeValue.trim(); | |
| 115 if (textContent.length == 0) | |
| 116 return; | |
| 117 | |
| 118 if (regExp.test(textContent)) { | |
| 119 revealParentSection_(node); | |
| 120 applyHighlightUi_(node, textContent.split(regExp)); | |
| 121 } | |
| 122 // Returning early since TEXT_NODE nodes never have children. | |
| 123 return; | |
| 124 } | |
| 125 | |
| 126 var child = node.firstChild; | |
| 127 while (child !== null) { | |
|
Dan Beam
2016/07/01 21:32:35
nit: put a comment here about how doSearch() may a
dpapad
2016/07/01 21:59:42
Done.
| |
| 128 var nextSibling = child.nextSibling; | |
| 129 doSearch(child); | |
| 130 child = nextSibling; | |
| 131 } | |
| 132 | |
| 133 var shadowRoot = node.shadowRoot; | |
| 134 if (shadowRoot) | |
| 135 doSearch(shadowRoot); | |
| 136 } | |
| 137 | |
| 138 doSearch(page); | |
| 139 } | |
| 140 | |
| 141 /** | |
| 142 * Finds and makes visible the <settings-section> parent of |node|. | |
| 143 * @param {!Node} node | |
| 144 */ | |
| 145 function revealParentSection_(node) { | |
| 146 // Find corresponding SETTINGS-SECTION parent and make it visible. | |
| 147 var parent = node; | |
| 148 while (parent && parent.tagName !== 'SETTINGS-SECTION') { | |
| 149 parent = parent.nodeType == Node.DOCUMENT_FRAGMENT_NODE ? | |
| 150 parent.host : parent.parentNode; | |
| 151 } | |
| 152 if (parent) | |
| 153 parent.hidden = false; | |
| 154 } | |
| 155 | |
| 156 /** | |
| 157 * @param {!Element} page | |
| 158 * @param {boolean} visible | |
| 159 * @private | |
| 160 */ | |
| 161 function setSectionsVisibility_(page, visible) { | |
| 162 var sections = Polymer.dom(page.root).querySelectorAll('settings-section'); | |
| 163 for (var i = 0; i < sections.length; i++) | |
| 164 sections[i].hidden = !visible; | |
| 165 } | |
| 166 | |
| 167 /** | |
| 168 * Performs hierarchical search, starting at the given page element. | |
| 169 * @param {string} text | |
| 170 * @param {!Element} page Must be either <settings-basic-page> or | |
| 171 * <settings-advanced-page>. | |
| 172 */ | |
| 173 function search(text, page) { | |
| 174 findAndRemoveHighligts_(page); | |
| 175 | |
| 176 // Generate search text by applying lowercase and escaping any characters | |
| 177 // that would be problematic for regular expressions. | |
| 178 var searchText = text.trim().replace(SANITIZE_REGEX, '\\$&'); | |
| 179 if (searchText.length == 0) { | |
| 180 setSectionsVisibility_(page, true); | |
| 181 return; | |
| 182 } | |
| 183 | |
| 184 setSectionsVisibility_(page, false); | |
| 185 findAndHighlightMatches_(page, new RegExp('(' + searchText + ')', 'i')); | |
| 186 } | |
| 187 | |
| 188 return { | |
| 189 search: search, | |
| 190 }; | |
| 191 }); | |
| OLD | NEW |