| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 cr.define('settings', function() { | 5 cr.define('settings', function() { |
| 6 /** @const {string} */ | 6 /** @const {string} */ |
| 7 var WRAPPER_CSS_CLASS = 'search-highlight-wrapper'; | 7 var WRAPPER_CSS_CLASS = 'search-highlight-wrapper'; |
| 8 | 8 |
| 9 /** @const {string} */ | 9 /** @const {string} */ |
| 10 var ORIGINAL_CONTENT_CSS_CLASS = 'search-highlight-original-content'; |
| 11 |
| 12 /** @const {string} */ |
| 10 var HIT_CSS_CLASS = 'search-highlight-hit'; | 13 var HIT_CSS_CLASS = 'search-highlight-hit'; |
| 11 | 14 |
| 12 /** @const {string} */ | 15 /** @const {string} */ |
| 13 var SEARCH_BUBBLE_CSS_CLASS = 'search-bubble'; | 16 var SEARCH_BUBBLE_CSS_CLASS = 'search-bubble'; |
| 14 | 17 |
| 15 /** | 18 /** |
| 16 * A CSS attribute indicating that a node should be ignored during searching. | 19 * A CSS attribute indicating that a node should be ignored during searching. |
| 17 * @const {string} | 20 * @const {string} |
| 18 */ | 21 */ |
| 19 var SKIP_SEARCH_CSS_ATTRIBUTE = 'no-search'; | 22 var SKIP_SEARCH_CSS_ATTRIBUTE = 'no-search'; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 38 'PAPER-ITEM', | 41 'PAPER-ITEM', |
| 39 'PAPER-RIPPLE', | 42 'PAPER-RIPPLE', |
| 40 'PAPER-SLIDER', | 43 'PAPER-SLIDER', |
| 41 'PAPER-SPINNER', | 44 'PAPER-SPINNER', |
| 42 'STYLE', | 45 'STYLE', |
| 43 'TEMPLATE', | 46 'TEMPLATE', |
| 44 ]); | 47 ]); |
| 45 | 48 |
| 46 /** | 49 /** |
| 47 * Finds all previous highlighted nodes under |node| (both within self and | 50 * Finds all previous highlighted nodes under |node| (both within self and |
| 48 * children's Shadow DOM) and removes the highlights (yellow rectangle and | 51 * children's Shadow DOM) and replaces the highlights (yellow rectangle and |
| 49 * search bubbles). | 52 * search bubbles) with the original text node. |
| 50 * TODO(dpapad): Consider making this a private method of TopLevelSearchTask. | 53 * TODO(dpapad): Consider making this a private method of TopLevelSearchTask. |
| 51 * @param {!Node} node | 54 * @param {!Node} node |
| 52 * @private | 55 * @private |
| 53 */ | 56 */ |
| 54 function findAndRemoveHighlights_(node) { | 57 function findAndRemoveHighlights_(node) { |
| 55 var wrappers = node.querySelectorAll('* /deep/ .' + WRAPPER_CSS_CLASS); | 58 var wrappers = node.querySelectorAll('* /deep/ .' + WRAPPER_CSS_CLASS); |
| 56 | 59 |
| 57 for (var wrapper of wrappers) { | 60 for (var wrapper of wrappers) { |
| 58 var hitElements = wrapper.querySelectorAll('.' + HIT_CSS_CLASS); | 61 var originalNode = wrapper.querySelector( |
| 59 // For each hit element, remove the highlighting. | 62 '.' + ORIGINAL_CONTENT_CSS_CLASS); |
| 60 for (var hitElement of hitElements) { | 63 wrapper.parentElement.replaceChild(originalNode.firstChild, wrapper); |
| 61 wrapper.replaceChild(hitElement.firstChild, hitElement); | |
| 62 } | |
| 63 | |
| 64 // Normalize so that adjacent text nodes will be combined. | |
| 65 wrapper.normalize(); | |
| 66 // Restore the DOM structure as it was before the search occurred. | |
| 67 if (wrapper.previousSibling) | |
| 68 wrapper.textContent = ' ' + wrapper.textContent; | |
| 69 if (wrapper.nextSibling) | |
| 70 wrapper.textContent = wrapper.textContent + ' '; | |
| 71 | |
| 72 wrapper.parentElement.replaceChild(wrapper.firstChild, wrapper); | |
| 73 } | 64 } |
| 74 | 65 |
| 75 var searchBubbles = node.querySelectorAll( | 66 var searchBubbles = node.querySelectorAll( |
| 76 '* /deep/ .' + SEARCH_BUBBLE_CSS_CLASS); | 67 '* /deep/ .' + SEARCH_BUBBLE_CSS_CLASS); |
| 77 for (var searchBubble of searchBubbles) | 68 for (var searchBubble of searchBubbles) |
| 78 searchBubble.remove(); | 69 searchBubble.remove(); |
| 79 } | 70 } |
| 80 | 71 |
| 81 /** | 72 /** |
| 82 * Applies the highlight UI (yellow rectangle) around all matches in |node|. | 73 * Applies the highlight UI (yellow rectangle) around all matches in |node|. |
| 83 * @param {!Node} node The text node to be highlighted. |node| ends up | 74 * @param {!Node} node The text node to be highlighted. |node| ends up |
| 84 * being removed from the DOM tree. | 75 * being removed from the DOM tree. |
| 85 * @param {!Array<string>} tokens The string tokens after splitting on the | 76 * @param {!Array<string>} tokens The string tokens after splitting on the |
| 86 * relevant regExp. Even indices hold text that doesn't need highlighting, | 77 * relevant regExp. Even indices hold text that doesn't need highlighting, |
| 87 * odd indices hold the text to be highlighted. For example: | 78 * odd indices hold the text to be highlighted. For example: |
| 88 * var r = new RegExp('(foo)', 'i'); | 79 * var r = new RegExp('(foo)', 'i'); |
| 89 * 'barfoobar foo bar'.split(r) => ['bar', 'foo', 'bar ', 'foo', ' bar'] | 80 * 'barfoobar foo bar'.split(r) => ['bar', 'foo', 'bar ', 'foo', ' bar'] |
| 90 * @private | 81 * @private |
| 91 */ | 82 */ |
| 92 function highlight_(node, tokens) { | 83 function highlight_(node, tokens) { |
| 93 var wrapper = document.createElement('span'); | 84 var wrapper = document.createElement('span'); |
| 94 wrapper.classList.add(WRAPPER_CSS_CLASS); | 85 wrapper.classList.add(WRAPPER_CSS_CLASS); |
| 95 // Use existing node as placeholder to determine where to insert the | 86 // Use existing node as placeholder to determine where to insert the |
| 96 // replacement content. | 87 // replacement content. |
| 97 node.parentNode.replaceChild(wrapper, node); | 88 node.parentNode.replaceChild(wrapper, node); |
| 98 | 89 |
| 90 // Keep the existing node around for when the highlights are removed. The |
| 91 // existing text node might be involved in data-binding and therefore should |
| 92 // not be discarded. |
| 93 var span = document.createElement('span'); |
| 94 span.classList.add(ORIGINAL_CONTENT_CSS_CLASS); |
| 95 span.style.display = 'none'; |
| 96 span.appendChild(node); |
| 97 wrapper.appendChild(span); |
| 98 |
| 99 for (var i = 0; i < tokens.length; ++i) { | 99 for (var i = 0; i < tokens.length; ++i) { |
| 100 if (i % 2 == 0) { | 100 if (i % 2 == 0) { |
| 101 wrapper.appendChild(document.createTextNode(tokens[i])); | 101 wrapper.appendChild(document.createTextNode(tokens[i])); |
| 102 } else { | 102 } else { |
| 103 var span = document.createElement('span'); | 103 var span = document.createElement('span'); |
| 104 span.classList.add(HIT_CSS_CLASS); | 104 span.classList.add(HIT_CSS_CLASS); |
| 105 span.style.backgroundColor = '#ffeb3b'; // --var(--paper-yellow-500) | 105 span.style.backgroundColor = '#ffeb3b'; // --var(--paper-yellow-500) |
| 106 span.textContent = tokens[i]; | 106 span.textContent = tokens[i]; |
| 107 wrapper.appendChild(span); | 107 wrapper.appendChild(span); |
| 108 } | 108 } |
| (...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 600 function setSearchManagerForTesting(searchManager) { | 600 function setSearchManagerForTesting(searchManager) { |
| 601 SearchManagerImpl.instance_ = searchManager; | 601 SearchManagerImpl.instance_ = searchManager; |
| 602 } | 602 } |
| 603 | 603 |
| 604 return { | 604 return { |
| 605 getSearchManager: getSearchManager, | 605 getSearchManager: getSearchManager, |
| 606 setSearchManagerForTesting: setSearchManagerForTesting, | 606 setSearchManagerForTesting: setSearchManagerForTesting, |
| 607 SearchRequest: SearchRequest, | 607 SearchRequest: SearchRequest, |
| 608 }; | 608 }; |
| 609 }); | 609 }); |
| OLD | NEW |