Bug 1258964 - Part 2 - Make autocomplete emphasize overlapping matches. r=mak
authorGuillaume Maudoux <layus.on@gmail.com>
Thu, 31 Mar 2016 05:16:00 +0200
changeset 291361 5c2450c268e419f4e33393e8a60b6bf70e64f302
parent 291360 7de386dfe2bb8b3014a4ed51c8ee89de5659cbb9
child 291362 b9ad1239e4b24d82932da5794babb9ab98750400
push id19656
push usergwagner@mozilla.com
push dateMon, 04 Apr 2016 13:43:23 +0000
treeherderb2g-inbound@e99061fde28a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmak
bugs1258964
milestone48.0a1
Bug 1258964 - Part 2 - Make autocomplete emphasize overlapping matches. r=mak
toolkit/content/tests/chrome/test_autocomplete_emphasis.xul
toolkit/content/widgets/autocomplete.xml
--- a/toolkit/content/tests/chrome/test_autocomplete_emphasis.xul
+++ b/toolkit/content/tests/chrome/test_autocomplete_emphasis.xul
@@ -95,17 +95,17 @@ let testcases = [
     },
     { search: "tea two",
       result: "Tea for two, and two for tea...",
       emphasis: ["Tea", " for ", "two", ", and ", "two", " for ", "tea", "..."],
       emphasizeFirst: true
     },
     { search: "tat",
       result: "tatatat",
-      emphasis: ["tat", "a", "tat"],
+      emphasis: ["tatatat"],
       emphasizeFirst: true
     },
     { search: "cheval valise",
       result: "chevalise",
       emphasis: ["chevalise"],
       emphasizeFirst: true
     }
 ];
--- a/toolkit/content/widgets/autocomplete.xml
+++ b/toolkit/content/widgets/autocomplete.xml
@@ -1447,26 +1447,23 @@ extends="chrome://global/content/binding
           <![CDATA[
           // Short circuit for empty search ([""] == "")
           if (aSearchTokens == "")
             return [0, aText.length];
 
           // Find which regions of text match the search terms
           let regions = [];
           for (let search of Array.prototype.slice.call(aSearchTokens)) {
-            let matchIndex;
-            let startIndex = 0;
+            let matchIndex = -1;
             let searchLen = search.length;
 
             // Find all matches of the search terms, but stop early for perf
-            let lowerText = aText.toLowerCase().substr(0, this.boundaryCutoff);
-            while ((matchIndex = lowerText.indexOf(search, startIndex)) >= 0) {
-              // Start the next search from where this one finished
-              startIndex = matchIndex + searchLen;
-              regions.push([matchIndex, startIndex]);
+            let lowerText = aText.substr(0, this.boundaryCutoff).toLowerCase();
+            while ((matchIndex = lowerText.indexOf(search, matchIndex + 1)) >= 0) {
+              regions.push([matchIndex, matchIndex + searchLen]);
             }
           }
 
           // Sort the regions by start position then end position
           regions = regions.sort((a, b) => {
             let start = a[0] - b[0];
             return (start == 0) ? a[1] - b[1] : start;
           });