Commit c16433ac authored by Yi Su's avatar Yi Su Committed by Commit Bot

Add comments, rename variables and other trivial fixes for

find_in_page.js.

This CL fixes remaining trivial problems like comments, naming issues of
variables and functions, and makes a small change on DFS.

Bug: 228585
Change-Id: I2df40f675e86fea099fedb987d815b00307417ec
Reviewed-on: https://chromium-review.googlesource.com/c/1355119
Commit-Queue: Yi Su <mrsuyi@chromium.org>
Reviewed-by: default avatarPeter Lee <pkl@chromium.org>
Cr-Commit-Position: refs/heads/master@{#612197}
parent 6b3b4182
...@@ -147,6 +147,11 @@ let matchId_ = 0; ...@@ -147,6 +147,11 @@ let matchId_ = 0;
* each PartialMatch. * each PartialMatch.
*/ */
class PartialMatch { class PartialMatch {
/**
* @param {number} matchId ID of the Match to which this PartialMatch belongs.
* @param {number} begin Beginning index of partial match text in |allText_|.
* @param {number} end Ending index of partial match text in |allText_|.
*/
constructor(matchId, begin, end) { constructor(matchId, begin, end) {
this.matchId = matchId; this.matchId = matchId;
this.begin = begin; this.begin = begin;
...@@ -169,8 +174,9 @@ let partialMatches_ = []; ...@@ -169,8 +174,9 @@ let partialMatches_ = [];
*/ */
class Replacement { class Replacement {
/** /**
* @param {Node} The HTML Node containing search result. * @param {Node} oldNode The HTML Node containing search result.
* @param {Array<Node>} New HTML Nodes created for substitution of |oldNode|. * @param {Array<Node>} newNodes New HTML Nodes created for substitution of
* |oldNode|.
*/ */
constructor(oldNode, newNodes) { constructor(oldNode, newNodes) {
this.oldNode = oldNode; this.oldNode = oldNode;
...@@ -337,12 +343,21 @@ const CSS_STYLE_ID = '__gCrWeb.findInPageStyle'; ...@@ -337,12 +343,21 @@ const CSS_STYLE_ID = '__gCrWeb.findInPageStyle';
*/ */
const NO_RESULTS = '[0,[0,0,0]]'; const NO_RESULTS = '[0,[0,0,0]]';
/**
* Result passed back to app to indicate pumpSearch has reached timeout.
* @type {string}
*/
const TIMEOUT = '[false]';
/** /**
* Regex to escape regex special characters in a string. * Regex to escape regex special characters in a string.
* @type {RegExp} * @type {RegExp}
*/ */
const REGEX_ESCAPER = /([.?*+^$[\]\\(){}|-])/g; const REGEX_ESCAPER = /([.?*+^$[\]\\(){}|-])/g;
/**
* @return {Match} The currently selected Match.
*/
function getCurrentSelectedMatch_() { function getCurrentSelectedMatch_() {
return __gCrWeb.findInPage.matches[__gCrWeb.findInPage.selectedMatchIndex]; return __gCrWeb.findInPage.matches[__gCrWeb.findInPage.selectedMatchIndex];
}; };
...@@ -387,16 +402,13 @@ class Timer { ...@@ -387,16 +402,13 @@ class Timer {
__gCrWeb.findInPage.highlightWord = function(findText, timeout) { __gCrWeb.findInPage.highlightWord = function(findText, timeout) {
if (__gCrWeb.findInPage.matches && __gCrWeb.findInPage.matches.length) { if (__gCrWeb.findInPage.matches && __gCrWeb.findInPage.matches.length) {
// Clean up a previous run. // Clean up a previous run.
clearHighlight_(); cleanUp_();
} }
if (!findText) { if (!findText) {
// No searching for emptyness. // No searching for emptyness.
return NO_RESULTS; return NO_RESULTS;
} }
// Node is what we are currently looking at.
__gCrWeb.findInPage.node = document.body;
// Holds what nodes we have not processed yet. // Holds what nodes we have not processed yet.
__gCrWeb.findInPage.stack = []; __gCrWeb.findInPage.stack = [];
...@@ -405,6 +417,7 @@ __gCrWeb.findInPage.highlightWord = function(findText, timeout) { ...@@ -405,6 +417,7 @@ __gCrWeb.findInPage.highlightWord = function(findText, timeout) {
let doc = frameDocs_[i]; let doc = frameDocs_[i];
__gCrWeb.findInPage.stack.push(doc); __gCrWeb.findInPage.stack.push(doc);
} }
__gCrWeb.findInPage.stack.push(document.body);
// Number of visible elements found. // Number of visible elements found.
__gCrWeb.findInPage.visibleFound = 0; __gCrWeb.findInPage.visibleFound = 0;
...@@ -431,7 +444,7 @@ __gCrWeb.findInPage.highlightWord = function(findText, timeout) { ...@@ -431,7 +444,7 @@ __gCrWeb.findInPage.highlightWord = function(findText, timeout) {
* 4. Check the visibility of each Match; * 4. Check the visibility of each Match;
* 5. Call __gCrWeb.findInPage.goNext. * 5. Call __gCrWeb.findInPage.goNext.
* *
* If |timeout| has been reached, the function will return '[false]', and the * If |timeout| has been reached, the function will return TIMEOUT, and the
* caller need to call this function again to continue searching. This prevents * caller need to call this function again to continue searching. This prevents
* the Js thread from blocking the WebView's UI. * the Js thread from blocking the WebView's UI.
* *
...@@ -447,8 +460,8 @@ __gCrWeb.findInPage.pumpSearch = function(timeout) { ...@@ -447,8 +460,8 @@ __gCrWeb.findInPage.pumpSearch = function(timeout) {
let timer = new Timer(timeout); let timer = new Timer(timeout);
// Go through every node in DFS fashion. // Go through every node in DFS fashion.
while (__gCrWeb.findInPage.node) { while (__gCrWeb.findInPage.stack.length) {
let node = __gCrWeb.findInPage.node; let node = __gCrWeb.findInPage.stack.pop();
let children = node.childNodes; let children = node.childNodes;
if (children && children.length) { if (children && children.length) {
// add all (reasonable) children // add all (reasonable) children
...@@ -469,13 +482,7 @@ __gCrWeb.findInPage.pumpSearch = function(timeout) { ...@@ -469,13 +482,7 @@ __gCrWeb.findInPage.pumpSearch = function(timeout) {
} }
if (timer.overtime()) if (timer.overtime())
return '[false]'; return TIMEOUT;
if (__gCrWeb.findInPage.stack.length > 0) {
__gCrWeb.findInPage.node = __gCrWeb.findInPage.stack.pop();
} else {
__gCrWeb.findInPage.node = null;
}
} }
// Do regex match in |allText_|, create |matches| and |replacements|. The // Do regex match in |allText_|, create |matches| and |replacements|. The
...@@ -518,7 +525,7 @@ __gCrWeb.findInPage.pumpSearch = function(timeout) { ...@@ -518,7 +525,7 @@ __gCrWeb.findInPage.pumpSearch = function(timeout) {
++matchId_; ++matchId_;
if (timer.overtime()) if (timer.overtime())
return '[false]'; return TIMEOUT;
} }
// Process remaining PartialMatches. // Process remaining PartialMatches.
processPartialMatchesInCurrentSection(); processPartialMatchesInCurrentSection();
...@@ -529,7 +536,7 @@ __gCrWeb.findInPage.pumpSearch = function(timeout) { ...@@ -529,7 +536,7 @@ __gCrWeb.findInPage.pumpSearch = function(timeout) {
for (let i = replacementsIndex_; i < replacements_.length; ++i) { for (let i = replacementsIndex_; i < replacements_.length; ++i) {
if (timer.overtime()) { if (timer.overtime()) {
replacementsIndex_ = i; replacementsIndex_ = i;
return __gCrWeb.stringify([false]); return TIMEOUT;
} }
replacements_[i].doSwap(); replacements_[i].doSwap();
} }
...@@ -541,7 +548,7 @@ __gCrWeb.findInPage.pumpSearch = function(timeout) { ...@@ -541,7 +548,7 @@ __gCrWeb.findInPage.pumpSearch = function(timeout) {
let match = __gCrWeb.findInPage.matches[index]; let match = __gCrWeb.findInPage.matches[index];
if (timer.overtime()) { if (timer.overtime()) {
__gCrWeb.findInPage.visibleIndex = index; __gCrWeb.findInPage.visibleIndex = index;
return __gCrWeb.stringify([false]); return TIMEOUT;
} }
// Stop after |maxVisible| elements. // Stop after |maxVisible| elements.
...@@ -567,22 +574,25 @@ __gCrWeb.findInPage.pumpSearch = function(timeout) { ...@@ -567,22 +574,25 @@ __gCrWeb.findInPage.pumpSearch = function(timeout) {
}; };
/** /**
* Removes all currently highlighted matches. * Removes highlights of previous search and reset all global vars.
* Note: It does not restore previous state, just removes the class name. * @return {undefined}
*/ */
function clearHighlight_() { function cleanUp_() {
for (let i = 0; i < replacements_.length; ++i) { for (let i = 0; i < replacements_.length; ++i) {
replacements_[i].undoSwap(); replacements_[i].undoSwap();
} }
replacements_ = [];
replacementsIndex_ = 0;
__gCrWeb.findInPage.matches = [];
__gCrWeb.findInPage.selectedMatchIndex = -1;
allText_ = ''; allText_ = '';
sections_ = []; sections_ = [];
sectionsIndex_ = 0; sectionsIndex_ = 0;
__gCrWeb.findInPage.matches = [];
__gCrWeb.findInPage.selectedMatchIndex = -1;
matchId_ = 0; matchId_ = 0;
partialMatches_ = []; partialMatches_ = [];
replacements_ = [];
replacementsIndex_ = 0;
}; };
/** /**
...@@ -836,7 +846,7 @@ function removeDocumentStyle_(thisDocument) { ...@@ -836,7 +846,7 @@ function removeDocumentStyle_(thisDocument) {
__gCrWeb.findInPage.disable = function() { __gCrWeb.findInPage.disable = function() {
if (styleElement_) { if (styleElement_) {
removeStyle_(); removeStyle_();
window.setTimeout(clearHighlight_, 0); window.setTimeout(cleanUp_, 0);
} }
__gCrWeb.findInPage.hasInitialized = false; __gCrWeb.findInPage.hasInitialized = false;
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment