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

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

Issue 2360853006: Keeps the original text node when search highlighting happens so it can later be restored. (Closed)
Patch Set: Created 4 years, 2 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 ORG_CONTENT_CSS_CLASS = 'search-highlight-original-content';
dpapad 2016/09/23 19:36:05 Nit: Probably better to stay away from non-establi
Moe 2016/09/23 23:22:54 Done.
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
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('.' + ORG_CONTENT_CSS_CLASS);
59 // For each hit element, remove the highlighting. 62 wrapper.parentElement.replaceChild(originalNode.firstChild, wrapper);
60 for (var hitElement of hitElements) {
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 } 63 }
74 64
75 var searchBubbles = node.querySelectorAll( 65 var searchBubbles = node.querySelectorAll(
76 '* /deep/ .' + SEARCH_BUBBLE_CSS_CLASS); 66 '* /deep/ .' + SEARCH_BUBBLE_CSS_CLASS);
77 for (var searchBubble of searchBubbles) 67 for (var searchBubble of searchBubbles)
78 searchBubble.remove(); 68 searchBubble.remove();
79 } 69 }
80 70
81 /** 71 /**
82 * Applies the highlight UI (yellow rectangle) around all matches in |node|. 72 * Applies the highlight UI (yellow rectangle) around all matches in |node|.
83 * @param {!Node} node The text node to be highlighted. |node| ends up 73 * @param {!Node} node The text node to be highlighted. |node| ends up
84 * being removed from the DOM tree. 74 * being removed from the DOM tree.
85 * @param {!Array<string>} tokens The string tokens after splitting on the 75 * @param {!Array<string>} tokens The string tokens after splitting on the
86 * relevant regExp. Even indices hold text that doesn't need highlighting, 76 * relevant regExp. Even indices hold text that doesn't need highlighting,
87 * odd indices hold the text to be highlighted. For example: 77 * odd indices hold the text to be highlighted. For example:
88 * var r = new RegExp('(foo)', 'i'); 78 * var r = new RegExp('(foo)', 'i');
89 * 'barfoobar foo bar'.split(r) => ['bar', 'foo', 'bar ', 'foo', ' bar'] 79 * 'barfoobar foo bar'.split(r) => ['bar', 'foo', 'bar ', 'foo', ' bar']
90 * @private 80 * @private
91 */ 81 */
92 function highlight_(node, tokens) { 82 function highlight_(node, tokens) {
93 var wrapper = document.createElement('span'); 83 var wrapper = document.createElement('span');
94 wrapper.classList.add(WRAPPER_CSS_CLASS); 84 wrapper.classList.add(WRAPPER_CSS_CLASS);
95 // Use existing node as placeholder to determine where to insert the 85 // Use existing node as placeholder to determine where to insert the
96 // replacement content. 86 // replacement content.
97 node.parentNode.replaceChild(wrapper, node); 87 node.parentNode.replaceChild(wrapper, node);
98 88
89 // Keep the existing node around for when the highlights are removed.
dpapad 2016/09/23 19:36:05 For future reference, explain in the comment that
Moe 2016/09/23 23:22:54 Done.
90 var span = document.createElement('span');
91 span.classList.add(ORG_CONTENT_CSS_CLASS);
92 span.style.display = 'none';
93 span.appendChild(node);
94 wrapper.appendChild(span);
95
99 for (var i = 0; i < tokens.length; ++i) { 96 for (var i = 0; i < tokens.length; ++i) {
100 if (i % 2 == 0) { 97 if (i % 2 == 0) {
101 wrapper.appendChild(document.createTextNode(tokens[i])); 98 wrapper.appendChild(document.createTextNode(tokens[i]));
102 } else { 99 } else {
103 var span = document.createElement('span'); 100 var span = document.createElement('span');
104 span.classList.add(HIT_CSS_CLASS); 101 span.classList.add(HIT_CSS_CLASS);
105 span.style.backgroundColor = '#ffeb3b'; // --var(--paper-yellow-500) 102 span.style.backgroundColor = '#ffeb3b'; // --var(--paper-yellow-500)
106 span.textContent = tokens[i]; 103 span.textContent = tokens[i];
107 wrapper.appendChild(span); 104 wrapper.appendChild(span);
108 } 105 }
(...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after
600 function setSearchManagerForTesting(searchManager) { 597 function setSearchManagerForTesting(searchManager) {
601 SearchManagerImpl.instance_ = searchManager; 598 SearchManagerImpl.instance_ = searchManager;
602 } 599 }
603 600
604 return { 601 return {
605 getSearchManager: getSearchManager, 602 getSearchManager: getSearchManager,
606 setSearchManagerForTesting: setSearchManagerForTesting, 603 setSearchManagerForTesting: setSearchManagerForTesting,
607 SearchRequest: SearchRequest, 604 SearchRequest: SearchRequest,
608 }; 605 };
609 }); 606 });
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698