/** * A candidate returned by {@link SelectorSet.matches}: the indexed selector and the value stored with it. */ export interface Match { selector: string; value: T; } /** * Indexes selectors by the key token (`#id`, `[attribute]`, `.class`, or `tag`) of their *subject* - the rightmost * compound of each comma-separated part - so a DOM mutation only needs to consult selectors that could plausibly match * the affected element. `form > button` is indexed under the `button` tag, and `div, .foo` under both `div` and `.foo`. * Selectors with a part whose subject has no such token (e.g. `:is(...)`, `*`) fall into a catch-all bucket and are * checked against every element. * * Modeled on https://github.com/josh/selector-set, which likewise keys each comma-separated part on its last compound. * Its indexes stop at `#id`, `.class` and `tag`; an attribute index is added here because a framework's own selectors * (`[data-target]`, `[data-action]`) are attribute-only compounds that would otherwise all sit in the catch-all bucket. * * Callers must run the final `element.matches(selector)` check on the returned candidates - the index narrows the * search space; it does not validate the full selector. {@link SelectorSet.queryAll} and * {@link SelectorSet.querySubjects} enumerate a subtree instead, and their contracts differ; see each. */ export default class SelectorSet { private idIndex; private attributeIndex; private classIndex; private tagIndex; private fallback; private count; private generation; private selectorCounts; private matchQueryString; private subjectQueryString; private attributeNames; /** * Indexes `value` under `selector`. Adding the same pair twice stores two entries, each needing its own * {@link SelectorSet.delete}. */ add(selector: string, value: T): void; /** * Removes one entry for the `selector`/`value` pair. A no-op when the pair was never added. */ delete(selector: string, value: T): void; /** * Every entry whose selector could match `element`, each returned once however many buckets it sits in. These are * candidates: run `element.matches(selector)` on them to get the real matches. * * Each call returns a new array, but the entries in it belong to the set. Read them, and do not mutate them: changing * one corrupts the index. */ matches(element: Element): Match[]; /** * The descendants of `context` matching any indexed selector, for a subtree that has just been *added*. It is already * in the document when a mutation record is delivered, and `querySelectorAll` resolves a selector against the whole * document before narrowing to descendants, so a selector anchored on an ancestor outside `context` still matches. * Results are therefore exact for the set as a whole, though a caller still needs {@link SelectorSet.matches} to * learn *which* selector each one matched. * * `context` itself is never included, the way `querySelectorAll` does not include its root. */ queryAll(context: Element): Iterable; /** * The descendants of `context` that could be tracked under any indexed selector, for a subtree that has just been * removed. Such a subtree is detached by the time its record is delivered, so an anchored selector can no longer * match inside it and {@link SelectorSet.queryAll} would come back empty. Each selector is reduced to its subject * instead, which every element it matched still matches, so the result is a superset - enough to find everything a * disconnect has to reach. * * Degrades to every element when some selector has no queryable subject at all. * * `context` itself is never included. */ querySubjects(context: Element): Iterable; /** * How many entries are indexed. */ get size(): number; /** * Tracks how many entries each distinct selector holds, dropping the cached queries and attribute names when the set * of selectors gains or loses one. A second entry for a selector already held changes neither. */ private countSelector; /** * One bucket per comma-separated part - two for an attribute whose name is not already lowercase, since an HTML * element stores it lowercased while an SVG or MathML one keeps the case it was written in. `null` when any part * cannot be indexed (the whole selector then goes to the fallback bucket so no part is ever missed). */ private bucketsFor; } //# sourceMappingURL=selector_set.d.ts.map