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/07/18 15:09:10 UTC

[incubator-annotator] branch improve-range-stuff updated (9a765d1 -> 2be85b6)

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

gerben pushed a change to branch improve-range-stuff
in repository https://gitbox.apache.org/repos/asf/incubator-annotator.git.


 discard 9a765d1  WIP reimplement range iteration
 discard d09af67  Add failing test for range without Text nodes
 discard 462a3ab  Replace replaceChild with replaceWith
 discard 8800720  Support empty ranges in highlighter & demo
     new 2be85b6  WIP reimplement range iteration

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (9a765d1)
            \
             N -- N -- N   refs/heads/improve-range-stuff (2be85b6)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 packages/dom/src/highlight-range.ts           |  8 +++++++-
 packages/dom/test/text-quote/describe.test.ts | 14 --------------
 web/demo/index.js                             | 18 ++++++++++++++----
 web/style.css                                 |  2 +-
 4 files changed, 22 insertions(+), 20 deletions(-)


[incubator-annotator] 01/01: WIP reimplement range iteration

Posted by ge...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

gerben pushed a commit to branch improve-range-stuff
in repository https://gitbox.apache.org/repos/asf/incubator-annotator.git

commit 2be85b60f215851808a92ebabbbeaf3424b32cff
Author: Gerben <ge...@treora.com>
AuthorDate: Thu Jun 25 23:40:54 2020 +0200

    WIP reimplement range iteration
---
 packages/dom/src/text-iterator.ts       | 64 +++++++++++++++++++++++++++++++++
 packages/dom/src/text-quote/describe.ts | 25 ++-----------
 2 files changed, 66 insertions(+), 23 deletions(-)

diff --git a/packages/dom/src/text-iterator.ts b/packages/dom/src/text-iterator.ts
new file mode 100644
index 0000000..53e8732
--- /dev/null
+++ b/packages/dom/src/text-iterator.ts
@@ -0,0 +1,64 @@
+/**
+ * @license
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+interface TextRange extends Range {
+  // We guarantee that to always have Text nodes as start and end containers.
+  readonly startContainer: Text;
+  readonly endContainer: Text;
+  cloneRange(): TextRange;
+
+  // Allow only Text nodes to be passed to these methods.
+  insertNode(node: Text): void;
+  selectNodeContents(node: Text): void;
+  setEnd(node: Text, offset: number): void;
+  setStart(node: Text, offset: number): void;
+
+  // Do not allow these methods to be used at all.
+  selectNode(node: never): void;
+  setEndAfter(node: never): void;
+  setEndBefore(node: never): void;
+  setStartAfter(node: never): void;
+  setStartBefore(node: never): void;
+  surroundContents(newParent: never): void;
+}
+
+function shrinkRangeToTextNodes(range: Range): TextRange {
+  // TODO walk to first & last text nodes inside the range.
+  return range as TextRange;
+}
+
+class TextIterator {
+  constructor(range: Range) {
+    const iter = document.createNodeIterator(
+      range.commonAncestorContainer,
+      NodeFilter.SHOW_TEXT,
+      {
+        acceptNode: node =>
+          range.intersectsNode(node)
+            ? NodeFilter.FILTER_ACCEPT
+            : NodeFilter.FILTER_REJECT
+      },
+    );
+
+    // Move to the start of the first text node (if any).
+    if (iter.nextNode())
+      iter.previousNode();
+  }
+}
diff --git a/packages/dom/src/text-quote/describe.ts b/packages/dom/src/text-quote/describe.ts
index 15749c9..0cc1760 100644
--- a/packages/dom/src/text-quote/describe.ts
+++ b/packages/dom/src/text-quote/describe.ts
@@ -148,29 +148,8 @@ function getRangeTextPosition(range: Range, scope: DomScope): number {
     },
   );
   const scopeOffset = isTextNode(scopeAsRange.startContainer) ? scopeAsRange.startOffset : 0;
-  if (isTextNode(range.startContainer))
-    return seek(iter, range.startContainer) + range.startOffset - scopeOffset;
-  else
-    return seek(iter, firstTextNodeInRange(range)) - scopeOffset;
-}
-
-function firstTextNodeInRange(range: Range): Text {
-  // Find the first text node inside the range.
-  const iter = document.createNodeIterator(
-    range.commonAncestorContainer,
-    NodeFilter.SHOW_TEXT,
-    {
-      acceptNode(node: Text) {
-        // Only reveal nodes within the range; and skip any empty text nodes.
-        return range.intersectsNode(node) && node.length > 0
-          ? NodeFilter.FILTER_ACCEPT
-          : NodeFilter.FILTER_REJECT
-      },
-    },
-  );
-  const node = iter.nextNode() as Text | null;
-  if (node === null) throw new Error('Range contains no text nodes');
-  return node;
+  const rangeOffset = isTextNode(range.startContainer) ? range.startOffset : 0;
+  return seek(iter, range.startContainer) + rangeOffset - scopeOffset;
 }
 
 function isTextNode(node: Node): node is Text {