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/05/28 11:01:19 UTC

[incubator-annotator] branch dom-tests updated (1592338 -> 9c9d2fa)

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

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


 discard 1592338  Test with custom/empty scopes and fix revealed mistakes
     new eb791b7  Test with custom/empty scopes and fix revealed mistakes
     new 82f0a27  Rename test files to end with '.test.ts'
     new 9c9d2fa  Organise folder structure

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   (1592338)
            \
             N -- N -- N   refs/heads/dom-tests (9c9d2fa)

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 3 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:
 package.json                                                      | 4 ++--
 packages/dom/src/index.ts                                         | 2 +-
 packages/dom/src/{ => range}/cartesian.ts                         | 0
 packages/dom/{test/index.js => src/range/index.ts}                | 2 +-
 packages/dom/src/{range.ts => range/match.ts}                     | 6 +++---
 packages/dom/src/text-quote/index.ts                              | 2 +-
 packages/dom/test/{cartesian.ts => range/cartesian.test.ts}       | 2 +-
 .../describe-cases.ts}                                            | 2 +-
 .../test/{text-quote-describe.ts => text-quote/describe.test.ts}  | 7 +++----
 .../test/{text-quote-match-cases.ts => text-quote/match-cases.ts} | 2 +-
 .../dom/test/{text-quote-match.ts => text-quote/match.test.ts}    | 8 ++++----
 packages/dom/test/{ => text-quote}/utils.ts                       | 0
 test/{data-model.ts => data-model.test.ts}                        | 0
 web/webpack.config.js                                             | 2 +-
 14 files changed, 19 insertions(+), 20 deletions(-)
 rename packages/dom/src/{ => range}/cartesian.ts (100%)
 rename packages/dom/{test/index.js => src/range/index.ts} (97%)
 rename packages/dom/src/{range.ts => range/match.ts} (91%)
 rename packages/dom/test/{cartesian.ts => range/cartesian.test.ts} (96%)
 rename packages/dom/test/{text-quote-describe-cases.ts => text-quote/describe-cases.ts} (97%)
 rename packages/dom/test/{text-quote-describe.ts => text-quote/describe.test.ts} (94%)
 rename packages/dom/test/{text-quote-match-cases.ts => text-quote/match-cases.ts} (99%)
 rename packages/dom/test/{text-quote-match.ts => text-quote/match.test.ts} (97%)
 rename packages/dom/test/{ => text-quote}/utils.ts (100%)
 rename test/{data-model.ts => data-model.test.ts} (100%)


[incubator-annotator] 01/03: Test with custom/empty scopes and fix revealed mistakes

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

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

commit eb791b7abf4580b321870babbc0a113c20a52385
Author: Gerben <ge...@treora.com>
AuthorDate: Wed May 27 20:36:27 2020 +0200

    Test with custom/empty scopes and fix revealed mistakes
    
    When describing a range that goes beyond the given scope, it will now
    trim the range to the part that is within the scope; is this desirable?
    We could make it optional and by default throw an error instead.
---
 packages/dom/src/text-quote/describe.ts        | 20 +++++++++----
 packages/dom/test/text-quote-describe-cases.ts | 20 +++++++++----
 packages/dom/test/text-quote-describe.ts       | 39 +++++++++++++++++++++++++-
 packages/dom/test/text-quote-match.ts          | 28 ++++++++++++++++++
 4 files changed, 95 insertions(+), 12 deletions(-)

