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 23aac8df61f96cb49d635f93da250fb45e19f53d Author: Gerben <[email protected]> AuthorDate: Thu Oct 29 17:36:16 2020 +0100 Clean up interfaces --- packages/dom/src/chunker.ts | 26 ++++++++++++++++---------- packages/dom/src/seek.ts | 12 ++++++------ 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/packages/dom/src/chunker.ts b/packages/dom/src/chunker.ts index 2480911..82a72e3 100644 --- a/packages/dom/src/chunker.ts +++ b/packages/dom/src/chunker.ts @@ -20,21 +20,27 @@ import { ownerDocument } from "./owner-document"; +// A Chunk represents a fragment (typically a string) of some document. +// Subclasses can add further attributes to map the chunk to its position in the +// data structure it came from (e.g. a DOM node). export interface Chunk<TData extends any> { - // A Chunk has a primary value (typically a string), and any other info that one may want to add to it. readonly data: TData; - - // The initial idea was that a Chunk is any toString-able object. Should suffice for us. - // But it would not let one create e.g. a Chunk with an array of unicode characters. - // toString(): string; } +// A Chunker lets one walk through the chunks of a document. +// It is inspired by, and similar to, the DOM’s NodeIterator. (but unlike +// NodeIterator, it has no concept of being ‘before’ or ‘after’ a chunk) export interface Chunker<TChunk extends Chunk<any>> { // currentChunk is null only if it contains no chunks at all. readonly currentChunk: TChunk | null; - readNext(): TChunk | null; - readPrev(): TChunk | null; - // read(length?: 1 | -1, roundUp?: true): TChunk | null; + + // Move currentChunk to the chunk following it, and return that chunk. + // If there are no chunks following it, keep currentChunk unchanged and return null. + nextChunk(): TChunk | null; + + // Move currentChunk to the chunk preceding it, and return that chunk. + // If there are no preceding chunks, keep currentChunk unchanged and return null. + previousChunk(): TChunk | null; } export interface PartialTextNode extends Chunk<string> { @@ -81,7 +87,7 @@ export class TextNodeChunker implements Chunker<PartialTextNode> { this.iter.nextNode(); } - readNext() { + nextChunk() { // Move the iterator to after the current node, so nextNode() will cause a jump. if (this.iter.pointerBeforeReferenceNode) this.iter.nextNode(); @@ -91,7 +97,7 @@ export class TextNodeChunker implements Chunker<PartialTextNode> { return null; } - readPrev() { + previousChunk() { if (!this.iter.pointerBeforeReferenceNode) this.iter.previousNode(); if (this.iter.previousNode()) diff --git a/packages/dom/src/seek.ts b/packages/dom/src/seek.ts index 67a7adc..94bb5b1 100644 --- a/packages/dom/src/seek.ts +++ b/packages/dom/src/seek.ts @@ -24,8 +24,8 @@ const E_END = 'Iterator exhausted before seek ended.'; interface NonEmptyChunker<TChunk extends Chunk<any>> { readonly currentChunk: TChunk; - readNext(): TChunk | null; - readPrev(): TChunk | null; + nextChunk(): TChunk | null; + previousChunk(): TChunk | null; } export interface BoundaryPointer<T extends any> { @@ -91,11 +91,11 @@ class _TextSeeker<TChunk extends Chunk<string>> implements Seeker<string> { // Move to the start of the next chunk, while counting the characters of the current one. if (read) result += this.chunker.currentChunk.data.substring(this.offsetInChunk); const chunkLength = this.chunker.currentChunk.data.length; - let nextChunk = this.chunker.readNext(); + let nextChunk = this.chunker.nextChunk(); if (nextChunk !== null) { // Skip empty chunks. while (nextChunk && nextChunk.data.length === 0) - nextChunk = this.chunker.readNext(); + nextChunk = this.chunker.nextChunk(); this.currentChunkPosition += chunkLength; this.offsetInChunk = 0; } else { @@ -121,8 +121,8 @@ class _TextSeeker<TChunk extends Chunk<string>> implements Seeker<string> { } else { // Move to the end of the previous chunk. if (read) result = this.chunker.currentChunk.data.substring(0, this.offsetInChunk) + result; - const prevChunk = this.chunker.readPrev(); - if (prevChunk !== null) { + const previousChunk = this.chunker.previousChunk(); + if (previousChunk !== null) { this.currentChunkPosition -= this.chunker.currentChunk.data.length; this.offsetInChunk = this.chunker.currentChunk.data.length; } else {
