Cherry-picking #124 in order to fix crbug/1297157 in milestone 99

Co-authored-by: Ernesto Izquierdo Clua <eic@google.com>
diff --git a/src/text-fragment-utils.js b/src/text-fragment-utils.js
index 0ccd7e9..1de08d4 100644
--- a/src/text-fragment-utils.js
+++ b/src/text-fragment-utils.js
@@ -216,8 +216,11 @@
           potentialMatch.endContainer, potentialMatch.endOffset);
       textEndRange.setEnd(searchRange.endContainer, searchRange.endOffset);
 
-      let missingSuffix = false;
-      let missingTextEnd = false;
+      // Keep track of matches of the end term followed by suffix term
+      // (if needed).
+      // If no matches are found then there's no point in keeping looking for
+      // matches of the start term after the current start term occurrence.
+      let matchFound = false;
 
       // Search through the rest of the document to find a textEnd match. This
       // may take multiple iterations if a suffix needs to be found.
@@ -225,9 +228,9 @@
         const textEndMatch =
             findTextInRange(textFragment.textEnd, textEndRange);
         if (textEndMatch == null) {
-          missingTextEnd = true;
           break;
         }
+
         advanceRangeStartPastOffset(
             textEndRange, textEndMatch.startContainer,
             textEndMatch.startOffset);
@@ -241,9 +244,9 @@
           const suffixResult =
               checkSuffix(textFragment.suffix, potentialMatch, searchRange);
           if (suffixResult === CheckSuffixResult.NO_SUFFIX_MATCH) {
-            missingSuffix = true;
             break;
           } else if (suffixResult === CheckSuffixResult.SUFFIX_MATCH) {
+            matchFound = true;
             results.push(potentialMatch.cloneRange());
             continue;
           } else if (suffixResult === CheckSuffixResult.MISPLACED_SUFFIX) {
@@ -251,12 +254,13 @@
           }
         } else {
           // If we've found textEnd and there's no suffix, then it's a match!
+          matchFound = true;
           results.push(potentialMatch.cloneRange());
         }
       }
       // Stopping match search because suffix or textEnd are missing from the
       // rest of the search space.
-      if (missingSuffix || missingTextEnd) {
+      if (!matchFound) {
         break;
       }
 
diff --git a/test/ambiguous-match.html b/test/ambiguous-match.html
index a25e485..76a06b4 100644
--- a/test/ambiguous-match.html
+++ b/test/ambiguous-match.html
@@ -3,4 +3,5 @@
   prefix1 <i id="target1">target</i> suffix1 prefix2
   <i id="target2">target</i> suffix2 prefix1 <i id="target3">target</i> suffix2
   prefix2 <i id="target4">target</i> suffix1
+  <div>start start end suffix</div>
 </div>
diff --git a/test/text-fragment-utils-test.js b/test/text-fragment-utils-test.js
index b6efc9e..036d0a5 100644
--- a/test/text-fragment-utils-test.js
+++ b/test/text-fragment-utils-test.js
@@ -588,6 +588,9 @@
     result = utils.processTextFragmentDirective(fragment);
     expect(result.length).toEqual(2);
 
+    fragment = utils.forTesting.parseTextFragmentDirective('start,end');
+    result = utils.processTextFragmentDirective(fragment);
+    expect(result.length).toEqual(2);
 
     // prefix, textStart, + textEnd
     fragment =
@@ -601,6 +604,10 @@
     result = utils.processTextFragmentDirective(fragment);
     expect(result.length).toEqual(2);
 
+    fragment = utils.forTesting.parseTextFragmentDirective('start,end,-suffix');
+    result = utils.processTextFragmentDirective(fragment);
+    expect(result.length).toEqual(2);
+
     // all parts
     fragment = utils.forTesting.parseTextFragmentDirective(
         'prefix1-,target,target,-suffix2');