diff --git a/packages/dom/src/text-quote/describe.ts b/packages/dom/src/text-quote/describe.ts
index 784881c..15749c9 100644
--- a/packages/dom/src/text-quote/describe.ts
+++ b/packages/dom/src/text-quote/describe.ts
@@ -28,6 +28,15 @@ export async function describeTextQuote(
   range: Range,
   scope: DomScope = ownerDocument(range).documentElement,
 ): Promise<TextQuoteSelector> {
+  range = range.cloneRange();
+
+  // Take the part of the range that falls within the scope.
+  const scopeAsRange = rangeFromScope(scope);
+  if (!scopeAsRange.isPointInRange(range.startContainer, range.startOffset))
+    range.setStart(scopeAsRange.startContainer, scopeAsRange.startOffset);
+  if (!scopeAsRange.isPointInRange(range.endContainer, range.endOffset))
+    range.setEnd(scopeAsRange.endContainer, scopeAsRange.endOffset);
+
   const exact = range.toString();
 
   const result: TextQuoteSelector = { type: 'TextQuoteSelector', exact };
@@ -94,10 +103,8 @@ function calculateContextForDisambiguation(
 function charactersNeededToBeUnique(target: string, impostor: string, reverse: boolean = false) {
   // Count how many characters the two strings have in common.
   let overlap = 0;
-  while (reverse
-    ? target[target.length - 1 - overlap] === impostor[impostor.length - 1 - overlap]
-    : target[overlap] === impostor[overlap]
-  )
+  const charAt = (s: string, i: number) => reverse ? s[s.length - 1 - i] : s[overlap];
+  while (overlap < target.length && charAt(target, overlap) === charAt(impostor, overlap))
     overlap++;
   if (overlap === target.length)
     return Infinity; // (no substring of target can make it distinguishable from its impostor)
@@ -140,10 +147,11 @@ 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;
+    return seek(iter, range.startContainer) + range.startOffset - scopeOffset;
   else
-    return seek(iter, firstTextNodeInRange(range));
+    return seek(iter, firstTextNodeInRange(range)) - scopeOffset;
 }
 
 function firstTextNodeInRange(range: Range): Text {
diff --git a/packages/dom/test/text-quote-describe-cases.ts b/packages/dom/test/text-quote-describe-cases.ts
index 4f34c92..a7e556e 100644
--- a/packages/dom/test/text-quote-describe-cases.ts
+++ b/packages/dom/test/text-quote-describe-cases.ts
@@ -98,11 +98,21 @@ const testCases: {
       suffix: ' ',
     },
   },
-
-  // TODO test for:
-  // empty scope
-  // custom scope
-  // element edges, across elements, etc.
+  'across elements': {
+    html: '<b>To annotate or <i>not</i> to <u>anno</u>tate</b>',
+    range: {
+      startContainerXPath: '//u/text()',
+      startOffset: 0,
+      endContainerXPath: '//b/text()[3]',
+      endOffset: 2,
+    },
+    expected: {
+      type: 'TextQuoteSelector',
+      exact: 'annota',
+      prefix: 'to ',
+      suffix: '',
+    },
+  },
 };
 
 export default testCases;
diff --git a/packages/dom/test/text-quote-describe.ts b/packages/dom/test/text-quote-describe.ts
index 749c083..ba5a52c 100644
--- a/packages/dom/test/text-quote-describe.ts
+++ b/packages/dom/test/text-quote-describe.ts
@@ -22,7 +22,7 @@ import { assert } from 'chai';
 import { describeTextQuote } from '../src/text-quote/describe';
 import testCases from './text-quote-describe-cases';
 import testMatchCases from './text-quote-match-cases';
-import { hydrateRange } from './utils';
+import { hydrateRange, evaluateXPath } from './utils';
 
 const domParser = new window.DOMParser();
 
@@ -35,6 +35,43 @@ describe('describeTextQuote', () => {
     })
   }
 
