From 712794cd60608e8d02e6cd5c982bbf73a893f0f5 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Sat, 19 Apr 2014 01:32:35 +0100 Subject: Implement number of matches in find mode --- content_scripts/vimium_frontend.coffee | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index a2139df6..5ba6fa45 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -8,7 +8,7 @@ window.handlerStack = new HandlerStack insertModeLock = null findMode = false -findModeQuery = { rawQuery: "" } +findModeQuery = { rawQuery: "", matchCount: 0 } findModeQueryHasResults = false findModeAnchorNode = null isShowingHelpDialog = false @@ -554,6 +554,25 @@ updateFindModeQuery = -> text = document.body.innerText findModeQuery.regexMatches = text.match(pattern) findModeQuery.activeRegexIndex = 0 + findModeQuery.matchCount = findModeQuery.regexMatches?.length + else + # escape all special characters, so RegExp just parses the string + parsedNonRegexQuery = findModeQuery.parsedQuery.replace("\\", "\\\\") + .replace("^", "\\^") + .replace("$", "\\$") + .replace("*", "\\*") + .replace("+", "\\+") + .replace("?", "\\?") + .replace("(", "\\(") + .replace(")", "\\)") + .replace("|", "\\|") + .replace("{", "\\{") + .replace("}", "\\}") + .replace("[", "\\[") + .replace("]", "\\]") + pattern = new RegExp(parsedNonRegexQuery, "g" + (if findModeQuery.ignoreCase then "i" else "")) + text = document.body.innerText + findModeQuery.matchCount = text.match(pattern)?.length handleKeyCharForFindMode = (keyChar) -> findModeQuery.rawQuery += keyChar @@ -815,7 +834,7 @@ window.goNext = -> showFindModeHUDForQuery = -> if (findModeQueryHasResults || findModeQuery.parsedQuery.length == 0) - HUD.show("/" + findModeQuery.rawQuery) + HUD.show("/" + findModeQuery.rawQuery + " (" + findModeQuery.matchCount + " Matches)") else HUD.show("/" + findModeQuery.rawQuery + " (No Matches)") @@ -839,7 +858,7 @@ window.showHelpDialog = (html, fid) -> container.innerHTML = html container.getElementsByClassName("closeButton")[0].addEventListener("click", hideHelpDialog, false) - + VimiumHelpDialog = # This setting is pulled out of local storage. It's false by default. getShowAdvancedCommands: -> settings.get("helpDialog_showAdvancedCommands") -- cgit v1.2.3 From 0450bec1a279e8d67ea46950397f1de91b1b118c Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Sat, 19 Apr 2014 01:46:22 +0100 Subject: Add a comment explaining match counting for string searches --- content_scripts/vimium_frontend.coffee | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'content_scripts') diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 5ba6fa45..c7cff961 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -555,8 +555,11 @@ updateFindModeQuery = -> findModeQuery.regexMatches = text.match(pattern) findModeQuery.activeRegexIndex = 0 findModeQuery.matchCount = findModeQuery.regexMatches?.length + # if we are doing a basic plain string match, we still want to grep for matches of the string, so we can + # show a the number of results. We can grep on document.body.innerText, as it should be indistinguishable + # from the internal representation used by window.find. else - # escape all special characters, so RegExp just parses the string + # escape all special characters, so RegExp just parses the string 'as is'. parsedNonRegexQuery = findModeQuery.parsedQuery.replace("\\", "\\\\") .replace("^", "\\^") .replace("$", "\\$") -- cgit v1.2.3 From 348a4052fed6f06e41ab84f2aa33290d20fb5a06 Mon Sep 17 00:00:00 2001 From: mrmr1993 Date: Sat, 19 Apr 2014 03:51:49 +0100 Subject: Use RegExp to escape special characters for plain find queries --- content_scripts/vimium_frontend.coffee | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) (limited to 'content_scripts') diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index c7cff961..69e21a60 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -560,19 +560,9 @@ updateFindModeQuery = -> # from the internal representation used by window.find. else # escape all special characters, so RegExp just parses the string 'as is'. - parsedNonRegexQuery = findModeQuery.parsedQuery.replace("\\", "\\\\") - .replace("^", "\\^") - .replace("$", "\\$") - .replace("*", "\\*") - .replace("+", "\\+") - .replace("?", "\\?") - .replace("(", "\\(") - .replace(")", "\\)") - .replace("|", "\\|") - .replace("{", "\\{") - .replace("}", "\\}") - .replace("[", "\\[") - .replace("]", "\\]") + # Taken from http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex + escapeRegExp = /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g + parsedNonRegexQuery = findModeQuery.parsedQuery.replace(escapeRegExp, (char) -> "\\" + char) pattern = new RegExp(parsedNonRegexQuery, "g" + (if findModeQuery.ignoreCase then "i" else "")) text = document.body.innerText findModeQuery.matchCount = text.match(pattern)?.length -- cgit v1.2.3