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/10/15 22:36:43 UTC
[incubator-annotator] 03/04: clean up
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 502baf9921689dfb7ccd9ec2b21eef8fa94f2d61
Author: Gerben <ge...@treora.com>
AuthorDate: Thu Oct 15 22:55:01 2020 +0200
clean up
---
packages/dom/src/seek.ts | 74 ++++++++++++++++++------------------------------
1 file changed, 27 insertions(+), 47 deletions(-)
diff --git a/packages/dom/src/seek.ts b/packages/dom/src/seek.ts
index 455168c..7d18037 100644
--- a/packages/dom/src/seek.ts
+++ b/packages/dom/src/seek.ts
@@ -2,28 +2,23 @@ import { ownerDocument } from "./owner-document";
const E_END = 'Iterator exhausted before seek ended.';
-interface BoundaryPointer {
- readonly referenceNode: Node;
+interface BoundaryPointer<T extends any = Node> {
+ readonly referenceNode: T;
readonly offsetInReferenceNode: number;
}
-interface TextBoundaryPointer extends BoundaryPointer{
- readonly referenceNode: Text;
- readonly offsetInReferenceNode: number;
-}
-
-interface Chunker<T extends Iterable<any>> {
+interface Chunker<T extends Iterable<any> = string> {
read1(): T;
}
-interface Seeker<T extends Iterable<any>> extends Chunker<T> {
+interface Seeker<T extends Iterable<any> = string> extends Chunker<T> {
readonly position: number;
read(length: number): T;
seekBy(length: number): void;
seekTo(target: number): void;
}
-export class Seeker_ implements Seeker<string>, TextBoundaryPointer {
+export class TextSeeker implements Seeker, BoundaryPointer<Text> {
// The node containing our current text position.
get referenceNode(): Text {
// The NodeFilter will guarantee this is a Text node (except before the
@@ -35,26 +30,15 @@ export class Seeker_ implements Seeker<string>, TextBoundaryPointer {
offsetInReferenceNode = 0;
// The index of the first character of iter.referenceNode inside the text.
- // get referenceNodePosition() { return this.position - this.offsetInReferenceNode; }
private referenceNodePosition = 0;
// The current text position, i.e. the number of code units passed so far.
// position = 0;
get position() { return this.referenceNodePosition + this.offsetInReferenceNode; }
- // // The number of code points passed so far.
- // codePointCount = 0;
-
private iter: NodeIterator;
- // // Counting code points is optional, to save the effort when it is not required.
- // private countCodePoints: boolean;
-
- constructor(scope: Range, options: {
- // countCodePoints?: boolean
- } = {}) {
- // this.countCodePoints = options.countCodePoints ?? false;
-
+ constructor(scope: Range) {
this.iter = ownerDocument(scope).createNodeIterator(
scope.commonAncestorContainer,
NodeFilter.SHOW_TEXT,
@@ -74,14 +58,13 @@ export class Seeker_ implements Seeker<string>, TextBoundaryPointer {
}
// TODO Handle the scope.endOffset as well, and fix behaviour in edge cases
// (e.g. any use of referenceNode.length is incorrect at the edges).
+ // Or rather, just extract this Range stuff into a Chunker, that cuts off
+ // those edges that fall outside the scope.
// Walk to the start of the first non-empty text node inside the scope.
this.seekTo(0);
}
- // seekCodePoints(count: number) {
- // }
-
read(length: number) {
return this._readOrSeekTo(true, this.position + length);
}
@@ -115,8 +98,6 @@ export class Seeker_ implements Seeker<string>, TextBoundaryPointer {
const oldOffset = this.offsetInReferenceNode;
this.offsetInReferenceNode = target - this.referenceNodePosition;
if (read) result += this.referenceNode.data.substring(oldOffset, this.offsetInReferenceNode);
- // if (this.countCodePoints)
- // this.codePointCount += [...this.referenceNode.data.substring(oldOffset, this.offsetInReferenceNode)].length;
break;
}
@@ -127,13 +108,9 @@ export class Seeker_ implements Seeker<string>, TextBoundaryPointer {
if (nextNode !== null) {
this.referenceNodePosition += curNode.length;
this.offsetInReferenceNode = 0;
- // if (this.countCodePoints)
- // this.codePointCount += [...curNode.data.substring(curOffset)].length;
} else {
// There is no next node. Finish at the end of the last node.
this.offsetInReferenceNode = this.referenceNode.length;
- // if (this.countCodePoints)
- // this.codePointCount += [...this.referenceNode.data.substring(this.offsetInReferenceNode)].length;
// Either the end of this node is our target, or the seek failed.
if (this.position === target)
break;
@@ -151,18 +128,14 @@ export class Seeker_ implements Seeker<string>, TextBoundaryPointer {
while (this.position > target) {
if (this.referenceNodePosition <= target) {
this.offsetInReferenceNode = target - this.referenceNodePosition;
- // if (this.countCodePoints)
- // this.codePointCount -= [...this.referenceNode.data.substring(this.offsetInReferenceNode, oldOffset)].length;
break;
}
// Move to the end of the previous node.
- // const curNode = this.referenceNode;
const prevNode = this.iter.previousNode();
if (prevNode !== null) {
this.referenceNodePosition -= this.referenceNode.length;
this.offsetInReferenceNode = this.referenceNode.length;
- // if (this.countCodePoints)
// this.codePointCount -= [...curNode.data].length;
} else {
this.offsetInReferenceNode = 0;
@@ -173,21 +146,12 @@ export class Seeker_ implements Seeker<string>, TextBoundaryPointer {
}
}
-function isText(node: Node): node is Text {
- return node.nodeType === Node.TEXT_NODE;
-}
+class _CharSeeker implements Seeker<string[]> {
+ position = 0;
-class CharSeeker implements Seeker<String[]>, TextBoundaryPointer {
- constructor(public readonly raw: Seeker<String> & TextBoundaryPointer) {
+ constructor(public readonly raw: Seeker<string>) {
}
- position = 0;
- get referenceNode() { return this.raw.referenceNode };
- get offsetInReferenceNode() {
- const substring = this.referenceNode.data.substring(0, this.raw.offsetInReferenceNode);
- return [...substring].length;
- };
-
seekBy(length: number) {
return this.seekTo(this.position + length);
}
@@ -222,3 +186,19 @@ class CharSeeker implements Seeker<String[]>, TextBoundaryPointer {
if (read) return result;
}
}
+
+export class CharSeeker extends _CharSeeker implements Seeker<string[]>, BoundaryPointer<string[]> {
+ constructor(public readonly raw: Seeker<string> & BoundaryPointer<Text>) {
+ super(raw);
+ }
+
+ get referenceNode() { return [...this.raw.referenceNode.data] };
+ get offsetInReferenceNode() {
+ const substring = this.raw.referenceNode.data.substring(0, this.raw.offsetInReferenceNode);
+ return [...substring].length;
+ };
+}
+
+function isText(node: Node): node is Text {
+ return node.nodeType === Node.TEXT_NODE;
+}