+  it('works with custom scope', async () => {
+    const { html, range } = testCases['minimal prefix'];
+    const doc = domParser.parseFromString(html, 'text/html');
+    const scope = document.createRange();
+    scope.setStart(evaluateXPath(doc, '//b/text()'), 15);
+    scope.setEnd(evaluateXPath(doc, '//b/text()'), 30); // "not to annotate"
+    const result = await describeTextQuote(hydrateRange(range, doc), scope);
+    assert.deepEqual(result, {
+      type: 'TextQuoteSelector',
+      exact: 'anno',
+      prefix: '', // no prefix needed in this scope.
+      suffix: '',
+    });
+  });
+
+  it('strips part of the range outside the scope', async () => {
+    const { html, range } = testCases['simple'];
+    const doc = domParser.parseFromString(html, 'text/html');
+    const scope = document.createRange();
+    scope.setStart(evaluateXPath(doc, '//b/text()'), 6);
+    scope.setEnd(evaluateXPath(doc, '//b/text()'), 17); // "ipsum dolor"
+    const result = await describeTextQuote(hydrateRange(range, doc), scope);
+    assert.deepEqual(result, {
+      type: 'TextQuoteSelector',
+      exact: 'dolor',
+      prefix: '',
+      suffix: '',
+    });
+  });
+
+  it('works if the range equals the scope', async () => {
+    const { html, range, expected } = testCases['simple'];
+    const doc = domParser.parseFromString(html, 'text/html');
+    const result = await describeTextQuote(hydrateRange(range, doc), hydrateRange(range, doc));
+    assert.deepEqual(result, expected);
+  });
+
   describe('inverts test cases of text quote matcher', () => {
     const applicableTestCases = Object.entries(testMatchCases)
       .filter(([_, { expected }]) => expected.length > 0);
diff --git a/packages/dom/test/text-quote-match.ts b/packages/dom/test/text-quote-match.ts
index 1144bc3..e7a025e 100644
--- a/packages/dom/test/text-quote-match.ts
+++ b/packages/dom/test/text-quote-match.ts
@@ -143,6 +143,34 @@ describe('createTextQuoteSelectorMatcher', () => {
     scope.setEnd(evaluateXPath(doc, '//b'), 4); // before the " yada yada"
     await testMatcher(doc, scope, selector, expected);
   });
+
+  it('ignores quote when scope is an empty range', async () => {
+    const { html, selector } = testCases['simple'];
+    const doc = domParser.parseFromString(html, 'text/html');
+
+    const scope = document.createRange();
+    await testMatcher(doc, scope, selector, []);
+  });
+
+  it('ignores quote extending just beyond scope', async () => {
+    const { html, selector } = testCases['simple'];
+    const doc = domParser.parseFromString(html, 'text/html');
+
+    const scope = document.createRange();
+    scope.setStart(evaluateXPath(doc, '//b/text()'), 0);
+    scope.setEnd(evaluateXPath(doc, '//b/text()'), 19);
+    await testMatcher(doc, scope, selector, []);
+  });
+
+  it('ignores quote starting just before scope', async () => {
+    const { html, selector } = testCases['simple'];
+    const doc = domParser.parseFromString(html, 'text/html');
+
+    const scope = document.createRange();
+    scope.setStart(evaluateXPath(doc, '//b/text()'), 13);
+    scope.setEnd(evaluateXPath(doc, '//b/text()'), 32);
+    await testMatcher(doc, scope, selector, []);
+  });
 });
 
 async function testMatcher(


[incubator-annotator] 02/03: Rename test files to end with '.test.ts'

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

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

commit 82f0a270da9a10881364c3a9dcb29147f37dd6b0
Author: Gerben <ge...@treora.com>
AuthorDate: Thu May 28 12:03:47 2020 +0200

    Rename test files to end with '.test.ts'
    
    So mocha won’t bother executing 'test/utils.ts' etcetera.
---
 package.json                                                          | 4 ++--
 packages/dom/test/{cartesian.ts => cartesian.test.ts}                 | 0
 .../dom/test/{text-quote-describe.ts => text-quote-describe.test.ts}  | 0
 packages/dom/test/{text-quote-match.ts => text-quote-match.test.ts}   | 0
 test/{data-model.ts => data-model.test.ts}                            | 0
 web/webpack.config.js                                                 | 2 +-
 6 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/package.json b/package.json
index 004eb24..5bbf2be 100644
--- a/package.json
+++ b/package.json
@@ -25,9 +25,9 @@
     "prepare": "lerna run prepare",
     "prepublishOnly": "yarn run build",
     "start": "yarn run web:server",
-    "test": "cross-env BABEL_ENV=test nyc mocha packages/*/test/**/*.[jt]s",
+    "test": "cross-env BABEL_ENV=test nyc mocha packages/*/test/**/*.test.[jt]s",
     "typecheck": "tsc && tsc -p tsconfig.tests.json",
-    "validate": "cross-env BABEL_ENV=test mocha test/**/*.[jt]s",
+    "validate": "cross-env BABEL_ENV=test mocha test/**/*.test.[jt]s",
     "web:build": "webpack  --config=web/webpack.config.js --mode development",
     "web:server": "webpack-dev-server --config=web/webpack.config.js --hot --mode development"
   },
