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 12:26:28 UTC
[incubator-annotator] 05/19: Factor out chunkRangeToRange
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 610ba79c9aa3e91e4217ef7e8a56bfba19da34de
Author: Gerben <ge...@treora.com>
AuthorDate: Mon Nov 16 23:13:59 2020 +0100
Factor out chunkRangeToRange
---
packages/dom/src/chunker.ts | 15 +++++++++++++++
packages/dom/src/text-position/match.ts | 6 +-----
packages/dom/src/text-quote/match.ts | 9 +--------
3 files changed, 17 insertions(+), 13 deletions(-)
diff --git a/packages/dom/src/chunker.ts b/packages/dom/src/chunker.ts
index 78b8646..f671b5e 100644
--- a/packages/dom/src/chunker.ts
+++ b/packages/dom/src/chunker.ts
@@ -116,6 +116,21 @@ export class TextNodeChunker implements Chunker<PartialTextNode> {
return { startChunk, startIndex, endChunk, endIndex };
}
+ chunkRangeToRange(chunkRange: ChunkRange<PartialTextNode>): Range {
+ const range = ownerDocument(this.scope).createRange();
+ // The `+…startOffset` parts are only relevant for the first chunk, as it
+ // might start within a text node.
+ range.setStart(
+ chunkRange.startChunk.node,
+ chunkRange.startIndex + chunkRange.startChunk.startOffset,
+ );
+ range.setEnd(
+ chunkRange.endChunk.node,
+ chunkRange.endIndex + chunkRange.endChunk.startOffset,
+ );
+ return range;
+ }
+
constructor(private scope: Range) {
this.iter = ownerDocument(scope).createNodeIterator(
scope.commonAncestorContainer,
diff --git a/packages/dom/src/text-position/match.ts b/packages/dom/src/text-position/match.ts
index 985e278..aa5fe49 100644
--- a/packages/dom/src/text-position/match.ts
+++ b/packages/dom/src/text-position/match.ts
@@ -19,7 +19,6 @@
*/
import type { Matcher, TextPositionSelector } from '@annotator/selector';
-import { ownerDocument } from '../owner-document';
import { TextSeeker, NonEmptyChunker } from '../seek';
import { CodePointSeeker } from '../code-point-seeker';
import { Chunk, ChunkRange, TextNodeChunker, PartialTextNode } from '../chunker';
@@ -39,10 +38,7 @@ export function createTextPositionSelectorMatcher(
const matches = abstractMatcher(textChunks as NonEmptyChunker<PartialTextNode>);
for await (const abstractMatch of matches) {
- const match = ownerDocument(scope).createRange();
- match.setStart(abstractMatch.startChunk.node, abstractMatch.startChunk.startOffset + abstractMatch.startIndex);
- match.setEnd(abstractMatch.endChunk.node, abstractMatch.endChunk.startOffset + abstractMatch.endIndex);
- yield match;
+ yield textChunks.chunkRangeToRange(abstractMatch);
}
};
}
diff --git a/packages/dom/src/text-quote/match.ts b/packages/dom/src/text-quote/match.ts
index f7c8da3..37a75ba 100644
--- a/packages/dom/src/text-quote/match.ts
+++ b/packages/dom/src/text-quote/match.ts
@@ -32,14 +32,7 @@ export function createTextQuoteSelectorMatcher(
const textChunks = new TextNodeChunker(scope);
for await (const abstractMatch of abstractMatcher(textChunks)) {
- const match = document.createRange();
- // The `+…startOffset` parts are only relevant for the first chunk, as it
- // might start within a text node.
- match.setStart(abstractMatch.startChunk.node,
- abstractMatch.startIndex + abstractMatch.startChunk.startOffset);
- match.setEnd(abstractMatch.endChunk.node,
- abstractMatch.endIndex + abstractMatch.endChunk.startOffset);
- yield match;
+ yield textChunks.chunkRangeToRange(abstractMatch);
}
}
}