You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@annotator.apache.org by ra...@apache.org on 2019/05/24 14:57:52 UTC

[incubator-annotator] branch master updated: Bind selector creators to their context

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 6b15d90  Bind selector creators to their context
6b15d90 is described below

commit 6b15d90f70c3b3fceb349cf77fe5f9dcbe8e46e2
Author: Randall Leeds <ra...@apache.org>
AuthorDate: Fri May 24 10:52:38 2019 -0400

    Bind selector creators to their context
---
 demo/search.js                 |  9 ++++-----
 packages/dom/src/index.js      |  7 +++----
 packages/dom/src/text/quote.js |  4 ++--
 packages/range/src/index.js    | 18 ++++++------------
 packages/selector/src/index.js | 34 ++++++++++++++++------------------
 packages/text/src/index.js     |  4 ++--
 6 files changed, 33 insertions(+), 43 deletions(-)

diff --git a/demo/search.js b/demo/search.js
index b6f81cf..c51c1b8 100644
--- a/demo/search.js
+++ b/demo/search.js
@@ -22,7 +22,7 @@ const allSelectorTypes = {
   RangeSelector: createRangeSelector,
 };
 
-const selectorFunc = createAnySelectorCreator(allSelectorTypes)();
+const createSelector = createAnySelectorCreator(allSelectorTypes);
 
 /**
  * Locate a selector.
@@ -34,10 +34,9 @@ export async function* search(root, descriptor) {
   for (const node of nodeIterator(root)) {
     if (!node.nodeValue) continue;
 
-    const matches = selectorFunc({
-      descriptors: [descriptor],
-      context: node.nodeValue,
-    });
+    const selector = createSelector(node.nodeValue);
+    const matches = selector([descriptor]);
+
     for await (let match of matches) {
       const startIndex = match.index;
       const endIndex = startIndex + match[0].length;
diff --git a/packages/dom/src/index.js b/packages/dom/src/index.js
index 70a36bb..80b1efd 100644
--- a/packages/dom/src/index.js
+++ b/packages/dom/src/index.js
@@ -15,10 +15,9 @@
 
 export * from './text';
 
-export function createCssSelector(selectors) {
-  const cssSelector = selectors.map(({ value }) => value).join(',');
-
-  async function* exec(context) {
+export function createCssSelector(context) {
+  async function* exec(descriptors) {
+    const cssSelector = descriptors.map(({ value }) => value).join(',');
     yield* context.querySelectorAll(cssSelector);
   }
 
diff --git a/packages/dom/src/text/quote.js b/packages/dom/src/text/quote.js
index ac13d71..13b6add 100644
--- a/packages/dom/src/text/quote.js
+++ b/packages/dom/src/text/quote.js
@@ -49,8 +49,8 @@ export async function describeTextQuoteByRange({ range, context }) {
   const rangeIndex = range.startOffset;
   const rangeEndIndex = range.endOffset;
 
-  const selector = createTextQuoteSelector();
-  const matches = selector({ descriptors: [descriptor], context: contextText });
+  const selector = createTextQuoteSelector(contextText);
+  const matches = selector([descriptor]);
   const minSuffixes = [];
   const minPrefixes = [];
   for await (let match of matches) {
diff --git a/packages/range/src/index.js b/packages/range/src/index.js
index f1fc4d7..a9b8b82 100644
--- a/packages/range/src/index.js
+++ b/packages/range/src/index.js
@@ -15,20 +15,14 @@
 
 import { product } from './cartesian.js';
 
-export function createRangeSelector({ createAnySelector }) {
-  const startSelector = createAnySelector();
-  const endSelector = createAnySelector();
+export function createRangeSelector(context, createAnySelector) {
+  const startSelector = createAnySelector(context);
+  const endSelector = createAnySelector(context);
 
-  async function* rangeSelector({ descriptors, context }) {
+  async function* rangeSelector(descriptors) {
     const descriptor = descriptors[0]; // TODO handle multiple descriptors
-    const startMatches = startSelector({
-      descriptors: [descriptor.startSelector],
-      context,
-    });
-    const endMatches = endSelector({
-      descriptors: [descriptor.endSelector],
-      context,
-    });
+    const startMatches = startSelector([descriptor.startSelector]);
+    const endMatches = endSelector([descriptor.endSelector]);
     const pairs = product(startMatches, endMatches);
     for await (let [start, end] of pairs) {
       if (start.index > end.index) {
diff --git a/packages/selector/src/index.js b/packages/selector/src/index.js
index a7cebba..17b79f1 100644
--- a/packages/selector/src/index.js
+++ b/packages/selector/src/index.js
@@ -14,39 +14,37 @@
  */
 
 export function createAnySelectorCreator(selectorCreatorsByType) {
-  function selectSelector(type) {
-    const selectorCreator = selectorCreatorsByType[type];
-    if (selectorCreator === undefined) {
+  function selectSelector(context, type) {
+    const selectorCreatorForType = selectorCreatorsByType[type];
+    if (selectorCreatorForType === undefined) {
       throw new Error(`Unsupported selector type: ${type}`);
     }
-    let selector = selectorCreator({ createAnySelector });
-    selector = makeRefinable(selector, { createAnySelector });
+    let selector = selectorCreatorForType(context, selectorCreator);
+    selector = makeRefinable(selector, selectorCreator);
     return selector;
   }
 
-  function createAnySelector() {
-    async function* anySelector({ descriptors, context }) {
+  function selectorCreator(context) {
+    async function* anySelector(descriptors) {
       const descriptor = descriptors[0]; // TODO handle multiple descriptors
-      const selectorFunc = selectSelector(descriptor.type);
-      yield* selectorFunc({ descriptors: [descriptor], context });
+      const selectorFunc = selectSelector(context, descriptor.type);
+      yield* selectorFunc([descriptor]);
     }
     return anySelector;
   }
 
-  return createAnySelector;
+  return selectorCreator;
 }
 
-export function makeRefinable(selector, { createAnySelector }) {
-  async function* refinableSelector({ descriptors, context }) {
-    const matches = selector({ descriptors, context });
+export function makeRefinable(selector, selectorCreator) {
+  async function* refinableSelector(descriptors) {
+    const matches = selector(descriptors);
     for await (let match of matches) {
       const refiningDescriptor = match.descriptor.refinedBy;
       if (refiningDescriptor) {
-        const anySelector = createAnySelector();
-        const refiningMatches = anySelector({
-          descriptors: [refiningDescriptor],
-          context: matchAsContext(match),
-        });
+        const refiningContext = matchAsContext(match);
+        const refiningSelector = selectorCreator(refiningContext);
+        const refiningMatches = refiningSelector([refiningDescriptor]);
         for await (let refiningMatch of refiningMatches) {
           const refinedMatch = composeMatches(refiningMatch, match);
           yield refinedMatch;
diff --git a/packages/text/src/index.js b/packages/text/src/index.js
index ee8810e..fa69321 100644
--- a/packages/text/src/index.js
+++ b/packages/text/src/index.js
@@ -13,8 +13,8 @@
  * the License.
  */
 
-export function createTextQuoteSelector() {
-  async function* exec({ descriptors, context }) {
+export function createTextQuoteSelector(context) {
+  async function* exec(descriptors) {
     for (let descriptor of descriptors) {
       const prefix = descriptor.prefix || '';
       const suffix = descriptor.suffix || '';