diff --git a/packages/dom/test/cartesian.ts b/packages/dom/test/cartesian.test.ts
similarity index 100%
rename from packages/dom/test/cartesian.ts
rename to packages/dom/test/cartesian.test.ts
diff --git a/packages/dom/test/text-quote-describe.ts b/packages/dom/test/text-quote-describe.test.ts
similarity index 100%
rename from packages/dom/test/text-quote-describe.ts
rename to packages/dom/test/text-quote-describe.test.ts
diff --git a/packages/dom/test/text-quote-match.ts b/packages/dom/test/text-quote-match.test.ts
similarity index 100%
rename from packages/dom/test/text-quote-match.ts
rename to packages/dom/test/text-quote-match.test.ts
diff --git a/test/data-model.ts b/test/data-model.test.ts
similarity index 100%
rename from test/data-model.ts
rename to test/data-model.test.ts
diff --git a/web/webpack.config.js b/web/webpack.config.js
index 8da5b59..5dcbfda 100644
--- a/web/webpack.config.js
+++ b/web/webpack.config.js
@@ -30,7 +30,7 @@ module.exports = {
     demo: ['./demo/index.html', './demo/index.js'],
     test: [
       './test/index.html',
-      'mocha-loader!multi-entry-loader?include=./packages/*/test/**/*.[jt]s!',
+      'mocha-loader!multi-entry-loader?include=./packages/*/test/**/*.test.[jt]s!',
     ],
   },
   resolve: {


[incubator-annotator] 03/03: Organise folder structure

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

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

commit 9c9d2fa78560c8270d70988b6eb081f3bdca5a49
Author: Gerben <ge...@treora.com>
AuthorDate: Thu May 28 12:51:24 2020 +0200

    Organise folder structure
    
    Note that test files directly in dom/test/ are not found by mocha now
    there are also test files present in its subdirectory; presumably due to
    a bug in how the ** wildcard is interpreted in
    packages/*/test/**/*.test.[jt]s; I have been unable to find out exactly
    why or in which module this occurs.
---
 packages/dom/src/index.ts                                         | 2 +-
 packages/dom/src/{ => range}/cartesian.ts                         | 0
 packages/dom/{test/index.js => src/range/index.ts}                | 2 +-
 packages/dom/src/{range.ts => range/match.ts}                     | 6 +++---
 packages/dom/src/text-quote/index.ts                              | 2 +-
 packages/dom/test/{ => range}/cartesian.test.ts                   | 2 +-
 .../describe-cases.ts}                                            | 2 +-
 .../{text-quote-describe.test.ts => text-quote/describe.test.ts}  | 6 +++---
 .../test/{text-quote-match-cases.ts => text-quote/match-cases.ts} | 2 +-
 .../test/{text-quote-match.test.ts => text-quote/match.test.ts}   | 8 ++++----
 packages/dom/test/{ => text-quote}/utils.ts                       | 0
 11 files changed, 16 insertions(+), 16 deletions(-)

diff --git a/packages/dom/src/index.ts b/packages/dom/src/index.ts
index 5b89ef6..6ef53d7 100644
--- a/packages/dom/src/index.ts
+++ b/packages/dom/src/index.ts
@@ -19,6 +19,6 @@
  */
 
 export * from './css';
-export * from './range';
+export * from './range/index';
 export * from './text-quote/index';
 export * from './highlight-range';
diff --git a/packages/dom/src/cartesian.ts b/packages/dom/src/range/cartesian.ts
similarity index 100%
rename from packages/dom/src/cartesian.ts
rename to packages/dom/src/range/cartesian.ts
diff --git a/packages/dom/test/index.js b/packages/dom/src/range/index.ts
similarity index 97%
rename from packages/dom/test/index.js
rename to packages/dom/src/range/index.ts
index ebd8b07..011e994 100644
--- a/packages/dom/test/index.js
+++ b/packages/dom/src/range/index.ts
@@ -18,4 +18,4 @@
  * under the License.
  */
 
-export {};
+export * from './match';
diff --git a/packages/dom/src/range.ts b/packages/dom/src/range/match.ts
similarity index 91%
rename from packages/dom/src/range.ts
rename to packages/dom/src/range/match.ts
index e465386..e540d5c 100644
--- a/packages/dom/src/range.ts
+++ b/packages/dom/src/range/match.ts
@@ -18,10 +18,10 @@
  * under the License.
  */
 
-import { ownerDocument } from './scope';
+import { ownerDocument } from '../scope';
 import { product } from './cartesian';
