aboutsummaryrefslogtreecommitdiffstats
path: root/background_scripts
diff options
context:
space:
mode:
authorPhil Crosby2012-06-03 01:21:50 -0700
committerPhil Crosby2012-06-03 16:56:39 -0700
commita8e46be20fa8070f7975381efc22c5a72880da6c (patch)
treecbd60561d23f4c64892e641324a56d60331cb113 /background_scripts
parentb04a90e85ffb65d53429c0d482bff98c9a80151e (diff)
downloadvimium-a8e46be20fa8070f7975381efc22c5a72880da6c.tar.bz2
Fix a naive term highlighting function to handle multiple words correctly.
Wish this was shorter.
Diffstat (limited to 'background_scripts')
-rw-r--r--background_scripts/completion.coffee14
1 files changed, 13 insertions, 1 deletions
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 <span>.
highlightTerms: (string) ->
+ toReplace = {}
for term in @queryTerms
regexp = @escapeRegexp(term)
- string = string.replace(regexp, "<span class='match'>$&</span>")
+ 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) +
+ "<span class='match'>" + string.substr(i, length) + "</span>" +
+ string.substr(i + length)
string
# Creates a Regexp from the given string, with all special Regexp characters escaped.