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 9c1ef9a738c5a3b2113c0973557047dfcdffdd9d
Author: Gerben <[email protected]>
AuthorDate: Wed Nov 18 18:55:18 2020 +0100

    Handle half-characters in CodePointSeeker.seekToChunk
---
 packages/dom/src/code-point-seeker.ts | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/packages/dom/src/code-point-seeker.ts 
b/packages/dom/src/code-point-seeker.ts
index b0a95cd..10cb1b1 100644
--- a/packages/dom/src/code-point-seeker.ts
+++ b/packages/dom/src/code-point-seeker.ts
@@ -64,8 +64,21 @@ export class CodePointSeeker<TChunk extends Chunk<string>> 
implements ChunkSeeke
     const oldPosition = this.position;
     const oldRawPosition = this.raw.position;
 
-    let result = [...this.raw.readToChunk(target, 0)];
-    this.position = this.raw.position >= oldRawPosition
+    let s = this.raw.readToChunk(target, 0);
+
+    const movedForward = this.raw.position >= oldRawPosition;
+
+    if (movedForward && endsWithinCharacter(s)) {
+      this.raw.seekBy(-1);
+      s = s.slice(0, -1);
+    } else if (!movedForward && startsWithinCharacter(s)) {
+      this.raw.seekBy(1);
+      s = s.slice(1);
+    }
+
+    let result = [...s];
+
+    this.position = movedForward
       ? this.position + result.length
       : this.position - result.length;
 
@@ -86,8 +99,8 @@ export class CodePointSeeker<TChunk extends Chunk<string>> 
implements ChunkSeeke
         this.seekTo(oldPosition);
         result = this.readTo(targetPosition);
       }
+      return result;
     }
-    return result;
   }
 
   private _readOrSeekTo(read: true, target: number, roundUp?: boolean): 
string[];

Reply via email to