-import { RangeSelector, Selector } from '../../selector/src/types';
-import { DomMatcher, DomScope } from './types';
+import { RangeSelector, Selector } from '../../../selector/src/types';
+import { DomMatcher, DomScope } from '../types';
 
 export function makeCreateRangeSelectorMatcher(
   createMatcher: <T extends Selector>(selector: T) => DomMatcher
diff --git a/packages/dom/src/text-quote/index.ts b/packages/dom/src/text-quote/index.ts
index 9f77e75..bb73732 100644
--- a/packages/dom/src/text-quote/index.ts
+++ b/packages/dom/src/text-quote/index.ts
@@ -18,5 +18,5 @@
  * under the License.
  */
 
- export * from './describe';
+export * from './describe';
 export * from './match';
diff --git a/packages/dom/test/cartesian.test.ts b/packages/dom/test/range/cartesian.test.ts
similarity index 96%
rename from packages/dom/test/cartesian.test.ts
rename to packages/dom/test/range/cartesian.test.ts
index 9ff47eb..5fd854b 100644
--- a/packages/dom/test/cartesian.test.ts
+++ b/packages/dom/test/range/cartesian.test.ts
@@ -19,7 +19,7 @@
  */
 
 import { assert } from 'chai';
-import { product } from '../src/cartesian';
+import { product } from '../../src/range/cartesian';
 
 async function* gen1() {
   yield 1;
diff --git a/packages/dom/test/text-quote-describe-cases.ts b/packages/dom/test/text-quote/describe-cases.ts
similarity index 97%
rename from packages/dom/test/text-quote-describe-cases.ts
rename to packages/dom/test/text-quote/describe-cases.ts
index a7e556e..c40ec3e 100644
--- a/packages/dom/test/text-quote-describe-cases.ts
+++ b/packages/dom/test/text-quote/describe-cases.ts
@@ -1,4 +1,4 @@
-import { TextQuoteSelector } from "../../selector/src";
+import { TextQuoteSelector } from "../../../selector/src";
 import { RangeInfo } from "./utils";
 
 const testCases: {
diff --git a/packages/dom/test/text-quote-describe.test.ts b/packages/dom/test/text-quote/describe.test.ts
similarity index 95%
rename from packages/dom/test/text-quote-describe.test.ts
rename to packages/dom/test/text-quote/describe.test.ts
index ba5a52c..f962157 100644
--- a/packages/dom/test/text-quote-describe.test.ts
+++ b/packages/dom/test/text-quote/describe.test.ts
@@ -19,9 +19,9 @@
  */
 
 import { assert } from 'chai';
-import { describeTextQuote } from '../src/text-quote/describe';
-import testCases from './text-quote-describe-cases';
-import testMatchCases from './text-quote-match-cases';
+import { describeTextQuote } from '../../src/text-quote/describe';
+import testCases from './describe-cases';
+import testMatchCases from './match-cases';
 import { hydrateRange, evaluateXPath } from './utils';
 
 const domParser = new window.DOMParser();
diff --git a/packages/dom/test/text-quote-match-cases.ts b/packages/dom/test/text-quote/match-cases.ts
similarity index 99%
rename from packages/dom/test/text-quote-match-cases.ts
rename to packages/dom/test/text-quote/match-cases.ts
index 0fd757a..856169a 100644
--- a/packages/dom/test/text-quote-match-cases.ts
+++ b/packages/dom/test/text-quote/match-cases.ts
@@ -1,4 +1,4 @@
-import { TextQuoteSelector } from "../../selector/src";
+import { TextQuoteSelector } from "../../../selector/src";
 import { RangeInfo } from "./utils";
 
 const testCases: {
diff --git a/packages/dom/test/text-quote-match.test.ts b/packages/dom/test/text-quote/match.test.ts
similarity index 97%
rename from packages/dom/test/text-quote-match.test.ts
rename to packages/dom/test/text-quote/match.test.ts
index e7a025e..3148a3f 100644
--- a/packages/dom/test/text-quote-match.test.ts
+++ b/packages/dom/test/text-quote/match.test.ts
@@ -19,10 +19,10 @@
  */
 
 import { assert } from 'chai';
-import { createTextQuoteSelectorMatcher } from '../src/text-quote/match';
-import { TextQuoteSelector } from '../../selector/src/types';
-import { DomScope } from '../src/types';
-import testCases from './text-quote-match-cases';
+import { createTextQuoteSelectorMatcher } from '../../src/text-quote/match';
+import { TextQuoteSelector } from '../../../selector/src/types';
+import { DomScope } from '../../src/types';
+import testCases from './match-cases';
 import { evaluateXPath, RangeInfo } from './utils';
 
 const domParser = new window.DOMParser();
diff --git a/packages/dom/test/utils.ts b/packages/dom/test/text-quote/utils.ts
similarity index 100%
rename from packages/dom/test/utils.ts
rename to packages/dom/test/text-quote/utils.ts