From a8e46be20fa8070f7975381efc22c5a72880da6c Mon Sep 17 00:00:00 2001 From: Phil Crosby Date: Sun, 3 Jun 2012 01:21:50 -0700 Subject: Fix a naive term highlighting function to handle multiple words correctly. Wish this was shorter. --- background_scripts/completion.coffee | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'background_scripts') diff --git a/background_scripts/completion.coffee b/background_scripts/completion.coffee index 47ab1ac4..41ebeab8 100644 --- a/background_scripts/completion.coffee +++ b/background_scripts/completion.coffee @@ -26,9 +26,21 @@ class Suggestion # Wraps each occurence of the query terms in the given string in a . highlightTerms: (string) -> + toReplace = {} for term in @queryTerms regexp = @escapeRegexp(term) - string = string.replace(regexp, "$&") + i = string.search(regexp) + toReplace[i] = term.length if i >= 0 && (!toReplace[i] || toReplace[i].length < term.length) + + indices = [] + indices.push([key, toReplace[key]]) for key of toReplace + indices.sort (a, b) -> b - a + for [i, length] in indices + i = +i # convert i from String to Integer. + string = + string.substr(0, i) + + "" + string.substr(i, length) + "" + + string.substr(i + length) string # Creates a Regexp from the given string, with all special Regexp characters escaped. -- cgit v1.2.3