This is an automated email from the ASF dual-hosted git repository.

gerben pushed a commit to branch dom-tests
in repository https://gitbox.apache.org/repos/asf/incubator-annotator.git

commit 6a09edf485d198c62807eb890284705fb075c466
Author: Gerben <[email protected]>
AuthorDate: Fri May 22 19:34:20 2020 +0200

    Remove superfluous seek, tidy up code
---
 packages/dom/src/text-quote/match.ts | 46 ++++++++++++++++--------------------
 1 file changed, 21 insertions(+), 25 deletions(-)

diff --git a/packages/dom/src/text-quote/match.ts 
b/packages/dom/src/text-quote/match.ts
index 3104ad3..f32afce 100644
--- a/packages/dom/src/text-quote/match.ts
+++ b/packages/dom/src/text-quote/match.ts
@@ -27,66 +27,62 @@ import { ownerDocument, rangeFromScope } from '../scope';
 export function createTextQuoteSelectorMatcher(selector: TextQuoteSelector): 
DomMatcher {
   return async function* matchAll(scope: DomScope) {
     const document = ownerDocument(scope);
-    const range = rangeFromScope(scope);
-    const root = range.commonAncestorContainer;
-    const text = range.toString();
+    const scopeAsRange = rangeFromScope(scope);
+    const scopeText = scopeAsRange.toString();
 
     const exact = selector.exact;
     const prefix = selector.prefix || '';
     const suffix = selector.suffix || '';
-    const pattern = prefix + exact + suffix;
+    const searchPattern = prefix + exact + suffix;
 
     const iter = document.createNodeIterator(
-      root,
+      scopeAsRange.commonAncestorContainer,
       NodeFilter.SHOW_TEXT,
       {
         acceptNode(node: Text) {
           // Only reveal nodes within the range; and skip any empty text nodes.
-          return range.intersectsNode(node) && node.length > 0
+          return scopeAsRange.intersectsNode(node) && node.length > 0
             ? NodeFilter.FILTER_ACCEPT
             : NodeFilter.FILTER_REJECT
         },
       },
     );
 
-    let fromIndex = 0;
-    let referenceNodeIndex = 0;
-
-    if (isTextNode(range.startContainer)) {
-      referenceNodeIndex -= range.startOffset;
-    }
+    // The index of the first character of iter.referenceNode inside the text.
+    let referenceNodeIndex = isTextNode(scopeAsRange.startContainer)
+      ? -scopeAsRange.startOffset
+      : 0;
 
-    while (fromIndex < text.length) {
-      const patternStartIndex = text.indexOf(pattern, fromIndex);
+    let fromIndex = 0;
+    while (fromIndex < scopeText.length) {
+      // Find the quote with its prefix and suffix in the string.
+      const patternStartIndex = scopeText.indexOf(searchPattern, fromIndex);
       if (patternStartIndex === -1) return;
 
-      const match = document.createRange();
-
+      // Correct for the prefix and suffix lengths.
       const matchStartIndex = patternStartIndex + prefix.length;
       const matchEndIndex = matchStartIndex + exact.length;
 
-      // Seek to the start of the match.
-      referenceNodeIndex += seek(iter, matchStartIndex - referenceNodeIndex);
+      // Create a range to represent this exact quote in the dom.
+      const match = document.createRange();
 
-      // Record the start container and offset.
+      // Seek to the start of the match, make the range start there.
+      referenceNodeIndex += seek(iter, matchStartIndex - referenceNodeIndex);
       match.setStart(iter.referenceNode, matchStartIndex - referenceNodeIndex);
 
-      // Seek to the end of the match.
+      // Seek to the end of the match, make the range end there.
       referenceNodeIndex += seek(iter, matchEndIndex - referenceNodeIndex);
-
-      // Record the end container and offset.
       match.setEnd(iter.referenceNode, matchEndIndex - referenceNodeIndex);
 
       // Yield the match.
       yield match;
 
-      // Advance the search forward.
+      // Advance the search forward to detect multiple occurrences.
       fromIndex = matchStartIndex + 1;
-      referenceNodeIndex += seek(iter, fromIndex - referenceNodeIndex);
     }
   };
 }
 
 function isTextNode(node: Node): node is Text {
-  return node.nodeType === Node.TEXT_NODE
+  return node.nodeType === Node.TEXT_NODE;
 }

Reply via email to