You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@annotator.apache.org by ge...@apache.org on 2020/11/20 21:21:05 UTC

[incubator-annotator] 07/14: Refactor clip range to scope

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

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

commit 2f97989bc21f3fcf188c47976fbc1de0247a655d
Author: Gerben <ge...@treora.com>
AuthorDate: Fri Nov 20 12:09:15 2020 +0100

    Refactor clip range to scope
---
 packages/dom/src/chunker.ts                | 9 +++++++++
 packages/dom/src/text-position/describe.ts | 7 -------
 packages/dom/src/text-quote/describe.ts    | 7 -------
 3 files changed, 9 insertions(+), 14 deletions(-)

diff --git a/packages/dom/src/chunker.ts b/packages/dom/src/chunker.ts
index bb71857..8c28f78 100644
--- a/packages/dom/src/chunker.ts
+++ b/packages/dom/src/chunker.ts
@@ -114,6 +114,15 @@ export class TextNodeChunker implements Chunker<PartialTextNode> {
   }
 
   rangeToChunkRange(range: Range): ChunkRange<PartialTextNode> {
+    range = range.cloneRange();
+
+    // Take the part of the range that falls within the scope.
+    if (range.compareBoundaryPoints(Range.START_TO_START, this.scope) === -1)
+      range.setStart(this.scope.startContainer, this.scope.startOffset);
+    if (range.compareBoundaryPoints(Range.END_TO_END, this.scope) === 1)
+      range.setEnd(this.scope.endContainer, this.scope.endOffset);
+
+    // Ensure it starts and ends at text nodes.
     const textRange = normalizeRange(range, this.scope);
 
     const startChunk = this.nodeToChunk(textRange.startContainer);
diff --git a/packages/dom/src/text-position/describe.ts b/packages/dom/src/text-position/describe.ts
index a711410..d4099a9 100644
--- a/packages/dom/src/text-position/describe.ts
+++ b/packages/dom/src/text-position/describe.ts
@@ -38,13 +38,6 @@ export async function describeTextPosition(
     scope.selectNodeContents(document);
   }
 
-  // Take the part of the range that falls within the scope.
-  range = range.cloneRange();
-  if (range.compareBoundaryPoints(Range.START_TO_START, scope) === -1)
-    range.setStart(scope.startContainer, scope.startOffset);
-  if (range.compareBoundaryPoints(Range.END_TO_END, scope) === 1)
-    range.setEnd(scope.endContainer, scope.endOffset);
-
   const textChunks = new TextNodeChunker(scope);
   if (textChunks.currentChunk === null)
     throw new RangeError('Range does not contain any Text nodes.');
diff --git a/packages/dom/src/text-quote/describe.ts b/packages/dom/src/text-quote/describe.ts
index 2e4693e..688089f 100644
--- a/packages/dom/src/text-quote/describe.ts
+++ b/packages/dom/src/text-quote/describe.ts
@@ -38,13 +38,6 @@ export async function describeTextQuote(
     scope.selectNodeContents(document);
   }
 
-  // Take the part of the range that falls within the scope.
-  range = range.cloneRange();
-  if (range.compareBoundaryPoints(Range.START_TO_START, scope) === -1)
-    range.setStart(scope.startContainer, scope.startOffset);
-  if (range.compareBoundaryPoints(Range.END_TO_END, scope) === 1)
-    range.setEnd(scope.endContainer, scope.endOffset);
-
   const chunker = new TextNodeChunker(scope);
 
   return await abstractDescribeTextQuote(