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 f57efc7a68bd35ca6d39d6435bb41e3e8638eb7e Author: Gerben <[email protected]> AuthorDate: Fri May 22 19:04:34 2020 +0200 Simplify skipping of empty text nodes --- packages/dom/src/text-quote/match.ts | 28 ++++------------------------ 1 file changed, 4 insertions(+), 24 deletions(-) diff --git a/packages/dom/src/text-quote/match.ts b/packages/dom/src/text-quote/match.ts index cc1de71..3104ad3 100644 --- a/packages/dom/src/text-quote/match.ts +++ b/packages/dom/src/text-quote/match.ts @@ -40,10 +40,12 @@ export function createTextQuoteSelectorMatcher(selector: TextQuoteSelector): Dom root, NodeFilter.SHOW_TEXT, { - acceptNode: node => - range.intersectsNode(node) + acceptNode(node: Text) { + // Only reveal nodes within the range; and skip any empty text nodes. + return range.intersectsNode(node) && node.length > 0 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT + }, }, ); @@ -66,34 +68,12 @@ export function createTextQuoteSelectorMatcher(selector: TextQuoteSelector): Dom // Seek to the start of the match. referenceNodeIndex += seek(iter, matchStartIndex - referenceNodeIndex); - // Peek forward and skip over any empty nodes. - if (iter.nextNode()) { - while ((iter.referenceNode.nodeValue as String).length === 0) { - iter.nextNode(); - } - - // The iterator now points to the end of the reference node. - // Move the iterator back to the start of the reference node. - iter.previousNode(); - } - // Record the start container and offset. match.setStart(iter.referenceNode, matchStartIndex - referenceNodeIndex); // Seek to the end of the match. referenceNodeIndex += seek(iter, matchEndIndex - referenceNodeIndex); - // Peek forward and skip over any empty nodes. - if (iter.nextNode()) { - while ((iter.referenceNode.nodeValue as String).length === 0) { - iter.nextNode(); - } - - // The iterator now points to the end of the reference node. - // Move the iterator back to the start of the reference node. - iter.previousNode(); - } - // Record the end container and offset. match.setEnd(iter.referenceNode, matchEndIndex - referenceNodeIndex);
