API reference

Module selector

This module provides types and generic functions for handling Selectors.

Annotation tool developers should not need most of the functions contained in this module, but would instead mainly use the module made for the specific context (document type) they are dealing with. See dom, currently the only such module.

Index

Functions

chunkEquals

  • chunkEquals(chunk1: Chunk<any>, chunk2: Chunk<any>): boolean
  • Test two Chunks for equality.

    Equality here means that both represent the same piece of text (i.e. at the same position) in the file. It compares using the custom Chunk.equals method if either chunk defines one, and falls back to checking the objects’ identity (i.e. chunk1 === chunk2).

    Parameters

    Returns boolean

chunkRangeEquals

  • Test two ChunkRanges for equality.

    Equality here means equality of each of their four properties (i.e. startChunk, startIndex, endChunk, and endIndex). For the startChunks and endChunks, this function uses the custom Chunk.equals method if defined.

    Note that if the start/end of one range points at the end of a chunk, and the other to the start of a subsequent chunk, they are not considered equal, even though semantically they may be representing the same range of characters. To test for such semantic equivalence, ensure that both inputs are normalised: typically this means the range is shrunk to its narrowest equivalent, and (if it is empty) positioned at its first equivalent.

    Parameters

    Returns boolean

describeTextPosition

  • Returns a TextPositionSelector that points at the target text within the given scope.

    This is an abstract implementation of the function’s logic, which expects a generic Chunker to represent the text, and a ChunkRange to represent the target.

    See dom.describeTextPosition for a wrapper around this implementation which applies it to the text of an HTML DOM.

    Type parameters

    • TChunk: Chunk<string, TChunk>

    Parameters

    • target: ChunkRange<TChunk>

      The range of characters that the selector should describe

    • scope: Chunker<TChunk>

      The text, presented as a Chunker, which contains the target range, and relative to which its position will be measured

    Returns Promise<TextPositionSelector>

    The TextPositionSelector that describes target relative to scope

describeTextQuote

  • Returns a TextQuoteSelector that points at the target quote in the given text.

    The selector will contain the exact target quote. In case this quote appears multiple times in the text, sufficient context around the quote will be included in the selector’s prefix and suffix attributes to disambiguate. By default, more prefix and suffix are included than strictly required; both in order to be robust against slight modifications, and in an attempt to not end halfway a word (mainly for human readability).

    This is an abstract implementation of the function’s logic, which expects a generic Chunker to represent the text, and a ChunkRange to represent the target.

    See dom.describeTextQuote for a wrapper around this implementation which applies it to the text of an HTML DOM.

    Type parameters

    • TChunk: Chunk<string, TChunk>

    Parameters

    • target: ChunkRange<TChunk>

      The range of characters that the selector should describe

    • scope: () => Chunker<TChunk>

      The text containing the target range; or, more accurately, a function that produces Chunkers corresponding to this text.

    • Optional options: DescribeTextQuoteOptions

      Options to fine-tune the function’s behaviour.

    Returns Promise<TextQuoteSelector>

    The TextQuoteSelector that describes target.

makeRefinable

  • makeRefinable<TSelector, TScope, TMatch>(matcherCreator: (selector: TSelector) => Matcher<TScope, TMatch>): (selector: TSelector) => Matcher<TScope, TMatch>
  • Wrap a matcher creation function so that it supports refinement of selection.

    See §4.2.9 Refinement of Selection in the Web Annotation Data Model.

    Type parameters

    • TSelector: Selector & { refinedBy?: TSelector }

    • TScope

    • TMatch

    Parameters

    • matcherCreator: (selector: TSelector) => Matcher<TScope, TMatch>

      The function to wrap; it will be executed both for Selectors passed to the returned wrapper function, and for any refining Selector those might contain (and any refinement of that, etc.).

        • (selector: TSelector): Matcher<TScope, TMatch>
        • Parameters

          • selector: TSelector

          Returns Matcher<TScope, TMatch>

    Returns (selector: TSelector) => Matcher<TScope, TMatch>

      • (selector: TSelector): Matcher<TScope, TMatch>
      • Parameters

        • selector: TSelector

        Returns Matcher<TScope, TMatch>

textPositionSelectorMatcher

  • Find the range of text corresponding to the given TextPositionSelector.

    This is an abstract implementation of the function’s logic, which expects a generic Chunker to represent the text, and returns an (async) generator producing a single ChunkRange to represent the match. (unlike e.g. TextQuoteSelector, it cannot result in multiple matches).

    See dom.createTextPositionSelectorMatcher for a wrapper around this implementation which applies it to the text of an HTML DOM.

    The function is curried, taking first the selector and then the text.

    example
    const selector = { type: 'TextPositionSelector', start: 702, end: 736 };
    const matches = textPositionSelectorMatcher(selector)(textChunks);
    const match = (await matches.next()).value;
    console.log(match);
    // ⇒ { startChunk: { … }, startIndex: 64, endChunk: { … }, endIndex: 98 }
    

    Parameters

    Returns <TChunk>(scope: Chunker<TChunk>) => AsyncGenerator<ChunkRange<TChunk>, void, void>

    a Matcher function that applies selector to a given text

      • Type parameters

        Parameters

        Returns AsyncGenerator<ChunkRange<TChunk>, void, void>

textQuoteSelectorMatcher

  • Find occurrences in a text matching the given TextQuoteSelector.

    This performs an exact search the selector’s quote (including prefix and suffix) within the given text.

    Note the match is based on strict character-by-character equivalence, i.e. it is sensitive to whitespace, capitalisation, etc.

    This is an abstract implementation of the function’s logic, which expects a generic Chunker to represent the text, and returns an (async) generator of ChunkRanges to represent the matches.

    See dom.createTextQuoteSelectorMatcher for a wrapper around this implementation which applies it to the text of an HTML DOM.

    The function is curried, taking first the selector and then the text.

    As there may be multiple matches for a given selector (when its prefix and suffix attributes are not sufficient to disambiguate it), the matcher will return an (async) generator that produces each match in the order they are found in the text.

    XXX Modifying the Chunks while the search is still running can mess up and result in an error or an infinite loop. See issue #112.

    example
    const selector = { type: 'TextQuoteSelector', exact: 'banana' };
    const matches = textQuoteSelectorMatcher(selector)(textChunks);
    for await (match of matches) console.log(match);
    // ⇒ { startChunk: { … }, startIndex: 187, endChunk: { … }, endIndex: 193 }
    // ⇒ { startChunk: { … }, startIndex: 631, endChunk: { … }, endIndex: 637 }
    

    Parameters

    Returns <TChunk>(scope: Chunker<TChunk>) => AsyncGenerator<ChunkRange<TChunk>, void, void>

    a Matcher function that applies selector to a given text

      • Type parameters

        Parameters

        Returns AsyncGenerator<ChunkRange<TChunk>, void, void>