diff options
| -rw-r--r-- | background_scripts/completion.coffee | 4 | ||||
| -rw-r--r-- | content_scripts/mode_find.coffee | 5 | ||||
| -rw-r--r-- | lib/utils.coffee | 6 |
3 files changed, 8 insertions, 7 deletions
diff --git a/background_scripts/completion.coffee b/background_scripts/completion.coffee index fb5a6120..2427bad8 100644 --- a/background_scripts/completion.coffee +++ b/background_scripts/completion.coffee @@ -762,8 +762,6 @@ RegexpCache = init: -> @initialized = true @clear() - # Taken from http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex - @escapeRegExp ||= /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g clear: -> @cache = {} @@ -777,7 +775,7 @@ RegexpCache = # TODO: `prefix` and `suffix` might be useful in richer word-relevancy scoring. get: (string, prefix="", suffix="") -> @init() unless @initialized - regexpString = string.replace(@escapeRegExp, "\\$&") + regexpString = Utils.escapeRegexSpecialCharacters string # Avoid cost of constructing new strings if prefix/suffix are empty (which is expected to be a common case). regexpString = prefix + regexpString if prefix regexpString = regexpString + suffix if suffix diff --git a/content_scripts/mode_find.coffee b/content_scripts/mode_find.coffee index 9b47cfbd..8c4286ae 100644 --- a/content_scripts/mode_find.coffee +++ b/content_scripts/mode_find.coffee @@ -132,10 +132,7 @@ class FindMode extends Mode # 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 'as is'. - # Taken from http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex - escapeRegExp = /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g - parsedNonRegexQuery = @query.parsedQuery.replace(escapeRegExp, (char) -> "\\" + char) + parsedNonRegexQuery = Utils.escapeRegexSpecialCharacters @query.parsedQuery pattern = new RegExp(parsedNonRegexQuery, "g" + (if @query.ignoreCase then "i" else "")) text = document.body.innerText @query.matchCount = text.match(pattern)?.length diff --git a/lib/utils.coffee b/lib/utils.coffee index 90469fad..e0b9ba36 100644 --- a/lib/utils.coffee +++ b/lib/utils.coffee @@ -19,6 +19,12 @@ Utils = func = obj[components.pop()] func.apply(obj, argArray) + # Escape all special characters, so RegExp will parse the string 'as is'. + # Taken from http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex + escapeRegexSpecialCharacters: do -> + escapeRegex = /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g + (str) -> str.replace escapeRegex, "\\$&" + escapeHtml: (string) -> string.replace(/</g, "<").replace(/>/g, ">") # Generates a unique ID |
