license import type { TextPositionSelector } from '../types.js' ; import type { Chunk , ChunkRange , Chunker } from './chunker.js' ; import { CodePointSeeker } from './code-point-seeker.js' ; import { TextSeeker } from './seeker.js' ; /**
 * 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 param selector - the  TextPositionSelector to be anchored
 *  returns a  Matcher function that applies `selector` to a given text
 *
 *  public export function textPositionSelectorMatcher ( selector ) { const { start , end } = selector ; return async function * matchAll < TChunk extends Chunk < string > > ( textChunks ) { const codeUnitSeeker = new TextSeeker ( textChunks ) ; const codePointSeeker = new CodePointSeeker ( codeUnitSeeker ) ; codePointSeeker . seekTo ( start ) ; const startChunk = codeUnitSeeker . currentChunk ; const startIndex = codeUnitSeeker . offsetInChunk ; codePointSeeker . seekTo ( end ) ; const endChunk = codeUnitSeeker . currentChunk ; const endIndex = codeUnitSeeker . offsetInChunk ; yield { startChunk , startIndex , endChunk , endIndex } ; } ; }