Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
studio: stop find in page matching part way through a grapheme (#10200)
Daniel Hana7dec18b6772
diff --git a/studio/frontend/src/features/find-in-page/lib/find-text-index.ts b/studio/frontend/src/features/find-in-page/lib/find-text-index.tsindex e49fef52443..39665f8e6f2 100644--- a/studio/frontend/src/features/find-in-page/lib/find-text-index.ts+++ b/studio/frontend/src/features/find-in-page/lib/find-text-index.ts@@ -6,32 +6,25 @@import { FIND_SKIP_ATTRIBUTE } from "./find-attributes.ts";-/** `Node.ELEMENT_NODE` / `Node.TEXT_NODE`, spelled out so this module needs no DOM globals. */export const ELEMENT_NODE = 1;export const TEXT_NODE = 3;-/** Written at block boundaries so a match cannot run from one paragraph into the next. NUL because- * no query can contain it, and `findMatches` rejects the one route that could (a paste). */+/** Written at block boundaries. NUL because no query can contain it, bar a paste, which+ * `normalizeQuery` rejects. */export const BLOCK_SEPARATOR = "\u0000";-/** Ceiling on the flattened text. A thread is bounded by what a person scrolled through, a tool- * result is not: a Bash step can paste a megabyte of build log in. Scanning 4M costs about 2ms. */+/** A thread is bounded by what a person scrolled through; a tool result is not. */export const MAX_INDEX_CHARS = 4_000_000;-/** Ceiling on matches for one query. Every match is a `Range` the highlight registry has to paint,- * and a single letter in a long thread has thousands. */export const MAX_MATCHES = 5_000;-/** Ceiling on what ONE text node may contribute. A Bash step's log arrives as a single text node,- * and left to itself it spends the whole budget on the top of the document, so the messages the- * reader is looking at are not indexed at all. A share each, and the walk goes on. */+/** A log arrives as one text node and would otherwise spend the whole budget on its own. */export const MAX_NODE_CHARS = 100_000;/** Reserve space for visible portaled surfaces after walking the workspace. */export const PORTAL_RESERVE_CHARS = 100_000;-/** Elements whose subtree holds no findable text. Form controls carry theirs in `value`; a `Range`- * over `SVG` or `CANVAS` content is not paintable on every engine. */+/** Form controls carry their text in `value`; `SVG`/`CANVAS` content is not paintable everywhere. */const SKIP_TAGS: ReadonlySet<string> = new Set(["SCRIPT","STYLE",@@ -50,9 +43,7 @@ const SKIP_TAGS: ReadonlySet<string> = new Set(["EMBED",]);-/** Elements that end the line they sit on, so a separator goes in either side of them. A tag set,- * not `getComputedStyle`: this runs on every element in the thread. Being wrong about an exotic- * one inserts a break that was not needed, never invents a match. */+/** A tag set, not `getComputedStyle`: being wrong inserts a needless break, never a match. */const BLOCK_TAGS: ReadonlySet<string> = new Set(["ADDRESS","ARTICLE",@@ -97,16 +88,14 @@ const BLOCK_TAGS: ReadonlySet<string> = new Set(["UL",]);-// Re-exported so this module stays the one import for everything about the index.export {FIND_SCOPE_ATTRIBUTE,FIND_SKIP_ATTRIBUTE,} from "./find-attributes.ts";-/** Spaces that render as a space but are not one. Each is one UTF-16 unit, which keeps the map valid. */+/** Spaces that render as a space but are not one. Each is one UTF-16 unit, keeping the map valid. */const HARD_SPACE_PATTERN = /[\u00A0\u2002\u2003\u2007\u2009\u202F]/g;-/** Only the shape this module reads, so a test can hand it plain objects. */export interface FindTextNodeLike {readonly nodeType: number;readonly data: string;@@ -117,7 +106,6 @@ export interface FindElementLike {readonly tagName: string;readonly childNodes: ArrayLike<FindTextNodeLike | FindElementLike>;getAttribute(name: string): string | null;- /** Optional so a test can hand this plain objects; every browser we ship on has it. */checkVisibility?(options?: {contentVisibilityAuto?: boolean;opacityProperty?: boolean;@@ -130,13 +118,10 @@ export interface FindElementLike {export type FindNodeLike = FindTextNodeLike | FindElementLike;-/** One text node's contribution, at the offset its first character took in `text`. */export interface TextSegment {node: FindTextNodeLike;start: number;- /** Always the node's own `data.length`, so an offset inside the run maps straight through. */length: number;- /** True inside a `<pre>` or anything else that keeps its whitespace. See `findMatches`. */preserved: boolean;}@@ -147,12 +132,13 @@ interface IndexedSurface {}export interface FindTextIndex {- /** Case-folded haystack, block boundaries written as `BLOCK_SEPARATOR`. */text: string;/** Sorted by `start`, gapped wherever a separator was written. */segments: TextSegment[];- /** True when `MAX_INDEX_CHARS` stopped the walk early. */truncated: boolean;+ /** Offsets where the walk dropped text and the page runs on across the gap. A match may not+ * end on one: nothing indexed says where the grapheme it cuts through ends. */+ seams: ReadonlySet<number>;/** Where the portaled surfaces begin. The whole length when none are open. */rootLength: number;/** Stable portal roots and the offsets occupied by their searchable text. */@@ -163,26 +149,19 @@ export const EMPTY_TEXT_INDEX: FindTextIndex = {text: "",segments: [],truncated: false,+ seams: new Set<number>(),rootLength: 0,surfaces: [],};-/** Dotted I, the only code point whose `toLowerCase` grows (scanned all of them). Mapped to bare- * `i`, the Turkic fold: the default adds a combining dot, and one index char must stand for one- * document char. */+/** The only code point whose `toLowerCase` grows. Mapped to the Turkic fold, a bare `i`, since one+ * index char must stand for one document char. */const DOTTED_I_PATTERN = /\u0130/g;-/** Final sigma, mapped to medial as `CaseFolding.txt` does. `toLowerCase` picks between them by- * what follows, so only one of the two spellings a reader can type would match. One unit each. */+/** Mapped to medial as `CaseFolding.txt` does: `toLowerCase` picks by what follows, so otherwise+ * only one of the two spellings a reader can type would match. */const FINAL_SIGMA_PATTERN = /\u03c2/g;-/**- * Case-fold the flattened document without changing its length.- *- * The two mappings above are what the default fold misses. With dotted I gone first `toLowerCase`- * cannot change a length, so this is one pass: 11ms at the 4M ceiling, against 146ms walking code- * points.- */export function foldText(raw: string): string {const spaced = raw.replace(HARD_SPACE_PATTERN, " ")@@ -191,8 +170,7 @@ export function foldText(raw: string): string {if (folded.length === spaced.length) {return folded.replace(FINAL_SIGMA_PATTERN, "\u03c3");}- // Nothing reaches this: dotted I was the only expansion and it is gone by here. But a wrong- // length would misplace every offset after it, so fall back to the fold that cannot drift.+ // Unreachable, but a wrong length would misplace every offset after it.let plain = "";for (const point of spaced) {const lower = point.toLowerCase();@@ -206,9 +184,7 @@ function hasClassToken(element: FindElementLike, token: string): boolean {return (element.getAttribute("class") ?? "").split(/\s+/).includes(token);}function skipsByMarkup(element: FindElementLike): boolean {- // Uppercased first: only HTML elements report their tag that way. SVG and MathML keep their source- // casing, so an inline `<svg>` answers "svg" and walked straight past this set, putting Mermaid- // labels in the index as matches no engine can paint. Already uppercase for everything else.+ // Uppercased: SVG and MathML keep their source casing, so `<svg>` answers "svg" and walks past.if (SKIP_TAGS.has(element.tagName.toUpperCase())) return true;if (hasClassToken(element, "katex-mathml")) return true;if (element.getAttribute(FIND_SKIP_ATTRIBUTE) !== null) return true;@@ -223,31 +199,15 @@ function skipsByMarkup(element: FindElementLike): boolean {);}-/**- * True when the walk must not descend into this element.- *- * `style` is the element's resolved style, passed in by the walk so the read is made once per- * element rather than once here and once again in `visit` -- which is what the measurement quoted- * on `computedStyle` was taken to mean, and was not what the code did. Optional because the- * exported signature is one the tests call directly; resolving it here is the same answer.- */export function skipsSubtree(element: FindElementLike,style: ResolvedStyle | null = computedStyle(element),): boolean {if (skipsByMarkup(element)) return true;- // Anything the engine is not painting. Attributes miss the common case: `hidden lg:flex` is a- // CLASS, and text under it would be counted and walked to while nobody can see it.- //- // `contentVisibilityAuto` off: such a subtree is skipped, not hidden, and asking would drop the- // far half of a Hub README and of a maths thread, with nothing to put it back (scrolling renders- // without mutating, so the observer never fires). Opacity off, so a message fading in stays- // findable.- //- // Both spellings of each option: `visibilityProperty`/`opacityProperty` are renames of- // `checkVisibilityCSS`/`checkOpacity`, an engine reads only the name it knows, and Web IDL drops- // an unknown member silently. The modern name alone is a no-op on Chrome 105-120 and Firefox- // 106-121, which would then index and highlight `visibility: hidden` text.+ // `contentVisibilityAuto` off, since such a subtree is skipped rather than hidden and nothing+ // would put it back (scrolling renders without mutating, so the observer never fires); opacity+ // off, so a message fading in stays findable. Both spellings of each option, since an engine+ // reads only the name it knows: the modern one alone is a no-op on Chrome 105-120, Firefox 106-121.const painted = element.checkVisibility?.({contentVisibilityAuto: false,opacityProperty: false,@@ -256,15 +216,11 @@ export function skipsSubtree(checkVisibilityCSS: true,});if (painted === false) {- // `display: contents` has no box, and no box is the first thing `checkVisibility` calls- // invisible, so a wrapper whose children are all on screen answers false. The shell and the- // training page both use one, which is most of what there is to search. A real box is what- // makes an element hidden rather than absent.+ // No box is the first thing `checkVisibility` calls invisible, and the shell is built out of+ // `display: contents` wrappers whose children are all on screen.return style?.display !== "contents";}- // No `checkVisibility` to ask: it landed in Safari 17.4, and WebKitGTK is a supported engine- // here, so this is a real path and the visible branch would index every `display: none` subtree.- // `paintsNothing` mirrors what the call above asks for.+ // `checkVisibility` landed in Safari 17.4 and WebKitGTK is supported here, so this is a real path.if (painted === undefined && paintsNothing(style)) return true;return clippedAway(style);}@@ -277,14 +233,8 @@ interface ResolvedStyle {clipPath?: string;}-/**- * True when this element keeps its own text off screen while still being descended into.- *- * Only `display: contents` reaches this, and `skipsSubtree` lets it through on purpose. But- * `visibility` inherits, and only ELEMENT children are re-checked, so a direct text child of a- * hidden `contents` wrapper would be indexed and highlighted. Scoped to the element's own text:- * a descendant restoring `visibility: visible` is painted again and still reached.- */+/** `skipsSubtree` lets a `display: contents` wrapper through, but `visibility` inherits and only+ * ELEMENT children are re-checked, so a direct text child of a hidden one would be indexed. */function hidesOwnText(style: ResolvedStyle | null): boolean {return (style?.display === "contents" &&@@ -292,18 +242,14 @@ function hidesOwnText(style: ResolvedStyle | null): boolean {);}-/** True when this element paints no box, for engines with no `checkVisibility`. `display: contents`- * is boxless rather than hidden, so it is excluded here and `hidesOwnText` covers its own text,- * which is what the branch above does when the API answers. */+/** For engines with no `checkVisibility`. `display: contents` is boxless, not hidden. */function paintsNothing(style: ResolvedStyle | null): boolean {if (style?.display === "none") return true;if (style?.display === "contents") return false;return style?.visibility === "hidden" || style?.visibility === "collapse";}-/** The two spellings of Tailwind's `sr-only`: a real box, full opacity, clipped to nothing.- * `checkVisibility` calls that visible, so without this the app's 46 screen-reader labels are- * counted and walked to under a highlight clipped away with them. */+/** Tailwind's `sr-only`: a real box at full opacity, which `checkVisibility` calls visible. */function clippedAway(style: ResolvedStyle | null): boolean {return (style?.clipPath === "inset(50%)" ||@@ -311,13 +257,6 @@ function clippedAway(style: ResolvedStyle | null): boolean {);}-/**- * `display` and `white-space` as resolved, or null off the DOM where the tag sets stand in.- *- * Used for three things: telling a boxless wrapper from a hidden one, the block boundaries no tag- * name carries, and where whitespace is preserved. At 8000 elements: 2.5ms for the visibility check- * alone, 4.3ms with this, against a 300ms floor between rebuilds.- */function computedStyle(element: FindElementLike): ResolvedStyle | null {const view = globalThis as unknown as {getComputedStyle?: (element: FindElementLike) => ResolvedStyle;@@ -325,9 +264,7 @@ function computedStyle(element: FindElementLike): ResolvedStyle | null {return view.getComputedStyle?.(element) ?? null;}-/** True when this element ends the line it sits on. `inline-block` and friends do not, `contents`- * has no box. Being wrong inserts a needless separator, which can only lose a match and never- * invent one, so anything unrecognised counts as a boundary. */+/** Anything unrecognised is a boundary: a needless separator can lose a match, never invent one. */function isBlockDisplay(display: string | undefined): boolean {if (display === undefined) return false;return !(@@ -337,8 +274,7 @@ function isBlockDisplay(display: string | undefined): boolean {);}-/** True where whitespace is kept rather than collapsed. `pre-line` is excluded: it still collapses- * runs of spaces, which is the half the query flexibility below cares about. */+/** `pre-line` is excluded: it still collapses runs of spaces, which is the half that matters. */function preservesWhitespace(whiteSpace: string | undefined): boolean {return (whiteSpace === "pre" ||@@ -347,51 +283,47 @@ function preservesWhitespace(whiteSpace: string | undefined): boolean {);}-/**- * Flatten `root` into one case-folded string plus the map back to its text nodes.- *- * Recursive rather than a `TreeWalker`: the walker reports entering an element but not leaving one,- * and the closing separator is what stops `<p>a</p>b` reading as "ab".- */+/** Recursive rather than a `TreeWalker`, which reports entering an element but not leaving one, and+ * the closing separator is what stops `<p>a</p>b` reading as "ab". */export function buildTextIndex(root: FindElementLike,- /** Subtrees to index after `root`, in the order they sit in the document. Portaled surfaces. */extraRoots: readonly FindElementLike[] = [],): FindTextIndex {const parts: string[] = [];const segments: TextSegment[] = [];const surfaces: IndexedSurface[] = [];+ const seams = new Set<number>();+ /** The end of what has been indexed, kept to what `joinsAcross` reads. */+ let tail = "";+ /** What a cut dropped, readable here and nowhere later: it settles that cut's far side. */+ let dropped: string | null = null;let length = 0;- /** The index does not hold everything: a node was clipped, or the ceiling was reached. */let truncated = false;- /** The ceiling was reached, which is the only thing that stops the walk. */+ /** The ceiling, the only thing that stops the walk. */let full = false;- /** What `full` is measured against. Raised for the portaled surfaces below, which the workspace- * gives up a share of the budget for, and only when there is one of them to give it to. */let ceiling =MAX_INDEX_CHARS - (extraRoots.length > 0 ? PORTAL_RESERVE_CHARS : 0);// Written lazily, so a run of empty blocks costs nothing and no separator lands at either end.let pendingSeparator = false;const visit = (element: FindElementLike, inherited: boolean): void => {- // Markup first, so a subtree turned down on a tag or an attribute costs no layout read at all.- // Then one resolved style, shared with the rest of the skip rule: both halves need it, and- // resolving it separately in each doubled the cost the comment on `computedStyle` quotes.+ // Markup first, so a subtree turned down on a tag or attribute costs no layout read at all.if (skipsByMarkup(element)) return;const style = computedStyle(element);if (skipsSubtree(element, style)) return;- // The tag set answers `<br>`, whose display is inline. Layout is the rest: two `span.block`- // run together without a boundary, so "Open" above "AI" reads as one word.+ // The tag set answers `<br>`, whose display is inline; layout catches two stacked `span.block`.const block =BLOCK_TAGS.has(element.tagName) || isBlockDisplay(style?.display);- if (block) pendingSeparator = true;+ if (block) {+ pendingSeparator = true;+ dropped = null;+ }const preserved =style?.whiteSpace === undefined? inherited: preservesWhitespace(style.whiteSpace);- // Asked once per element, not per text node: a boxless wrapper that is also invisible paints- // none of its own text, and that text is the one thing `skipsSubtree` never gets to judge.+ // Its own text is the one thing `skipsSubtree` never gets to judge.const ownTextHidden = hidesOwnText(style);const children = element.childNodes;for (let i = 0; i < children.length; i += 1) {@@ -406,21 +338,40 @@ export function buildTextIndex(// `slice(0, negative)` takes all but the last character of the next node.if (length >= ceiling) {truncated = true;+ // The node is still there to be read, so its first code point settles the junction.+ if (!pendingSeparator && joinsAcross(tail, firstPointOf(data))) {+ seams.add(length);+ }full = true;return;}+ let separated = false;if (pendingSeparator) {pendingSeparator = false;if (length > 0) {parts.push(BLOCK_SEPARATOR);length += 1;+ tail = BLOCK_SEPARATOR;+ separated = true;}}- // A share, not all of it: the prefix keeps one huge node findable, while the whole- // remaining budget for it would leave out everything after, messages on screen included.- const take = Math.min(ceiling - length, MAX_NODE_CHARS);+ // The far side of a cut, settled by what it dropped.+ if (dropped !== null) {+ if (joinsAcross(dropped, firstPointOf(data))) seams.add(length);+ dropped = null;+ }+ // A share, not all: one huge node given the rest leaves out everything after it.+ let take = Math.min(ceiling - length, MAX_NODE_CHARS);+ // Never between the halves of a pair: keeping the leading one leaves a code unit that is+ // not a character, which reads as a grapheme of its own and lets a match end against it,+ // inside what the page draws as one. Dropping it instead makes the cut a real boundary.+ if (take > 0 && take < data.length && isPairedHalf(data, take))+ take -= 1;if (take <= 0) {truncated = true;+ if (!separated && joinsAcross(tail, firstPointOf(data))) {+ seams.add(length);+ }full = true;return;}@@ -428,10 +379,14 @@ export function buildTextIndex(parts.push(raw);segments.push({ node, start: length, length: raw.length, preserved });length += raw.length;- // Siblings still get indexed, but what was dropped must leave a boundary or the prefix- // runs into the next node and a match across that seam paints over the gap.+ tail = (tail + raw).slice(-JOIN_CONTEXT);+ // What was dropped must leave a boundary, or a match across the seam paints over the gap.if (raw.length < data.length) {truncated = true;+ // Both edges settle from the node: the next code point here, the dropped tail later.+ if (joinsAcross(raw, firstPointOf(data.slice(take))))+ seams.add(length);+ dropped = data.slice(-JOIN_CONTEXT);pendingSeparator = true;}} else if (child.nodeType === ELEMENT_NODE) {@@ -439,7 +394,11 @@ export function buildTextIndex(if (full) return;}}- if (block) pendingSeparator = true;+ // As on the way in: what a clip inside this block dropped cannot reach the sibling after it.+ if (block) {+ pendingSeparator = true;+ dropped = null;+ }};visit(root, false);@@ -452,8 +411,10 @@ export function buildTextIndex(full = false;for (const extra of extraRoots) {if (full) break;- // A boundary between roots: they are separate surfaces, and a match must not run across.+ // Its own surface, so a boundary whatever the last root ended on, and whatever tags either of+ // them happen to use: nothing dropped back there can reach into what a portal paints.pendingSeparator = true;+ dropped = null;const firstSegment = segments.length;visit(extra, false);if (segments.length > firstSegment) {@@ -469,6 +430,7 @@ export function buildTextIndex(text: foldText(parts.join("")),segments,truncated,+ seams,rootLength,surfaces,};@@ -521,8 +483,7 @@ export function renumbersMatches(return !workspaceGrewAtTail || after.rootLength !== before.rootLength;}-/** Fold a query the way the haystack was folded. Null when it cannot match: empty, or carrying the- * separator, which only a paste could produce. */+/** Null when the query cannot match: empty, or carrying the separator (only a paste can). */export function normalizeQuery(query: string): string | null {if (query.length === 0) return null;const folded = foldText(query);@@ -536,24 +497,12 @@ export interface FindMatch {end: number;}-/** Regex metacharacters, so a query of "a.b" does not match "axb". */const REGEX_META_PATTERN = /[.*+?^${}()|[\]\\]/g;-/** Combining dot above, the tail of a decomposed dotted I. */const COMBINING_DOT = "̇";-/**- * The canonically equivalent spellings of `needle`, longest first so the fuller one wins.- *- * Composed and decomposed spellings look identical on screen and both turn up in one thread.- * Normalizing the index would change its length, and every offset stands for one document- * character, so the variants go into the pattern instead and the document is left alone.- *- * `dotted` adds the spelling a decomposed dotted I leaves behind. U+0130 decomposes to `I` plus a- * combining dot, which folds to `i` plus that dot, and `i` + dot has no precomposed form, so NFC- * cannot put it back and the plain query misses a word plainly on screen. Only offered when the- * index carries a combining dot, so an ordinary document keeps the single-variant `indexOf` path.- */+/** Longest first. Normalizing the index instead would change its length, and every offset stands+ * for one document character. */function canonicalVariants(needle: string, dotted: boolean): string[] {const variants = [needle];for (const form of ["NFC", "NFD"] as const) {@@ -623,37 +572,49 @@ function partiallyComposedHangul(cluster: string): string | null {return partial === cluster ? null : partial;}+/** The second half of a surrogate pair, and only where there is a pair: a low surrogate on its own+ * is a character in its own right, and one can reach a page through JSON or a pasted log. Taking+ * it for half of something joined it to whatever preceded it, hiding both. */+function isPairedHalf(text: string, at: number): boolean {+ const low = text.charCodeAt(at);+ if (!(low >= 0xdc00 && low <= 0xdfff) || at === 0) return false;+ const high = text.charCodeAt(at - 1);+ return high >= 0xd800 && high <= 0xdbff;+}+/**- * The needle as a regex source in which each cluster may match either of its spellings.- *- * Alternating whole spellings of the WHOLE query only reaches text that is all-composed or- * all-decomposed. A single occurrence can be neither: joining two text nodes joins two sources, so- * `café` in one and `café` in the next make one visible word with a spelling the query- * cannot be written in. Every engine's own find matches that (measured on chromium, firefox and- * webkit), so it is below the baseline the rest of this file is held to.- *- * Per cluster rather than per query, which is also SMALLER: the old form repeated the query once- * per spelling, this writes it once and pays about eight characters for each cluster that actually- * has two spellings. Decomposed first, since it is the longer of the two.+ * Per cluster, because alternating whole spellings of the WHOLE query reaches only all-composed or+ * all-decomposed text, and one occurrence can be neither: joining two text nodes joins two sources,+ * so `café` in one and `café` in the next make one visible word with a spelling the+ * query cannot be written in. Every engine's own find matches it.*/function canonicalSource(needle: string, dotted: boolean): string {let out = "";for (const [cluster] of needle.normalize("NFD").matchAll(CLUSTER_PATTERN)) {if (/^\s/.test(cluster)) {- // A whitespace run in the query matches a run in the document, however it is spelt there.+ // The space flexes, what is attached to it does not: a mark on a space is part of that+ // grapheme, and dropping it left the match ending inside one, which the fence then threw+ // away. Only one `\s+` for a run of them, so the marks of the last still follow it.out += out.endsWith("\\s+") ? "" : "\\s+";+ out += escapeForRegex(cluster.slice(1));continue;}const spellings = [cluster];const composed = cluster.normalize("NFC");if (composed !== cluster) spellings.push(composed);- // Hangul composes in two steps, and text can stop after the first.+ // Hangul composes in two steps, and text can stop after the first. Joining two text nodes+ // produces exactly that, an LV syllable in one and its trailing Jamo in the next.const partial = partiallyComposedHangul(cluster);if (partial !== null && !spellings.includes(partial))spellings.push(partial);// A decomposed dotted I folds to `i` plus a combining dot, which has no precomposed form, so// NFC cannot put it back and the plain query would miss a word plainly on screen.if (dotted && cluster === "i") spellings.push(`i${COMBINING_DOT}`);+ // Longest first, as `canonicalVariants` is: alternation takes the first that fits, so a short+ // spelling that is a prefix of a long one wins and the rest of the cluster is left outside the+ // match: `i` before `i` plus its combining dot ended the match inside the grapheme, and the+ // boundary check threw the occurrence away rather than reaching for the longer spelling.+ if (spellings.length > 1) spellings.sort((a, b) => b.length - a.length);const spellingSource =spellings.length === 1? escapeForRegex(spellings[0])@@ -668,17 +629,216 @@ function canonicalSource(needle: string, dotted: boolean): string {return out;}-function escapeForRegex(text: string): string {- return text.replace(REGEX_META_PATTERN, "\\$&");+/** The index's segmentation, made at the first match that needs one and kept for as long as the+ * index lives. Making it walks nothing: `containing` seeks to the offset asked about, so a 4M+ * index costs 4ms once and a fraction of a microsecond a question. */+const segmentsCache = new WeakMap<FindTextIndex, GraphemeSegments>();++/** Every boundary in the index, once seeking for them has cost more than walking the lot would.+ *+ * In time, not in seeks: a seek is 0.2us into a page of Hangul and 1236us into a page of flags,+ * so any count is far too small for one and far too large for the other, and the large end cost+ * a first search twenty seconds. The budget is what a scan of this index would itself cost, so+ * the total stays within twice the better of the two and no constant is left to be wrong about.+ * The rate is the slower of the two measured, 1.3M characters scanned in 82ms. */+const boundaryCache = new WeakMap<FindTextIndex, Uint8Array>();+const seekCosts = new WeakMap<+ FindTextIndex,+ { spent: number; since: number; seen?: number }+>();+const SCAN_CHARS_PER_MS = 16_000;+const MIN_SEEK_BUDGET_MS = 8;+/** Timed in blocks, so the clock is read twice per block rather than twice per seek. The whole+ * block is measured, so what accumulates is the real cost and not a sample of it. */+const SEEK_BLOCK = 32;++/** Drop a block left open by a search that ended inside one: it is wall time, and between two+ * searches that is the reader thinking, which was being billed to the next query. What is lost+ * is under a block of seeks, which the budget will not miss. */+function endSeekWindow(index: FindTextIndex): void {+ const cost = seekCosts.get(index);+ if (cost === undefined) return;+ cost.seen = (cost.seen ?? 0) - ((cost.seen ?? 0) % SEEK_BLOCK);+ cost.since = 0;+}++/** The one thing below U+0300 that joins (GB3), and so the one exception to the fast path below.+ * Everywhere whitespace is collapsed a newline is its own grapheme; in a `<pre>` the pair arrives+ * intact and a search for the feed alone landed between them. */+function splitsCrlf(text: string, at: number): boolean {+ return at > 0 && text.charCodeAt(at - 1) === 13 && text.charCodeAt(at) === 10;+}++/** Anything that could extend or be extended into a grapheme. See `alignsToGraphemes`. */+const JOINS_GRAPHEME = /[^\u0000-\u02ff]/;++interface GraphemeSegments {+ containing(at: number): { index: number } | undefined;+ [Symbol.iterator](): IterableIterator<{ index: number }>;+}++let segmenter: { segment(input: string): GraphemeSegments } | null | undefined;++/** The platform's own grapheme segmenter, or null where there is none. */+function graphemeSegmenter() {+ if (segmenter !== undefined) return segmenter;+ const scope = globalThis as unknown as {+ Intl?: { Segmenter?: new (locale?: string, options?: object) => never };+ };+ segmenter =+ typeof scope.Intl?.Segmenter === "function"+ ? new scope.Intl.Segmenter(undefined, { granularity: "grapheme" })+ : null;+ return segmenter;}/**- * A pattern for a query spanning whitespace or spelt more than one way, or null for a plain scan.+ * Whether this engine's `containing` agrees with its own iterator about where a segment starts.*- * HTML collapses whitespace, so a soft-wrapped paragraph renders as one line while its node holds- * the newline; each run in the query matches a run in the document. The separator is not- * whitespace, so block boundaries stay closed. Single-word ASCII queries keep the `indexOf` path.+ * WebKit's answers the segment that ENDS at the offset: over `x` and a thumbs up it reads 0, 0, 1+ * where Chromium and Firefox read 0, 1, 1, so every interior boundary comes back "not a boundary".+ * Where it is wrong the boundaries are tabulated instead, which is the same answer more slowly.*/+let seeksBoundaries: boolean | undefined;+function segmenterSeeksBoundaries(platform: {+ segment(input: string): GraphemeSegments;+}): boolean {+ if (seeksBoundaries !== undefined) return seeksBoundaries;+ try {+ const probe = platform.segment("x\u{1f44d}");+ seeksBoundaries =+ probe.containing(1)?.index === 1 && probe.containing(0)?.index === 0;+ } catch {+ seeksBoundaries = false;+ }+ return seeksBoundaries;+}++const JOIN_CONTEXT = 32;++/** As much of the end of `before` as segments the same alone as in the whole string: back to a+ * character below U+0300, which always begins a cluster. A fixed-size window would not do, since+ * regional indicators pair off from the START of a run and an odd tail reads as even. */+function exactTail(before: string): string | null {+ const from = Math.max(0, before.length - JOIN_CONTEXT);+ for (let at = before.length - 1; at >= from; at -= 1) {+ if (before.charCodeAt(at) < 0x300) return before.slice(at);+ }+ return null;+}++/** The first whole code point of `text`, or "" when there is none. */+function firstPointOf(text: string): string {+ return text.length === 0+ ? ""+ : String.fromCodePoint(text.codePointAt(0) as number);+}++/** Whether the grapheme `before` ends on runs on into `point`: the one question a cut has to+ * answer. False without a segmenter, which leaves the search where it is today. */+function joinsAcross(before: string, point: string): boolean {+ if (before.length === 0 || point.length === 0) return false;+ const platform = graphemeSegmenter();+ if (platform === null) return false;+ const window = exactTail(before);+ // No anchor, so unknown rather than open: costs an occurrence ending exactly inside a run.+ if (window === null) return true;+ const at = window.length;+ const body = window + point;+ if (segmenterSeeksBoundaries(platform)) {+ return platform.segment(body).containing(at)?.index !== at;+ }+ for (const { index: start } of platform.segment(body)) {+ if (start === at) return false;+ if (start > at) break;+ }+ return true;+}++/**+ * True when `[start, end)` begins and ends where a grapheme does.+ *+ * Asked of the platform, which knows the whole of UAX 29 and is kept current with it; enumerating+ * the ranges here kept missing one more way to land inside a cluster every round. Asked one offset+ * at a time, so neither the size of the index nor where the match landed in it costs anything:+ * tabulating a block's boundaries instead paid 250ms per block, and paid it again on every reindex.+ */+function alignsToGraphemes(+ index: FindTextIndex,+ start: number,+ end: number,+): boolean {+ const text = index.text;+ // Before the cheap test: at the end of a truncated index there is no `text[end]` to read.+ if (+ index.seams.size > 0 &&+ (index.seams.has(start) || index.seams.has(end))+ ) {+ return false;+ }+ // Almost every match is in text that cannot join at either edge, and asking the segmenter costs+ // far more than looking. Nothing below U+0300 joins: the lowest combining mark is U+0300, the+ // lowest spacing mark U+0903, Prepend starts at U+0600, Hangul Jamo at U+1100, and everything+ // astral arrives as a surrogate. Both sides of each edge, since the query can end with one.+ if (+ !splitsCrlf(text, start) &&+ !splitsCrlf(text, end) &&+ !(start > 0 && JOINS_GRAPHEME.test(text[start - 1])) &&+ !JOINS_GRAPHEME.test(text[start]) &&+ !JOINS_GRAPHEME.test(text[end - 1]) &&+ !(end < text.length && JOINS_GRAPHEME.test(text[end]))+ ) {+ return true;+ }+ return startsGrapheme(index, start) && startsGrapheme(index, end);+}++/** True when a grapheme starts at `at`, and true where there is no segmenter to ask: Firefox+ * shipped one in 125, and hand-rolling UAX 29 for the versions behind it is a second Unicode+ * implementation to keep current, against those readers keeping today's behaviour. */+function startsGrapheme(index: FindTextIndex, at: number): boolean {+ const text = index.text;+ if (at === 0 || at === text.length) return true;+ const platform = graphemeSegmenter();+ if (platform === null) return true;+ const marked = boundaryCache.get(index);+ if (marked !== undefined) return marked[at] === 1;+ let segments = segmentsCache.get(index);+ if (segments === undefined) {+ segments = platform.segment(text);+ segmentsCache.set(index, segments);+ }+ let cost = seekCosts.get(index);+ if (cost === undefined) {+ cost = { spent: 0, since: 0 };+ seekCosts.set(index, cost);+ }+ const budget = Math.max(MIN_SEEK_BUDGET_MS, text.length / SCAN_CHARS_PER_MS);+ if (segmenterSeeksBoundaries(platform) && cost.spent <= budget) {+ if (cost.since === 0) cost.since = performance.now();+ const answer = segments.containing(at)?.index === at;+ cost.seen = (cost.seen ?? 0) + 1;+ if (cost.seen % SEEK_BLOCK === 0) {+ cost.spent += performance.now() - cost.since;+ cost.since = 0;+ }+ return answer;+ }+ // Past the budget, and a capped search anchored near the end walks the candidates up to three+ // times, so this is bought once and answers every question after it.+ const marks = new Uint8Array(text.length + 1);+ for (const { index: start } of segments) marks[start] = 1;+ marks[text.length] = 1;+ boundaryCache.set(index, marks);+ return marks[at] === 1;+}++function escapeForRegex(text: string): string {+ return text.replace(REGEX_META_PATTERN, "\\$&");+}++/** Null for a plain scan. Whitespace flexes because a soft-wrapped paragraph renders as one line+ * while its node holds the newline; the separator is not whitespace, so blocks stay shut. */function matchPattern(variants: string[], needle: string): RegExp | null {const dotted = variants.some((variant) => variant.includes(COMBINING_DOT));if (@@ -689,29 +849,19 @@ function matchPattern(variants: string[], needle: string): RegExp | null {return null;try {const pattern = new RegExp(canonicalSource(needle, dotted), "g");- // V8 compiles lazily, so an oversized pattern is accepted here and throws on the first `exec`- // instead, back outside this `try`. One run against nothing forces the compile while it can- // still be caught, and leaves `lastIndex` at 0 for the real scan.+ // V8 compiles lazily, so an oversized pattern is accepted here and throws on the first `exec`,+ // outside this `try`. One run against nothing forces the compile while it is catchable.pattern.exec("");return pattern;} catch {- // Every engine caps how large a pattern it will compile, and the cap is its own business:- // the spec sets none, so there is no length to test against that would be right everywhere.- // A pasted log reaches it -- measured at 15,651 characters on V8 -- and the throw came out- // through the keystroke that caused it and took the bar down with it. Falling back to the- // literal scan below costs the flexed whitespace and keeps the search working.+ // Every engine caps how large a pattern it compiles and the spec sets none, so there is no+ // length to test against. A pasted log reaches it, and the throw took the bar down with it.return null;}}-/**- * Walk every match for `needle` in document order, stopping when `visit` says so. Non-overlapping,- * like every browser's own find, which is what terminates a self-overlapping query.- *- * One place, so the two ways of matching stay one behaviour. Flexed whitespace is for prose; inside- * a `<pre>` the whitespace on screen IS the whitespace in the node, so "foo bar" must not land on- * "foo bar" there. The platform's own find draws the same line.- */+/** Non-overlapping, like every browser's own find, which terminates a self-overlapping query.+ * Inside a `<pre>` the whitespace on screen IS the whitespace in the node, so it cannot flex. */function eachMatch(index: FindTextIndex,needle: string,@@ -721,17 +871,12 @@ function eachMatch(needle,index.text.includes(COMBINING_DOT),);- // Nothing longer than the haystack can be inside it. Measured against the SHORTEST spelling,- // since a decomposed query is longer than the precomposed text it is meant to find. This is- // ahead of `matchPattern` because that is the expensive half: escaping a pasted log, repeating- // it once per variant and handing the result to the regex compiler.+ // Against the SHORTEST spelling: a decomposed query is longer than the text it is meant to find.if (Math.min(...variants.map((variant) => variant.length)) > index.text.length)return;- // What counts as "spelt the way it was typed": any whole-query spelling, or any mixture of the- // per-cluster ones, which compare equal once composed. Whitespace flexing survives NFC, so a hit- // that only flexed a space is still told apart from one that merely spells a letter differently.+ // "Spelt as typed" survives NFC, so a hit that only flexed a space is still told apart.const composedNeedle = needle.normalize("NFC");const asTyped = (hit: string): boolean =>variants.includes(hit) || hit.normalize("NFC") === composedNeedle;@@ -741,7 +886,11 @@ function eachMatch(const hit = pattern.exec(index.text);if (hit === null) return;const end = hit.index + hit[0].length;- // Any spelling of the query counts as typed exactly; only flexed whitespace does not.+ // Never part way through a grapheme, whichever way the match was found.+ if (!alignsToGraphemes(index, hit.index, end)) {+ pattern.lastIndex = hit.index + 1;+ continue;+ }if (touchesPreserved(index.segments, hit.index, end) &&!asTyped(hit[0])@@ -757,12 +906,16 @@ function eachMatch(for (;;) {const at = index.text.indexOf(needle, from);if (at === -1) return;- if (!visit(at, at + needle.length)) return;- from = at + needle.length;+ const end = at + needle.length;+ if (!alignsToGraphemes(index, at, end)) {+ from = at + 1;+ continue;+ }+ if (!visit(at, end)) return;+ from = end;}}-/** `limit` matches starting from the `skip`-th one. */function collectMatches(index: FindTextIndex,needle: string,@@ -780,17 +933,8 @@ function collectMatches(return out;}-/**- * Matches for `query`, at most `limit` of them, as a window around `anchor`.- *- * A common letter in a long thread has tens of thousands of matches, and keeping the first `limit`- * keeps only the top of the document, walking a reader at the bottom away from the occurrences- * beside them. When the cap bites the kept matches are the ones nearest the reader; under the cap- * this is the same single pass it always was.- *- * `anchor` may be a thunk, because working it out reads layout and a plain argument is evaluated- * whether or not it is wanted. A number still means the same thing.- */+/** At most `limit` matches, as a window around `anchor`: keeping the first `limit` instead keeps+ * only the top of the document. `anchor` may be a thunk, because working it out reads layout. */export function findMatches(index: FindTextIndex,query: string,@@ -799,20 +943,15 @@ export function findMatches(): FindMatch[] {const needle = normalizeQuery(query);if (needle === null) return [];+ endSeekWindow(index);const head = collectMatches(index, needle, limit, 0);// Before resolving the anchor, so an under-cap query never pays for it.if (head.length < limit) return head;const at = typeof anchor === "function" ? anchor() : anchor;if (at <= 0) return head;- // Capped, so where the window sits matters. Two more passes, no allocation in the first.- //- // The count stops early: `total` only keeps the window from running off the end, and once- // `total - limit` reaches the left edge it can no longer pull it back, so later matches are- // counted for nothing. Past the anchor `before` is final, which is what makes the edge knowable.- // The clamp is safe because it only binds near the end, where the true total is under the- // ceiling and the walk never stops early. At 4M chars, 444,444 matches, anchored halfway:- // 6.4ms over all of them to 3.3ms over 224,722, same window either way.+ // The count stops early: `total` only keeps the window off the end, and once `total - limit`+ // reaches the left edge it can no longer pull it back. Past the anchor `before` is final.let total = 0;let before = 0;let enough = Number.POSITIVE_INFINITY;@@ -827,7 +966,6 @@ export function findMatches(}return total < enough;});- // Centred on the reader, then pushed back inside the list at either end.const start = Math.min(Math.max(before - (limit >> 1), 0),Math.max(total - limit, 0),@@ -835,28 +973,14 @@ export function findMatches(return start === 0 ? head : collectMatches(index, needle, limit, start);}-/**- * Throw away the one match asked for over the cap, from whichever end the reader is further from.- *- * The caller asks for `MAX_MATCHES + 1` so that a page holding exactly the cap does not have to- * read as a floor, then throws the extra one away. Taking it off the tail is right only while the- * window starts at the top of the document. Once the reader is far enough down the anchored window- * IS the tail, and its last entry is the document's final occurrence -- the one nearest them, and- * the one the walk was then unable to reach at all.- *- * Measured on 6000 matches with the reader at the bottom: the window covered the final occurrence- * at offset 11998, and trimming the tail left the walk ending at 11996.- *- * Lives here rather than beside its caller for the same reason `mutatesSearchableText` does: the- * hook imports React and cannot be loaded under `node --test`.- */+/** Drop the one match asked for over the cap, from whichever end the reader is further from. Taking+ * it off the tail is right only while the window starts at the top of the document. */export function dropProbeFurthestFrom(matches: FindMatch[],anchor: number | null,limit = MAX_MATCHES,): void {- // No anchor means `findMatches` never resolved one, which only happens under the cap. A window- // that already starts at the document's first match has nothing above the reader to give up.+ // No anchor means the window starts at the document's first match, with nothing above to give up.if (anchor !== null &&matches.length > 0 &&@@ -868,7 +992,6 @@ export function dropProbeFurthestFrom(matches.length = limit;}-/** True when `[start, end)` reaches into a run whose whitespace is kept rather than collapsed. */function touchesPreserved(segments: TextSegment[],start: number,@@ -888,7 +1011,6 @@ function touchesPreserved(return false;}-/** The segment holding `offset`, or -1 when it lands on a separator or past the end. */export function segmentAt(segments: TextSegment[], offset: number): number {let lo = 0;let hi = segments.length - 1;@@ -906,13 +1028,11 @@ export function segmentAt(segments: TextSegment[], offset: number): number {return -1;}-/** A text node and an offset inside it, which is what a `Range` boundary takes. */export interface TextPosition {node: FindTextNodeLike;offset: number;}-/** Where a match starts. */export function startPositionAt(segments: TextSegment[],offset: number,@@ -923,10 +1043,8 @@ export function startPositionAt(return { node: segment.node, offset: offset - segment.start };}-/**- * Where a match ends. Located from its last character, since an exclusive end sits one past the run- * whenever the match finishes a text node, and that is the boundary `setEnd` wants there.- */+/** Located from the match's last character: an exclusive end sits one past the run whenever the+ * match finishes a text node, which is the boundary `setEnd` wants there. */export function endPositionAt(segments: TextSegment[],end: number,diff --git a/studio/frontend/tests/find-in-page.test.ts b/studio/frontend/tests/find-in-page.test.tsindex 8afb809c998..3eb57112f63 100644--- a/studio/frontend/tests/find-in-page.test.ts+++ b/studio/frontend/tests/find-in-page.test.ts@@ -1,15 +1,13 @@// SPDX-License-Identifier: AGPL-3.0-only// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0-// Find in page. What has to hold is that the flat index and the document agree: every offset the-// search reports has to land on the character a reader sees, or the highlight paints over the wrong-// word and the walk sends them somewhere they did not ask to go.-//-// There is no DOM library in this project. The runner is `node --test` and every sibling test that-// needs a document hand-rolls one, which is what the flatten's structural types are for. The rest-// of the DOM half is covered in smoke-find-in-page.tsx.+// Find in page. What has to hold is that the flat index and the document agree: every offset+// the search reports lands on the character a reader sees. There is no DOM library here, so+// the runner is `node --test` over a hand-rolled document.import assert from "node:assert/strict";+import { spawnSync } from "node:child_process";+import { readFileSync } from "node:fs";import { readFile } from "node:fs/promises";import test from "node:test";@@ -44,7 +42,16 @@ import {startPositionAt,} from "../src/features/find-in-page/lib/find-text-index.ts";-// --- the hand-rolled tree ----------------------------------------------------------------------+/** The feature's component module as one string. */+async function readComponentSource(): Promise<string> {+ return await readFile(+ new URL(+ "../src/features/find-in-page/components/find-in-page.tsx",+ import.meta.url,+ ),+ "utf8",+ );+}function text(data: string): FindTextNodeLike {return { nodeType: 3, data };@@ -64,11 +71,7 @@ function el(};}-// --- the flatten --------------------------------------------------------------------------------test("inline markup does not break a word", () => {- // The case the whole feature turns on: markdown emphasis, code spans and links split a word- // across text nodes, and a find that searched node by node would not see it.const root = el("DIV", [el("P", [text("un"), el("EM", [text("sloth")]), text(" studio")]),]);@@ -84,7 +87,6 @@ test("a block boundary stops a match running across it", () => {]);const index = buildTextIndex(root);assert.equal(index.text, `the end${BLOCK_SEPARATOR}start of the next`);- // Neither the run-together form nor the spaced one: the two words are not adjacent on screen.assert.deepEqual(findMatches(index, "endstart"), []);assert.deepEqual(findMatches(index, "end start"), []);});@@ -99,12 +101,9 @@ test("a subtree the walk must not read contributes nothing", () => {["SCRIPT", {}],["STYLE", {}],["TEXTAREA", {}],- // The bar itself, so the query typed into it is not a match of itself.["DIV", { [FIND_SKIP_ATTRIBUTE]: "" }],- // A workspace the shell parks off-route rather than unmounting.["DIV", { inert: "" }],["DIV", { hidden: "" }],- // The rest of the page for as long as a modal is up.["DIV", { "aria-hidden": "true" }],] as const) {const root = el("DIV", [@@ -174,8 +173,7 @@ test("an offset maps back to the node and character it came from", () => {const end = endPositionAt(index.segments, match.end);assert.equal(start?.node, second);assert.equal(start?.offset, 0);- // The match finishes the node, so the end boundary is its length -- the one offset no segment- // holds, and the one a Range needs there.+ // The end boundary is the node's length: the one offset no segment holds.assert.equal(end?.node, second);assert.equal(end?.offset, 6);});@@ -201,9 +199,7 @@ test("an offset on a separator belongs to no node", () => {});test("dotted I folds to a plain i, and the offsets still hold", () => {- // Its default fold is two code units, which one index character cannot stand for. The Turkic fold- // is a bare `i`, which fits and is what a search wants: the platform's own find matches all four- // spellings of Istanbul against a visible dotted one, and now so does this.+ // The Turkic fold is a bare `i`, which fits in one unit.assert.equal("İ".toLowerCase().length, 2, "premise: the default fold grows");assert.equal(foldText("İ"), "i");@@ -214,7 +210,6 @@ test("dotted I folds to a plain i, and the offsets still hold", () => {assert.equal(findMatches(spellings, query).length, 1, query);}- // And the offsets stay true, which is what folding it in place buys.const marker = text("İ");const after = text("Unsloth");const index = buildTextIndex(el("DIV", [el("P", [marker, after])]));@@ -226,33 +221,24 @@ test("dotted I folds to a plain i, and the offsets still hold", () => {});test("an expanding fold does not change the letters around it", () => {- // Greek final sigma is decided by what follows it, so the fold of a run is not the folds of its- // code points strung together. One expanding character in a node used to force the per-code-point- // path for the whole run, turning every sigma in it into the wrong letter: text plainly on screen- // then matched nothing.+ // The fold of a run is not the folds of its code points strung together.const dottedI = String.fromCharCode(0x0130); // Turkish dotted I, whose fold is two unitsconst greek = `\u039f\u0394\u039f\u03a3 ${dottedI} \u039f\u03a3`; // "ΟΔΟΣ İ ΟΣ"const index = buildTextIndex(el("DIV", [el("P", [text(greek)])]));// The length has to hold, or every offset after it maps to the wrong character.assert.equal(index.text.length, greek.length);assert.equal(index.segments[0].length, greek.length);- // Both sigmas are word-final here, and both fold to the one form a query folds to.assert.equal(findMatches(index, "\u039f\u03a3").length, 2);assert.equal(findMatches(index, "\u039f\u0394\u039f\u03a3").length, 1);- // And the character that would have grown is one plain `i`, still one wide.assert.equal(index.text, "\u03bf\u03b4\u03bf\u03c3 i \u03bf\u03c3");});test("casing context carries across inline markup", () => {- // Split by an `<em>`, a word-final sigma folds medial per node and final over the run, so the two- // used to disagree about a word plainly on screen. Both sigmas now fold to one letter, which- // settles it whichever way the flatten arrives at the run.const index = buildTextIndex(el("DIV", [el("P", [text("\u039f"), el("EM", [text("\u03a3")])])]),);assert.equal(index.text, "\u03bf\u03c3");assert.equal(findMatches(index, "\u039f\u03a3").length, 1);- // The offset map still lands on the right nodes, which is what folding in place buys.assert.equal(index.segments.length, 2);assert.equal(index.segments[1].start, 1);});@@ -262,15 +248,11 @@ test("several dotted I in a run fold without drift", () => {const raw = `${dottedI}a${dottedI}\u039f\u03a3${dottedI}b`;const index = buildTextIndex(el("DIV", [el("P", [text(raw)])]));assert.equal(index.text.length, raw.length);- // Each one is a plain `i`, and the sigma between them still reads its own context: a cased letter- // follows it, so it stays medial.assert.equal(index.text, "iai\u03bf\u03c3ib");});test("either sigma finds the other, whichever one is on screen", () => {- // `toLowerCase` picks the final form by position, so uppercase Greek ending in sigma folded one- // way and a query typed with the medial sigma folded the other, and half the spellings a reader- // can produce found nothing. Measured: chromium, firefox and webkit all match `ΟΣ` from either.+ // `toLowerCase` picks the final form by position, so half the spellings folded the other way.const index = buildTextIndex(el("DIV", [el("P", [text("\u039f\u0394\u039f\u03a3 \u039f\u03a3")])]),);@@ -285,7 +267,6 @@ test("either sigma finds the other, whichever one is on screen", () => {`${escape(query)} found nothing`,);}- // One letter in the index, so the offsets still stand for what is written.const run = "\u03a3\u03a3\u03a3";const sigmas = buildTextIndex(el("DIV", [el("P", [text(run)])]));assert.equal(sigmas.text, "\u03c3\u03c3\u03c3");@@ -293,42 +274,33 @@ test("either sigma finds the other, whichever one is on screen", () => {});test("a non-breaking space answers to the space key", () => {- // Spelled with a char code rather than pasted: a literal U+00A0 in a source file looks like a- // space to every reader and every diff, which is the confusion this guards against.+ // Spelled with a char code: a literal U+00A0 in source looks like a space to every reader.const nbsp = String.fromCharCode(0x00a0);const index = buildTextIndex(el("DIV", [el("P", [text(`Unsloth${nbsp}Studio`)])]),);assert.equal(findMatches(index, "unsloth studio").length, 1);- // Substituted one for one, so every offset after it is untouched.assert.equal(index.text.length, "Unsloth Studio".length);assert.equal(index.text.includes(nbsp), false);});test("a dotted I does not make the rest of its run case-sensitive", () => {- // Its default fold grows, and giving up on the whole run for that left ordinary words in the- // same node unmatchable.const index = buildTextIndex(el("DIV", [el("P", [text("HELLO İ")])]));assert.equal(findMatches(index, "hello").length, 1);- // And the offsets still line up, which is what the whole-run fallback was protecting.const [match] = findMatches(index, "hello");assert.equal(startPositionAt(index.segments, match.start)?.offset, 0);});test("a text node bigger than its share contributes its prefix", () => {- // One Bash step's log arrives as one text node. Dropping it whole indexed nothing at all.const node = text(`unsloth ${"x".repeat(MAX_NODE_CHARS + 10)}`);const index = buildTextIndex(el("DIV", [el("P", [node])]));assert.equal(index.truncated, true);assert.equal(index.text.length, MAX_NODE_CHARS);assert.equal(findMatches(index, "unsloth").length, 1);- // The prefix maps back to the node it came from, so a match in it is reachable.assert.equal(startPositionAt(index.segments, 0)?.node, node);});test("an oversized node does not take the whole budget with it", () => {- // It used to: the node claimed every remaining character, the walk stopped, and the messages the- // reader was looking at were not in the index at all. A share each, and the walk goes on.const log = el("PRE", [text("x".repeat(MAX_INDEX_CHARS + 1000))]);const onScreen = el("P", [text("the message in front of the reader says unsloth"),@@ -336,14 +308,11 @@ test("an oversized node does not take the whole budget with it", () => {const index = buildTextIndex(el("DIV", [log, onScreen]));assert.equal(index.truncated, true);assert.equal(findMatches(index, "in front of the reader").length, 1);- // The log is still there, up to its share.assert.equal(index.text.startsWith("x".repeat(MAX_NODE_CHARS)), true);});test("a popover over a document at the ceiling is still searchable", () => {- // The workspace filling the budget used to end the walk, and the portal roots come after it, so- // the one surface the reader is actually looking at fell out of the index entirely. That is the- // case portal support exists for, so it gets a reserve rather than the leftovers.+ // A workspace filling the budget used to end the walk, leaving the portal unindexed.const filler = Array.from({ length: 50 }, () =>el("P", [text("x".repeat(MAX_NODE_CHARS))]),);@@ -354,13 +323,11 @@ test("a popover over a document at the ceiling is still searchable", () => {});test("the reserve is only held back when there is a portal to hold it for", () => {- // With nothing portaled the workspace gets the whole budget, so the ceiling means what it says.const filler = Array.from({ length: 50 }, () =>el("P", [text("x".repeat(MAX_NODE_CHARS))]),);const alone = buildTextIndex(el("DIV", filler));assert.equal(alone.text.length, MAX_INDEX_CHARS);- // And with one, the workspace gives up only the reserve, not more.const withPopover = buildTextIndex(el("DIV", filler), [el("DIV", [el("P", [text("unsloth")])]),]);@@ -372,7 +339,6 @@ test("the reserve is only held back when there is a portal to hold it for", () =});test("an element the engine is not painting is skipped", () => {- // Attributes miss the common case: a responsive `hidden lg:flex` is a class.const hidden = {...el("DIV", [text("buried")]),checkVisibility: () => false,@@ -383,8 +349,6 @@ test("an element the engine is not painting is skipped", () => {assert.equal(index.text.includes("shown"), true);});-// --- the search ---------------------------------------------------------------------------------test("matching ignores case in both directions", () => {const index = buildTextIndex(el("DIV", [el("P", [text("Unsloth STUDIO")])]));assert.equal(findMatches(index, "unsloth").length, 1);@@ -412,8 +376,7 @@ test("an empty query matches nothing", () => {});test("a pasted separator cannot match across a block boundary", () => {- // Not typeable, but a paste could carry one, and it would otherwise match the very boundary the- // separator is there to keep closed.+ // A paste could carry one, and it would match the very boundary the separator keeps closed.const index = buildTextIndex(el("DIV", [el("P", [text("a")]), el("P", [text("b")])]),);@@ -422,9 +385,7 @@ test("a pasted separator cannot match across a block boundary", () => {});test("content-visibility skipping is not treated as invisibility", () => {- // A `content-visibility: auto` subtree the reader has not scrolled to is SKIPPED, not hidden,- // and asking `checkVisibility` about that would drop the far half of a Hub README from the- // index. Nothing would put it back: scrolling renders the subtree without mutating the DOM.+ // Such a subtree is SKIPPED, not hidden, and nothing would put it back.const asked: unknown[] = [];const probe = {...el("DIV", [text("readme")]),@@ -446,10 +407,7 @@ test("content-visibility skipping is not treated as invisibility", () => {});test("both spellings of every visibility option are asked for", () => {- // `visibilityProperty`/`opacityProperty` are renames of `checkVisibilityCSS`/`checkOpacity`, an- // engine reads only the name it knows, and Web IDL drops an unknown member silently. The modern- // name alone is a no-op on Chrome 105-120 and Firefox 106-121, which would then index and- // highlight `visibility: hidden` text.+ // An engine reads only the name it knows, and Web IDL drops an unknown member silently.const seen: Record<string, unknown>[] = [];const probe = {...el("DIV", [text("readme")]),@@ -467,7 +425,6 @@ test("both spellings of every visibility option are asked for", () => {]) {assert.equal(modern in options, true, `${modern} is missing`);assert.equal(historic in options, true, `${historic} is missing`);- // The two names mean the same thing, so they must never disagree.assert.equal(options[modern],options[historic],@@ -477,8 +434,7 @@ test("both spellings of every visibility option are asked for", () => {});test("an engine that honours only the historic option names still hides hidden text", () => {- // Simulates Chrome 105-120 / Firefox 106-121: `checkVisibility` exists, but the modern option- // names are unknown to it and therefore ignored.+ // Chrome 105-120 / Firefox 106-121: `checkVisibility` exists, but ignores the modern names.const legacyEngine = (style: { visibility?: string }) => ({checkVisibility: (options?: Record<string, unknown>) =>!(options?.checkVisibilityCSS === true && style.visibility === "hidden"),@@ -492,9 +448,7 @@ test("an engine that honours only the historic option names still hides hidden t});test("an inline SVG is skipped despite reporting a lowercase tag", () => {- // Only HTML elements report their tag uppercased. SVG and MathML keep their source casing, so a- // Mermaid diagram's `<svg>` answers "svg" and walked past a set spelled in HTML casing, putting- // its labels in the index as matches a Range cannot reliably paint.+ // SVG and MathML keep their source casing, so `<svg>` answers "svg".const svg = el("svg", [el("text", [text("mermaid label")])]);const index = buildTextIndex(el("DIV", [svg, el("P", [text("prose")])]));assert.equal(index.text.includes("mermaid"), false);@@ -502,13 +456,11 @@ test("an inline SVG is skipped despite reporting a lowercase tag", () => {});test("a portaled surface is indexed after the scope, behind a boundary", () => {- // A popover renders to the body, outside the scope, and a reader sees it as part of the page.const scope = el("DIV", [el("P", [text("in the thread")])]);const portal = el("DIV", [el("P", [text("in the popover")])]);const index = buildTextIndex(scope, [portal]);assert.equal(index.text, `in the thread${BLOCK_SEPARATOR}in the popover`);assert.equal(findMatches(index, "in the popover").length, 1);- // Separate surfaces, so nothing runs out of one and into the other.assert.deepEqual(findMatches(index, "thread in"), []);});@@ -521,8 +473,6 @@ test("every portaled surface is separated from the one before it", () => {});test("a portaled surface with nothing to contribute leaves no separator behind", () => {- // The boundary is pending until text follows it, so a surface that is skipped, or empty, does- // not leave a gap at the end of the index for a match to be measured against.const scope = el("DIV", [el("P", [text("only")])]);const index = buildTextIndex(scope, [el("DIV", [text("parked")], { inert: "" }),@@ -555,8 +505,6 @@ function withStyles(}}-// --- the observer's own filter ------------------------------------------------------------------/** The two bits of `Element` the filter touches, so a record can be handed over without a DOM. */function skipNode(options: {skipped?: boolean;@@ -602,10 +550,8 @@ function record(}test("the selection fallback hands the caret back to the field", async () => {- // Moving the selection into ordinary text takes the caret with it on WebKit and Blink: the field- // still reports as active but every keystroke is swallowed, so the query freezes at one- // character. Measured with the registry removed, the field held "u" and never grew. This matters- // exactly where the fallback runs: Firefox below 140, or the desktop build's WebKitGTK.+ // The caret goes with the selection on WebKit and Blink: the field still reports as active+ // while every keystroke is swallowed.const engine = await readFile(new URL("../src/features/find-in-page/lib/find-dom.ts", import.meta.url),"utf8",@@ -616,8 +562,7 @@ test("the selection fallback hands the caret back to the field", async () => {const body = fallback.slice(0, fallback.indexOf("\n}"));assert.match(body, /holdCaret\(\)/);assert.match(body, /releaseCaret\(/);- // The caret has to be taken BEFORE the selection moves and given back after, or there is nothing- // left to restore.+ // The caret has to be taken BEFORE the selection moves, or there is nothing left to restore.assert.ok(body.indexOf("holdCaret()") < body.indexOf("selection.addRange"),"the caret must be captured before the selection is moved",@@ -629,11 +574,7 @@ test("the selection fallback hands the caret back to the field", async () => {});test("a workspace generating off-route does not rebuild the index", () => {- // `__root.tsx` keeps Chat, Images, Video and Audio mounted under `hidden` and `inert` precisely- // so a long generation is not cancelled by navigating away, and they sit INSIDE the scope. Every- // character such a reply streams is a mutation the bar used to answer with a full flatten, which- // then correctly excluded that text: the whole rebuild was for nothing, once per throttle for as- // long as the generation ran.+ // The shell keeps off-route workspaces mounted under `hidden` and `inert` INSIDE the scope.for (const mark of ["[inert]", "[hidden]", '[aria-hidden="true"]']) {const parked = skipNode({ mark });const streamed = skipNode({ parent: parked });@@ -643,15 +584,13 @@ test("a workspace generating off-route does not rebuild the index", () => {`a reply streaming under ${mark} still asked for a rebuild`,);}- // And an ordinary reply in the workspace on screen still does.const live = skipNode({ parent: skipNode({}) });assert.equal(mutatesSearchableText(record(live, "characterData")), true);});test("parking a workspace is itself a change, whichever attribute says so", () => {- // The same trap as the skip attribute below: `closest` matches the element it starts at, so the- // record announcing that a workspace just went `inert` would answer "inside skipped content" and- // be dropped, leaving the workspace the reader just left in the count.+ // `closest` matches where it starts, so the record saying a workspace went `inert` answers+ // "inside skipped content".for (const mark of ["[inert]", "[hidden]", '[aria-hidden="true"]']) {const parked = skipNode({ mark, parent: skipNode({}) });assert.equal(@@ -663,9 +602,7 @@ test("parking a workspace is itself a change, whichever attribute says so", () =});test("adding the skip attribute is what reindexes, not only removing it", () => {- // `closest` matches where it starts, so the record announcing that an element became skippable- // answers "inside skipped content" and is thrown away, leaving the region counted and painted- // until some unrelated mutation happens by. Removal always worked, which is what hid this.+ // Same trap: gaining the attribute answers "inside skipped content" and is thrown away.const parent = skipNode({});const marked = skipNode({ skipped: true, parent });assert.equal(@@ -683,8 +620,6 @@ test("adding the skip attribute is what reindexes, not only removing it", () =>});test("ordinary mutations inside skipped content are still ignored", () => {- // The point of the filter: the bar floats inside the region it searches, so its own counter- // re-rendering must not order a re-index of itself.const bar = skipNode({ skipped: true });const inside = skipNode({ parent: bar });assert.equal(mutatesSearchableText(record(inside)), false);@@ -699,7 +634,6 @@ test("a mutation in ordinary content always reindexes", () => {});test("a detached target counts as a change rather than being dropped", () => {- // No parent to ask, so the conservative answer is the safe one.const orphan = skipNode({ skipped: true, parent: null });assert.equal(mutatesSearchableText(record(orphan, "attributes", FIND_SKIP_ATTRIBUTE)),@@ -708,7 +642,6 @@ test("a detached target counts as a change rather than being dropped", () => {});test("an attribute that is not the skip flag is judged from the target itself", () => {- // Chrome stays chrome: `inert` flipping inside a skipped region is still skipped.const bar = skipNode({ skipped: true });const inside = skipNode({ parent: bar });assert.equal(@@ -718,10 +651,6 @@ test("an attribute that is not the skip flag is judged from the target itself",});test("a display:contents wrapper that is itself invisible keeps its own text out", () => {- // `skipsSubtree` lets a boxless wrapper through on purpose, since `checkVisibility` calls- // anything with no box invisible and the shell wraps visible content in one. But `visibility`- // inherits and only ELEMENT children are re-checked, so a direct text child of a hidden- // `contents` wrapper was indexed, counted and painted while nobody could see it.const ghost = el("SPAN", [text("invisible")]);(ghost as { checkVisibility?: () => boolean }).checkVisibility = () => false;withStyles(@@ -735,7 +664,6 @@ test("a display:contents wrapper that is itself invisible keeps its own text out});test("a visible display:contents wrapper is still searched", () => {- // The other half: the rescue has to keep working, or most of what there is to search goes.const wrapper = el("SPAN", [text("findable")]);(wrapper as { checkVisibility?: () => boolean }).checkVisibility = () =>false;@@ -747,8 +675,6 @@ test("a visible display:contents wrapper is still searched", () => {});test("an element child that restores visibility inside a hidden contents wrapper is kept", () => {- // Scoped to the wrapper's OWN text: `visibility: visible` paints again and the walk does not- // turn back, so that child has to survive.const inner = el("SPAN", [text("restored")]);const ghost = el("SPAN", [text("invisible"), inner]);(ghost as { checkVisibility?: () => boolean }).checkVisibility = () => false;@@ -766,9 +692,7 @@ test("an element child that restores visibility inside a hidden contents wrapper});test("the match window anchor is resolved only once the cap bites", () => {- // `viewportOffset` reads layout, and an argument is evaluated whether or not the callee wants- // it, so inline it ran on every keystroke however few matches there were. As a thunk it is paid- // only where it changes the answer: when the cap actually cuts the list short.+ // `viewportOffset` reads layout, and an argument is evaluated whether or not it is wanted.const index = buildTextIndex(el("P", [text("a a a a a a a a")]));let asked = 0;@@ -783,17 +707,12 @@ test("the match window anchor is resolved only once the cap bites", () => {const capped = findMatches(index, "a", 3, anchor);assert.equal(asked, 1, "a capped query resolves the anchor exactly once");- // A thunk and the number it returns must pick the same window.assert.deepEqual(capped, findMatches(index, "a", 3, 6));});test("a decomposed dotted I is found by the ordinary query", () => {- // U+0130 decomposes to `I` + a combining dot, which folds to `i` + that dot. `i` + dot has no- // precomposed form, so NFC cannot put it back and the plain query missed a word on screen while- // the precomposed spelling of the same word matched.+ // U+0130 decomposes to `I` + a combining dot, which folds to `i` + that dot.const decomposed = "\u0049\u0307stanbul";- // The fold is what strands it: `I` + dot lowercases to `i` + dot, and THAT has no precomposed- // form, so no amount of normalizing the query reaches it.assert.equal("\u0069\u0307".normalize("NFC"), "\u0069\u0307");const index = buildTextIndex(el("P", [text(`Welcome to ${decomposed}`)]));for (const query of ["istanbul", "ISTANBUL", "\u0130stanbul"]) {@@ -802,17 +721,13 @@ test("a decomposed dotted I is found by the ordinary query", () => {});test("the dotted variant costs nothing on a document without combining marks", () => {- // It is only offered when the index carries a combining dot, so an ordinary thread keeps the- // single-variant `indexOf` path.const index = buildTextIndex(el("P", [text("indexing is fine here")]));assert.equal(findMatches(index, "indexing").length, 1);assert.equal(findMatches(index, "i").length, 4);});test("a query too large to compile falls back instead of throwing", () => {- // Every engine caps the pattern size it will compile and the spec sets none, so there is no- // length that is right everywhere. Measured on V8: a whitespace-bearing query throws at 15,651- // characters, and the throw came out through the keystroke and took the bar down with it.+ // Measured on V8: a whitespace-bearing query throws at 15,651 characters.const index = buildTextIndex(el("P", [text("a small thread about unsloth")]));const huge = "some log line with spaces ".repeat(4000);assert.ok(huge.length > 15_651, "premise: past the measured V8 ceiling");@@ -823,16 +738,13 @@ test("a query too large to compile falls back instead of throwing", () => {test("a needle longer than the haystack is rejected before any of the work", () => {const index = buildTextIndex(el("P", [text("short")]));assert.deepEqual(findMatches(index, "x".repeat(500)), []);- // Measured against the shortest spelling: a decomposed query is longer than the precomposed- // text it is meant to find, so the raw length is the wrong thing to test.+ // A decomposed query is longer than the precomposed text it is meant to find.const cafe = buildTextIndex(el("P", [text("caf\u00e9")]));assert.equal(findMatches(cafe, "cafe\u0301").length, 1);- // And a needle that still fits is unaffected.assert.equal(findMatches(index, "short").length, 1);});test("a numeric anchor still means what it always did", () => {- // The thunk is additive. Every existing caller passes a number and must be unaffected.const index = buildTextIndex(el("P", [text("b b b b b b b b")]));assert.deepEqual(findMatches(index, "b", 3, 0), findMatches(index, "b", 3));assert.deepEqual(@@ -842,9 +754,7 @@ test("a numeric anchor still means what it always did", () => {});test("a word matches whichever way either side spells it", () => {- // The same word composed and decomposed. macOS hands back decomposed filenames while a model- // writes composed prose, so one thread holds both, and the platform's own find matches either- // from either. Measured: all four pairings hit.+ // macOS hands back decomposed filenames while a model writes composed prose.const composed = "caf\u00e9";const decomposed = "cafe\u0301";assert.notEqual(composed, decomposed);@@ -859,8 +769,6 @@ test("a word matches whichever way either side spells it", () => {1,`text ${escape(written)} and query ${escape(typed)} did not meet`,);- // And the offsets are the document's, not a normalized copy's: the match has to cover- // exactly the characters that were written, whatever length that spelling is.assert.deepEqual(matches[0], { start: 2, end: 2 + written.length });assert.equal(index.text.slice(matches[0].start, matches[0].end), written);}@@ -868,11 +776,8 @@ test("a word matches whichever way either side spells it", () => {});test("an occurrence that mixes the two spellings is still one word", () => {- // Alternating whole spellings of the query only reaches text that is all-composed or- // all-decomposed. Joining two text nodes joins two sources, so one visible word can be neither,- // and then no spelling the query CAN be written in matches it. Every engine's own find reaches- // this: measured `true` on chromium, firefox and webkit for both all-composed and all-decomposed- // queries against a mixed occurrence.+ // Alternating whole spellings reaches only all-composed or all-decomposed text, and joining+ // two text nodes joins two sources. Every engine's own find reaches this.const composed = "é";const decomposed = "é";const mixed = `caf${composed}caf${decomposed}`;@@ -888,12 +793,10 @@ test("an occurrence that mixes the two spellings is still one word", () => {1,`query ${escape(typed)} missed a mixed word`,);- // Still the document's own offsets, so the highlight covers what was written.assert.deepEqual(matches[0], { start: 2, end: 2 + mixed.length });assert.equal(index.text.slice(matches[0].start, matches[0].end), mixed);}- // The shape that produces it here: one word, two inline nodes, one source each.const split = buildTextIndex(el("DIV", [el("P", [text(`caf${composed}`), text(`caf${decomposed}`)])]),);@@ -1027,8 +930,7 @@ test("a half-composed Hangul syllable is found and covered whole", () => {});test("the index itself is left in the form the document wrote", () => {- // Normalizing it is the other way to fix the above, and it would change its length: every offset- // in the index stands for one character of a text node, so a shorter index misplaces them all.+ // Normalizing the index would change its length, and every offset stands for one character.const decomposed = "cafe\u0301";const index = buildTextIndex(el("DIV", [el("P", [text(decomposed)])]));assert.equal(index.text, decomposed);@@ -1036,8 +938,6 @@ test("the index itself is left in the form the document wrote", () => {});test("spelling variants do not loosen whitespace inside a fence", () => {- // The variants share the pattern path with the flexible-whitespace one, and inside a `<pre>` the- // whitespace on screen is the whitespace in the node. A variant is exact; a flexed run is not.const fence = el("PRE", [text("caf\u00e9 au lait")]);withStyles(new Map([[fence, { whiteSpace: "pre" }]]), () => {const index = buildTextIndex(el("DIV", [fence]));@@ -1047,9 +947,8 @@ test("spelling variants do not loosen whitespace inside a fence", () => {});test("an engine with no checkVisibility falls back to the computed properties", () => {- // `checkVisibility` landed in Safari 17.4, and WebKitGTK is already supported here: it is the- // engine `selectRangeFallback` exists for. The optional call answers undefined there, and read as- // "not false" that put every `display: none` subtree in the app back into the index.+ // `checkVisibility` is undefined on WebKitGTK, and read as "not false" it indexed every+ // `display: none` subtree.for (const style of [{ display: "none" },{ visibility: "hidden" },@@ -1070,9 +969,6 @@ test("an engine with no checkVisibility falls back to the computed properties",});test("with no checkVisibility, a hidden boxless wrapper is still descended into", () => {- // Where the two mechanisms meet. `display: contents` is boxless rather than hidden, so the- // fallback must not skip it whole; `hidesOwnText` drops the text it holds directly, and a child- // that turns visibility back on is painted and still has to be found.const shown = el("SPAN", [text("turned back on")]);const wrapper = el("DIV", [text("the wrapper's own text"), shown]);withStyles(@@ -1089,8 +985,6 @@ test("with no checkVisibility, a hidden boxless wrapper is still descended into"});test("the fallback does not mistake a boxless wrapper for a hidden one", () => {- // `display: contents` is the case the whole visibility branch was written around: it has no box- // and is not hidden, and the shell hands a grid its children through one.const wrapper = el("DIV", [el("P", [text("inside a wrapper")])]);withStyles(new Map([[wrapper, { display: "contents" }]]), () => {assert.equal(buildTextIndex(el("DIV", [wrapper])).text, "inside a wrapper");@@ -1098,9 +992,7 @@ test("the fallback does not mistake a boxless wrapper for a hidden one", () => {});test("two spans the CSS renders as blocks do not run together", () => {- // No tag name says these are blocks, and Tailwind stacks them all over the app: a source's title- // over its URL in the research panel is exactly this shape. Run together they invent a word that- // is on screen nowhere, under one highlight spanning two rows.+ // No tag name says these are blocks, and run together they invent a word on screen nowhere.const first = el("SPAN", [text("Open")]);const second = el("SPAN", [text("AI models")]);withStyles(@@ -1112,12 +1004,10 @@ test("two spans the CSS renders as blocks do not run together", () => {const index = buildTextIndex(el("DIV", [first, second]));assert.equal(index.text.includes("openai"), false);assert.deepEqual(findMatches(index, "openai"), []);- // Each row still matches on its own.assert.equal(findMatches(index, "open").length, 1);assert.equal(findMatches(index, "ai models").length, 1);},);- // An inline span is not a boundary, so markup inside a sentence still reads as one word.const inline = el("SPAN", [text("slo")]);withStyles(new Map([[inline, { display: "inline" }]]), () => {const index = buildTextIndex(el("P", [text("un"), inline, text("th")]));@@ -1126,10 +1016,6 @@ test("two spans the CSS renders as blocks do not run together", () => {});test("whitespace is only flexible where the page collapses it", () => {- // The flexible run exists for prose, where a markdown soft wrap puts a newline in the node that- // renders as a space. In a code fence the whitespace on screen IS the whitespace in the node, so- // a query typed with one space must not land on three. The platform's own find draws the same- // line: matching across a wrap in a paragraph, and not inside a `<pre>`.const fence = el("PRE", [text("unsloth fast")]);const prose = el("P", [text("unsloth\n fast")]);withStyles(@@ -1142,9 +1028,7 @@ test("whitespace is only flexible where the page collapses it", () => {const wrapped = buildTextIndex(el("DIV", [prose]));assert.deepEqual(findMatches(fenced, "unsloth fast"), []);assert.equal(findMatches(fenced, "unsloth fast").length, 1);- // Prose is unchanged: one space still crosses the wrap.assert.equal(findMatches(wrapped, "unsloth fast").length, 1);- // And the flag rides on the segment, not the element, so it survives the offset map.assert.equal(fenced.segments[0].preserved, true);assert.equal(wrapped.segments[0].preserved, false);},@@ -1162,9 +1046,6 @@ test("preserved whitespace is inherited by the nodes inside it", () => {});test("a boxless wrapper is walked through, not skipped", () => {- // `display: contents` generates no box, and no box is the first thing `checkVisibility` calls- // invisible. The shell (sidebar.tsx) and the training page (studio-page.tsx) each wrap their- // content in one, so reading that answer as hidden empties the index for most of the app.const wrapper = {...el("DIV", [el("P", [text("training")])]),checkVisibility: () => false,@@ -1185,7 +1066,6 @@ test("a boxless wrapper is walked through, not skipped", () => {try {const index = buildTextIndex(el("DIV", [wrapper, collapsed]));assert.equal(index.text.includes("training"), true);- // A wrapper with a box that says invisible is still hidden.assert.equal(index.text.includes("offscreen"), false);} finally {view.getComputedStyle = saved;@@ -1193,13 +1073,10 @@ test("a boxless wrapper is walked through, not skipped", () => {});test("a query spanning whitespace matches the phrase as it renders", () => {- // HTML collapses runs of whitespace, so a markdown paragraph soft-wrapped mid-sentence renders- // as one line while its text node still holds the newline.const index = buildTextIndex(el("DIV", [el("P", [text("A soft wrapped\n phrase about unsloth.")])]),);assert.equal(findMatches(index, "wrapped phrase").length, 1);- // The match is as wide as the run it covered, so the highlight lands on the whole phrase.const [match] = findMatches(index, "wrapped phrase");assert.equal(match.end - match.start, "wrapped\n phrase".length);});@@ -1218,23 +1095,19 @@ test("a regex metacharacter in a query is a literal", () => {assert.deepEqual(findMatches(index, "axb and"), []);});-// --- the bounds ---------------------------------------------------------------------------------test("a document past the ceiling is flattened as far as it goes and says so", () => {const chunk = "x".repeat(100_000);const paragraphs = Array.from({ length: 60 }, () => el("P", [text(chunk)]));const index = buildTextIndex(el("DIV", paragraphs));assert.equal(index.truncated, true);assert.ok(index.text.length <= MAX_INDEX_CHARS);- // What was read is still usable, which is the point of stopping rather than giving up.assert.ok(index.segments.length > 0);assert.ok(findMatches(index, "xxx").length > 0);});test("a clipped node does not run into the next one", () => {- // What the clip threw away is still in the document. Without a boundary the retained prefix and- // the next node touch, a query across the seam matches, and the Range it maps back to spans every- // discarded character in between: measured at 8503 characters of unrelated highlight.+ // Without a boundary a match across the seam maps back to a Range spanning every discarded+ // character.const clipped = text(`${"x".repeat(MAX_NODE_CHARS)}${"discarded ".repeat(500)}`,);@@ -1242,16 +1115,13 @@ test("a clipped node does not run into the next one", () => {const index = buildTextIndex(el("DIV", [el("P", [clipped, next])]));assert.equal(index.truncated, true);assert.deepEqual(findMatches(index, "xy"), []);- // The separator is what closes it, and both sides are still findable on their own.assert.equal(index.text.includes(BLOCK_SEPARATOR), true);assert.equal(findMatches(index, "yz").length, 1);});test("the ceiling holds across a block boundary", () => {- // A node landing exactly on the ceiling used to let the next block's separator push `length`- // past it, and the negative `room` that followed made `slice(0, room)` take all but the last- // character of the following node: a 500,000 character overshoot on a 4,000,000 cap.- // Filled a node at a time now that no single one may take the lot, landing exactly on the cap.+ // A node landing exactly on the ceiling let the next separator push `length` past it, and the+ // negative `room` made `slice(0, room)` take all but the last character.const blocks = Array.from({ length: MAX_INDEX_CHARS / MAX_NODE_CHARS }, () =>el("P", [text("x".repeat(MAX_NODE_CHARS))]),);@@ -1264,8 +1134,6 @@ test("the ceiling holds across a block boundary", () => {});test("nothing lands past the ceiling however the walk arrives at it", () => {- // Every shape that reaches the cap: one oversized node, many small ones, and a boundary in the- // middle. None may report an index longer than the cap it was given.const shapes = [[el("P", [text("x".repeat(MAX_INDEX_CHARS + 1_000))])],Array.from({ length: 9 }, () => el("P", [text("x".repeat(500_000))])),@@ -1289,8 +1157,6 @@ test("a document inside the ceiling is not marked truncated", () => {assert.equal(index.truncated, false);});-// --- the probe over the cap ---------------------------------------------------------------------/** One node holding `count` occurrences of "x", each at its own offset. */function documentOfMatches(count: number): FindElementLike {return el("DIV", [text("x-".repeat(count))]);@@ -1312,8 +1178,6 @@ function walkAsTheHookDoes(}test("the last match in the document is reachable from the bottom of it", () => {- // The probe asked for over the cap used to come off the tail unconditionally. Once the reader is- // far enough down the window IS the tail, so that threw away the occurrence beside them.const index = buildTextIndex(documentOfMatches(MAX_MATCHES + 1_000));const all = findMatches(index, "x", Number.POSITIVE_INFINITY, 0);const last = all[all.length - 1].start;@@ -1380,8 +1244,6 @@ test("the trim takes the far end, and the tail when there is no anchor to judge);});-// --- the paint window ---------------------------------------------------------------------------test("every match is painted while there are few enough of them", () => {assert.deepEqual(paintWindow(12, 4, 400), { from: 0, to: 12 });});@@ -1406,8 +1268,6 @@ function cssRule(css: string, selector: string): string {return css.slice(at, css.indexOf("}", at));}-// --- the wiring ---------------------------------------------------------------------------------test("the stylesheet paints the two highlights the code registers", async () => {const css = await readFile(new URL("../src/index.css", import.meta.url),@@ -1544,8 +1404,6 @@ test("the bar keeps itself out of the region it searches", async () => {"utf8",);assert.match(bar, new RegExp(`${FIND_SKIP_ATTRIBUTE}=`));- // And the mutation filter reads the same attribute, so the counter re-rendering does not order a- // re-index of the conversation.const engine = await readFile(new URL("../src/features/find-in-page/hooks/use-find-in-page.ts",@@ -1564,15 +1422,11 @@ test("light mode is the chatbox's background, under a slightly heavier shadow",const composer = cssRule(css, ".unsloth-composer-surface");const bar = cssRule(css, ".find-bar-surface");- // The composer is the reference, not a copied colour: if it restyles, this fails rather than- // leaving the one floating panel a shade off from the one below it.+ // The composer is the reference, not a copied colour, so a restyle fails here.const background = /background-color:\s*(#[0-9a-f]{6});/i.exec(composer);assert.ok(background);assert.match(bar, new RegExp(`background-color:\\s*${background[1]};`, "i"));- // The shadow is the composer's, spread wider and softened: that one sits at the bottom of the- // page with nothing under it, this one floats over content and needs more to sit on, not more- // weight.const shape = (rule: string) => {const hit =/box-shadow:\s*0 (\d+)px (\d+)px (-?\d+)px rgba\(0, 0, 0, ([\d.]+)\);/.exec(@@ -1596,12 +1450,10 @@ test("light mode is the chatbox's background, under a slightly heavier shadow",to.spread > from.spread,`spread ${to.spread} is not wider than ${from.spread}`,);- // Wider, never heavier: the width is what lifts it off the page, not the ink.assert.ok(to.alpha < from.alpha,`alpha ${to.alpha} is not softer than ${from.alpha}`,);- // But still a shadow, and still in the composer's family.assert.ok(to.alpha >= from.alpha * 0.7,`alpha ${to.alpha} has faded to nothing`,@@ -1626,14 +1478,12 @@ test("dark mode sits above the cards it floats over", async () => {return hit[1].trim();};const grey = (hex: string) => Number.parseInt(hex.slice(1, 3), 16);- // The thread's message cards are `--card`. A bar at that value dissolves into whatever scrolls- // under it, so it sits above them, and below `--border`, past which it reads as an edge.+ // A bar at `--card` dissolves into what scrolls under it; past `--border` it reads as an edge.const bar = grey(value(".dark .find-bar-surface", "background-color"));const card = grey(value(".dark", "--card"));const border = grey(value(".dark", "--border"));assert.ok(bar > card, `bar ${bar} is not lighter than --card ${card}`);assert.ok(bar < border, `bar ${bar} is not darker than --border ${border}`);- // And a halo in the page background, not a dark edge around a borderless panel.assert.match(value(".dark .find-bar-surface", "box-shadow"),/var\(--background\)/,@@ -1654,13 +1504,11 @@ test("the bar stays out of a backgrounded scope, and off the document origin", abar,/isSurfaceBackgrounded\(`\[\$\{FIND_SCOPE_ATTRIBUTE\}\]`\)/,);- // Fixed, not absolute: on a route whose outer container scrolls, an absolutely positioned bar- // sits at the top of a scope taller than the window and scrolls out of reach.+ // Fixed, not absolute: on a route whose outer container scrolls, an absolute bar scrolls away.const surface = /className="(find-bar-surface[^"]*)"/.exec(bar);assert.ok(surface);assert.match(surface[1], /\bfixed\b/);assert.equal(/\babsolute\b/.test(surface[1]), false);- // And capped, so a narrow window cannot push it off the left edge.assert.match(surface[1], /max-w-\[calc\(100vw-2rem\)\]/);// The counter grows from `1/2` to `1234/5000+` in a long chat. The pill must keep its nominal@@ -1675,33 +1523,26 @@ test("the bar stays out of a backgrounded scope, and off the document origin", a});test("the reveal looks again while the scroll is still moving", async () => {- // A `content-visibility: auto` subtree contributes its placeholder height to scrollHeight until- // it renders, so the first scroll is clamped short and reaching toward the block is what makes it- // render. Measured in a real viewport: 3415px short on all three engines without this. The node- // suite cannot see a scroll, so what is pinned here is the shape the browser harness relies on.+ // Such a subtree contributes placeholder height until it renders, clamping the first scroll+ // 3415px short on all three engines. The node suite cannot see a scroll.const dom = await readFile(new URL("../src/features/find-in-page/lib/find-dom.ts", import.meta.url),"utf8",);- // The scroll reports whether it moved anything, which is the whole signal.assert.match(dom,/export function scrollRangeIntoView\(range: Range\): boolean/,);const reveal = dom.slice(dom.indexOf("function revealPass("));const body = reveal.slice(0, reveal.indexOf("\n}\n"));- // Stops as soon as a pass moves nothing, and is bounded so nothing can spin.assert.match(body,/if \(!scrollRangeIntoView\(range\) \|\| tries <= 1\) return;/,);assert.match(body, /tries - 1/);assert.match(dom, /revealRangeWhenPainted\(range: Range, tries = \d\)/);- // Next frame, not a timer: what is being waited for is a paint.assert.match(body, /requestAnimationFrame\(/);- // And a range whose nodes a streaming reply has replaced is dropped rather than scrolled to.assert.match(body, /range\.startContainer\.isConnected/);- // The engine asks for the retrying one, or the second look never happens.const engine = await readFile(new URL("../src/features/find-in-page/hooks/use-find-in-page.ts",@@ -1714,14 +1555,12 @@ test("the reveal looks again while the scroll is still moving", async () => {});test("a dismissed or superseded search abandons its queued reveal passes", async () => {- // The reveal chain walks up to seven more frames after the first scroll. The workspace stays- // mounted when the bar closes, so `isConnected` stays true and the old chain would keep scrolling- // the reader toward a match they already dismissed. A generation token retires it.+ // The workspace stays mounted when the bar closes, so `isConnected` stays true and the old+ // chain keeps scrolling to a dismissed match.const dom = await readFile(new URL("../src/features/find-in-page/lib/find-dom.ts", import.meta.url),"utf8",);- // Every new reveal retires the previous one, so two navigations cannot scroll against each other.const entry = dom.slice(dom.indexOf("export function revealRangeWhenPainted"),);@@ -1729,13 +1568,11 @@ test("a dismissed or superseded search abandons its queued reveal passes", asyncentry.slice(0, entry.indexOf("\n}\n")),/cancelRevealPasses\(\)/,);- // And the queued frame checks the token it was queued under before scrolling again.const pass = dom.slice(dom.indexOf("function revealPass("));assert.match(pass.slice(0, pass.indexOf("\n}\n")),/if \(generation !== revealGeneration\) return;/,);- // Teardown retires the chain: closing the bar is exactly when the reader stops asking to move.const engine = await readFile(new URL("../src/features/find-in-page/hooks/use-find-in-page.ts",@@ -1747,16 +1584,12 @@ test("a dismissed or superseded search abandons its queued reveal passes", async});test("a query too large to compile falls back instead of throwing on the first scan", () => {- // V8 compiles a regex lazily, so an oversized pattern is accepted by the constructor and throws- // `SyntaxError` on the first `exec` instead. Guarding only the constructor left the throw coming- // out through the keystroke that caused it, which tears the bar out of the DOM. Whitespace is- // what gets a query there: every run becomes `\\s+`, so a spaced paste doubles on the way in.+ // V8 compiles lazily, so guarding the constructor alone left the throw coming out through the+ // keystroke. Whitespace is what gets a query there: every run becomes `\\s+`.const index = buildTextIndex(el("DIV", [el("P", [text("b".repeat(60000))])]));- // Longer than the compiler will take once the whitespace flexes, shorter than the haystack, so- // the length guard cannot short-circuit it before the pattern is built.+ // Shorter than the haystack, so the length guard cannot short-circuit it.const query = "a ".repeat(10000).trim();assert.ok(query.length < index.text.length);- // The premise: this pattern really does survive construction and die on use.const escaped = query.replace(/\s+/g, "\\s+");let lazy = false;try {@@ -1769,23 +1602,856 @@ test("a query too large to compile falls back instead of throwing on the first strue,"premise: the pattern throws at compile-on-first-use",);- // No throw, and no matches: the literal scan is exact, so a spaced query simply finds nothing.assert.deepEqual(findMatches(index, query, 10), []);});+test("a Hangul query finds the syllables it is looking at", () => {+ // NFD takes a syllable apart into Jamo, no combining marks, so the general rule made three+ // clusters that each composed back to themselves.+ const index = buildTextIndex(+ el("DIV", [el("P", [text("\uac00\ub098\ub2e4 hello \ud55c\uad6d\uc5b4")])]),+ );+ assert.deepEqual(findMatches(index, "\uac00", 10), [{ start: 0, end: 1 }]);+ assert.deepEqual(findMatches(index, "\uac00\ub098\ub2e4", 10), [+ { start: 0, end: 3 },+ ]);+ assert.deepEqual(findMatches(index, "\ud55c\uad6d\uc5b4", 10), [+ { start: 10, end: 13 },+ ]);+ assert.deepEqual(+ findMatches(index, "\uac00\ub098\ub2e4".normalize("NFD"), 10),+ [{ start: 0, end: 3 }],+ );+ const decomposed = buildTextIndex(+ el("DIV", [el("P", [text("\uac00\ub098\ub2e4".normalize("NFD"))])]),+ );+ assert.equal(findMatches(decomposed, "\uac00\ub098\ub2e4", 10).length, 1);+});++test("a Hangul syllable is matched whole, in every spelling it can be stored in", () => {+ // Three canonical spellings, not two. Besides fully composed and fully decomposed there is the+ // half-composed one, an LV syllable followed by a loose trailing Jamo, which is exactly what+ // joining two text nodes produces when the syllable straddles them.+ const ga = "\uac00"; // 가+ const gag = "\uac01"; // 각, the same syllable closed by a trailing consonant+ const gagNfd = "\u1100\u1161\u11a8";+ const gagHalf = "\uac00\u11a8";+ const index = (body: string) =>+ buildTextIndex(el("DIV", [el("P", [text(body)])]));++ // Every spelling of the text is reachable from every spelling of the query.+ for (const body of [gag, gagNfd, gagHalf]) {+ for (const query of [gag, gagNfd, gagHalf]) {+ const hits = findMatches(index(body), query, 10);+ assert.equal(+ hits.length,+ 1,+ `${escape(body)} searched for ${escape(query)}`,+ );+ // And the highlight covers the whole syllable as that text spells it, never part of it.+ assert.deepEqual(hits[0], { start: 0, end: body.length });+ }+ }++ // An open syllable must not stop short of the trailing Jamo that closes the one on screen.+ // Decomposed, 가 would otherwise match the first two thirds of 각 and highlight part of a letter,+ // while composed text never could, since there the whole syllable is one code point.+ for (const body of [gag, gagNfd, gagHalf]) {+ assert.deepEqual(findMatches(index(body), ga, 10), [], escape(body));+ }+ // The open syllable is still found where it really is open.+ assert.deepEqual(findMatches(index(ga), ga, 10), [{ start: 0, end: 1 }]);+});++test("Hangul clusters keep every Jamo, and only a real syllable is fenced", () => {+ const index = (body: string) =>+ buildTextIndex(el("DIV", [el("P", [text(body)])]));++ // A grapheme can carry more than one trailing Jamo. The half-composed spelling has to keep the+ // whole suffix: dropping all but the first both shortened the match and let it land on text that+ // lacks the query's final Jamo.+ const twoTrailing = "\uac00\u11a8\u11a8";+ assert.deepEqual(findMatches(index(twoTrailing), twoTrailing, 10), [+ { start: 0, end: 3 },+ ]);+ assert.deepEqual(+ findMatches(index("\uac01\u11a8"), "\u1100\u1161\u11a8\u11a8", 10),+ [{ start: 0, end: 2 }],+ "the same grapheme, spelt half-composed in the text",+ );+ // ... but never on text that is missing the last Jamo the query asked for.+ assert.deepEqual(+ findMatches(index("\uac00\u11a8"), "\uac00\u11a8\u11a8", 10),+ [],+ );++ // A bare leading Jamo is its own grapheme, not a syllable waiting to be closed, so a trailing+ // Jamo after one belongs to something else and must not be fenced off.+ assert.deepEqual(+ findMatches(index("\uac00\u1100\u11a8"), "\uac00\u1100", 10),+ [{ start: 0, end: 2 }],+ );+});++test("a Hangul match starts and stops on grapheme boundaries", () => {+ const index = (body: string) =>+ buildTextIndex(el("DIV", [el("P", [text(body)])]));+ const gag = "\uac01"; // 각+ const ga = "\uac00"; // 가+ const lead = "\u1100"; // a bare leading Jamo++ // Not stopping inside one. A query carrying its own trailing Jamo could still match the prefix+ // of a grapheme that carries two, so the fence belongs after closed syllables as well as open.+ for (const body of [`${gag}\u11a8`, `${lead}\u1161\u11a8\u11a8`]) {+ assert.deepEqual(findMatches(index(body), gag, 10), [], escape(body));+ }+ // Nor starting inside one: a grapheme can carry more than one leading Jamo, and a match that+ // begins at the second highlights only its tail.+ for (const body of [`${lead}${ga}`, `${lead}${lead}\u1161`]) {+ assert.deepEqual(findMatches(index(body), ga, 10), [], escape(body));+ }+ // The fence is on the first cluster only, so a query that itself opens with a bare leading Jamo+ // still finds exactly that text.+ assert.deepEqual(findMatches(index(`${lead}${ga}`), `${lead}${ga}`, 10), [+ { start: 0, end: 2 },+ ]);+ // And an ordinary syllable is still found where it really stands alone.+ assert.deepEqual(findMatches(index(`${ga} hello`), ga, 10), [+ { start: 0, end: 1 },+ ]);+});++test("an engine without lookbehind falls back rather than throwing", () => {+ // The grapheme fence uses lookbehind, which JavaScriptCore only shipped in Safari 16.4. The+ // pattern is built from a string, so an older engine throws at construction, where `matchPattern`+ // already catches it and hands the search to the literal scan.+ const real = globalThis.RegExp;+ const refuseLookbehind = (source: string, flags?: string) => {+ if (typeof source === "string" && source.includes("(?<")) {+ throw new SyntaxError("Invalid regular expression");+ }+ return new real(source, flags);+ };+ refuseLookbehind.prototype = real.prototype;+ globalThis.RegExp = refuseLookbehind as unknown as RegExpConstructor;+ try {+ const index = buildTextIndex(+ el("DIV", [el("P", [text("\uac00\ub098\ub2e4 hello")])]),+ );+ // Exact queries still work; what is lost is only the flexing the pattern would have added.+ assert.deepEqual(findMatches(index, "\uac00\ub098\ub2e4", 10), [+ { start: 0, end: 3 },+ ]);+ assert.deepEqual(findMatches(index, "hello", 10), [{ start: 4, end: 9 }]);+ } finally {+ globalThis.RegExp = real;+ }+});++test("no Hangul match ever begins or ends inside a grapheme", () => {+ // Five rounds of review found five ways to stop or start half way through a Hangul syllable,+ // each a range that had not been thought of. So this asserts the property rather than the cases,+ // against `Intl.Segmenter` as the authority on where a grapheme ends.+ const segmenter = new Intl.Segmenter("ko", { granularity: "grapheme" });+ const boundaries = (body: string) => {+ const edges = new Set([0]);+ let at = 0;+ for (const { segment } of segmenter.segment(body)) {+ at += segment.length;+ edges.add(at);+ }+ return edges;+ };++ // Leading, vowel and trailing Jamo from the main block and from Extended-A and Extended-B.+ const leads = ["\u1100", "\u1101", "\ua960"];+ const vowels = ["\u1161", "\u1162", "\ud7b0"];+ const trails = ["\u11a8", "\u11a9", "\ud7cb"];+ const corpus = new Set([+ "hello \uac00",+ "\uac00 hello",+ "\uac00\ub098\ub2e4",+ "caf\u00e9",+ "cafe\u0301",+ "\uac00\u0301", // a syllable and a combining mark are one grapheme+ "\uac00\u200d\ub098", // ... and so is a joiner between two+ "\u0600\uac00", // a Prepend joins whatever follows it+ "\u1100\uac00\ub098", // two leading Jamo, then a second syllable+ ]);+ for (const lead of leads) {+ corpus.add(lead);+ for (const vowel of vowels) {+ const open = lead + vowel;+ corpus.add(open);+ corpus.add(open.normalize("NFC"));+ for (const other of vowels) corpus.add(open + other);+ for (const other of leads) {+ corpus.add(lead + other + vowel);+ corpus.add(lead + other + vowel + "\ub098");+ }+ corpus.add(open.normalize("NFC") + "\u0301");+ corpus.add("\u0600" + open.normalize("NFC"));+ for (const trail of trails) {+ const closed = open + trail;+ corpus.add(closed);+ corpus.add(closed.normalize("NFC"));+ corpus.add(open.normalize("NFC") + trail);+ for (const other of trails) corpus.add(closed + other);+ }+ }+ }++ let checked = 0;+ for (const body of corpus) {+ const index = buildTextIndex(el("DIV", [el("P", [text(body)])]));+ const edges = boundaries(index.text);+ for (const query of corpus) {+ for (const hit of findMatches(index, query, 50)) {+ checked += 1;+ assert.ok(+ edges.has(hit.start) && edges.has(hit.end),+ `${escape(body)} searched for ${escape(query)} gave ${hit.start}..${hit.end}`,+ );+ }+ }+ // And every string still finds itself, which is what a fence is easiest to break.+ assert.ok(+ findMatches(index, body, 10).length >= 1,+ `${escape(body)} cannot find itself`,+ );+ // A fence that is too eager is the other failure, and it does not show up as a bad range: the+ // match simply goes missing. Every leading run of whole graphemes must still be findable.+ let prefix = "";+ for (const { segment } of segmenter.segment(index.text)) {+ prefix += segment;+ if (prefix === index.text) break;+ assert.ok(+ findMatches(index, prefix, 10).length >= 1,+ `${escape(body)} cannot find its own prefix ${escape(prefix)}`,+ );+ }+ }+ assert.ok(checked > 200, `only ${checked} matches exercised`);+});++test("the grapheme fences do not depend on lookbehind", async () => {+ // JavaScriptCore only shipped lookbehind in Safari 16.4, and a pattern using one throws on older+ // engines straight into the unfenced literal scan, quietly undoing both boundaries. So the start+ // of the fence is checked in code and the pattern carries none.+ const source = await readFile(+ new URL(+ "../src/features/find-in-page/lib/find-text-index.ts",+ import.meta.url,+ ),+ "utf8",+ );+ assert.equal(source.includes("(?<"), false, "no lookbehind in the pattern");++ const real = globalThis.RegExp;+ const refuse = (pattern: string, flags?: string) => {+ if (typeof pattern === "string" && pattern.includes("(?<")) {+ throw new SyntaxError("Invalid regular expression");+ }+ return new real(pattern, flags);+ };+ refuse.prototype = real.prototype;+ globalThis.RegExp = refuse as unknown as RegExpConstructor;+ try {+ const closed = buildTextIndex(el("DIV", [el("P", [text("\uac01\u11a8")])]));+ assert.deepEqual(findMatches(closed, "\uac01", 10), []);+ const led = buildTextIndex(el("DIV", [el("P", [text("\u1100\uac00")])]));+ assert.deepEqual(findMatches(led, "\uac00", 10), []);+ const plain = buildTextIndex(+ el("DIV", [el("P", [text("\uac00\ub098\ub2e4")])]),+ );+ assert.deepEqual(findMatches(plain, "\uac00", 10), [{ start: 0, end: 1 }]);+ } finally {+ globalThis.RegExp = real;+ }+});++test("the grapheme boundary is the platform's answer, not a list of ranges", () => {+ // Enumerating the ranges by hand kept missing one: Hangul Jamo, combining marks, Prepend,+ // spacing marks, skin tones. The question goes to `Intl.Segmenter` instead, which knows the whole+ // of UAX 29. These are the cases that were wrong before it did.+ const index = (body: string) =>+ buildTextIndex(el("DIV", [el("P", [text(body)])]));+ for (const [body, query] of [+ ["\uac00\u093e", "\uac00"], // a spacing mark+ ["\uac00\u{1f3fb}", "\uac00"], // an astral modifier+ ["\u{1193f}\uac00", "\uac00"], // a supplementary Prepend+ ["\uac00\u0301", "\uac00"], // a combining mark+ ["\u0600\uac00", "\uac00"], // a BMP Prepend+ ["\uac01\u11a8", "\uac01"], // one trailing Jamo too few+ ["\u1100\uac00", "\uac00"], // starting after a leading Jamo+ ] as const) {+ assert.deepEqual(findMatches(index(body), query, 10), [], escape(body));+ }+ // Whitespace ends a grapheme, so a query that ends in a space is not held to what follows it.+ assert.deepEqual(findMatches(index("\uac00 \u11a8"), "\uac00 ", 10), [+ { start: 0, end: 2 },+ ]);+ // An emoji sequence is one grapheme too, and this was never Hangul-specific.+ assert.deepEqual(+ findMatches(+ index("\u{1f469}\u200d\u{1f469}\u200d\u{1f466}"),+ "\u{1f469}",+ 10,+ ),+ [],+ );+});++test("an engine whose `containing` is off by one is not trusted with it", () => {+ // WebKit reads 0, 0, 1 over `x` and a thumbs up where Chromium and Firefox read 0, 1, 1, and+ // the seek budget hides it: past the budget the table comes from the iterator instead.+ const body = `x${"\u{1f44d}"} and caf\u00e9`;+ const withReal = buildTextIndex(el("DIV", [el("P", [text(body)])]));+ const correct = findMatches(withReal, "x", 10);+ assert.deepEqual(correct, [{ start: 0, end: 1 }]);++ // A fresh process, since the segmenter and the probe are both held for the life of the module.+ {+ const probe = spawnSync(+ process.execPath,+ [+ "--experimental-strip-types",+ "--no-warnings",+ "--input-type=module",+ "-e",+ `+ const real = Intl.Segmenter;+ Intl.Segmenter = class {+ constructor(...args) { this.inner = new real(...args); }+ segment(input) {+ const segments = this.inner.segment(input);+ const starts = [...segments].map(({ index }) => index);+ return {+ containing: (at) => {+ if (at >= input.length) return undefined;+ const previous = starts.filter((start) => start <= at);+ const own = previous[previous.length - 1];+ const earlier = previous[previous.length - 2];+ return { index: own === at && earlier !== undefined ? earlier : own };+ },+ [Symbol.iterator]: () => segments[Symbol.iterator](),+ };+ }+ };+ const { buildTextIndex, findMatches } = await import(${JSON.stringify(+ new URL(+ "../src/features/find-in-page/lib/find-text-index.ts",+ import.meta.url,+ ).href,+ )});+ const el = (tagName, childNodes) => ({+ nodeType: 1, tagName, childNodes, getAttribute: () => null,+ });+ const text = (data) => ({ nodeType: 3, data });+ const index = buildTextIndex(el("DIV", [el("P", [text(${JSON.stringify(+ body,+ )})])]));+ console.log(JSON.stringify(findMatches(index, "x", 10)));+ `,+ ],+ { encoding: "utf8" },+ );+ assert.equal(probe.status, 0, probe.stderr);+ assert.deepEqual(+ JSON.parse(probe.stdout.trim()),+ correct,+ "the same answer, by the table rather than by seeking",+ );+ }+});++test("the seek probe is asked once, on a fixture the spec settles", () => {+ // Once per process: a probe on the hot path is the cost the budget exists to avoid.+ const source = readFileSync(+ new URL(+ "../src/features/find-in-page/lib/find-text-index.ts",+ import.meta.url,+ ),+ "utf8",+ );+ assert.match(source, /let seeksBoundaries: boolean \| undefined;/);+ assert.match(+ source,+ /if \(seeksBoundaries !== undefined\) return seeksBoundaries;/,+ );+ // `containing(1)` over a code unit and a surrogate pair is the start of the pair, per the spec.+ assert.match(source, /probe\.containing\(1\)\?\.index === 1/);+ // The definition, the seek in `startsGrapheme`, and the junction check a cut asks.+ assert.equal((source.match(/segmenterSeeksBoundaries\(/g) ?? []).length, 3);+});++test("a query that needs no pattern is not given one", () => {+ // Forcing Hangul through the regex path so the fences could apply meant a large paste built a+ // pattern that V8 accepted and then refused to run, and the throw escaped the search entirely.+ // With the boundary asked of the segmenter instead, no query needs a pattern it did not earn.+ const body = "\u11a8".repeat(50_000);+ const index = buildTextIndex(el("DIV", [el("P", [text(body)])]));+ assert.deepEqual(findMatches(index, body, 10), [{ start: 0, end: 50_000 }]);+});++test("plain text does not pay for the boundary check", () => {+ // The segmenter is asked only where something could actually join, which nothing below U+0300+ // can. Latin prose therefore costs one comparison per match rather than a segmentation.+ const source = readFileSync(+ new URL(+ "../src/features/find-in-page/lib/find-text-index.ts",+ import.meta.url,+ ),+ "utf8",+ );+ assert.match(source, /const JOINS_GRAPHEME = \/\[\^\\u0000-\\u02ff\]\//);+ const guard = source.slice(source.indexOf("function alignsToGraphemes"));+ const before = guard.indexOf("JOINS_GRAPHEME");+ const asks = guard.indexOf("graphemeSegmenter()");+ assert.ok(before > 0 && before < asks, "the cheap test comes first");+});++test("the cheap boundary test looks at both sides of each edge", () => {+ // The shortcut asked what sat outside the match and not what sat at its edges, so a query that+ // itself ends in a character joining forwards slipped through: a Prepend at the start of the+ // text has nothing before it and an ordinary letter after it, and both outside looks passed.+ const index = (body: string) =>+ buildTextIndex(el("DIV", [el("P", [text(body)])]));+ assert.deepEqual(findMatches(index("\u0600a"), "\u0600", 10), []);+ // The same from the other side, where the match begins with a mark that joins backwards.+ assert.deepEqual(findMatches(index("a\u0301"), "\u0301", 10), []);+});++test("text with no whitespace for hundreds of characters is still fenced", () => {+ // The window is anchored at the nearest whitespace or block separator, and where there is none+ // in reach the match used to be waved through. A log line, a URL or a long identifier has none+ // for hundreds of characters, which put the original hole straight back.+ const index = (body: string) =>+ buildTextIndex(el("DIV", [el("P", [text(body)])]));+ const run = "a".repeat(257);+ assert.deepEqual(findMatches(index(`${run}\uac01\u11a8`), "\uac01", 10), []);+ // ... while a match that really is whole is still found out there.+ assert.deepEqual(findMatches(index(`${run}\uac01 x`), "\uac01", 10), [+ { start: 257, end: 258 },+ ]);+});++test("whitespace is not treated as a grapheme boundary", () => {+ // A combining mark joins a preceding space and a Prepend joins a following one, so anchoring at+ // a space cut off the context that decides the join. Only the block separator can be picked up+ // from, being a control, which UAX 29 breaks on either side unconditionally.+ const index = (body: string) =>+ buildTextIndex(el("DIV", [el("P", [text(body)])]));+ assert.deepEqual(findMatches(index("a \u0301"), "\u0301", 10), []);+ assert.deepEqual(findMatches(index("\u0600 "), "\u0600", 10), []);+});++test("a long run of regional indicators keeps its parity", () => {+ // Flags pair off by the parity of the run they sit in, and a run has no length limit, so no+ // fixed amount of preceding context is enough to work out where the pairs fall.+ const flags = "\u{1f1e6}".repeat(129) + "\u{1f1e8}\u{1f1e9}";+ const index = buildTextIndex(el("DIV", [el("P", [text(flags)])]));+ // The 129th indicator pairs with the one after it, so the last two are not a grapheme of their own.+ assert.deepEqual(findMatches(index, "\u{1f1e8}\u{1f1e9}", 10), []);+});++test("a capped search does not pay twice for the same segmentation", () => {+ // A capped search walks its candidates more than once, to count them and again to take the+ // window around the viewport. The segmentation is made once and kept for as long as the index+ // lives, and is asked one offset at a time rather than block by block.+ const body = "\uac00\ub098\ub2e4".repeat(20_000);+ const index = buildTextIndex(el("DIV", [el("P", [text(body)])]));+ const anchor = () => index.text.length - 10;+ const first = Date.now();+ assert.equal(+ findMatches(index, "\uac00", MAX_MATCHES + 1, anchor).length,+ MAX_MATCHES + 1,+ );+ const cost = Date.now() - first;+ const second = Date.now();+ findMatches(index, "\uac00", MAX_MATCHES + 1, anchor);+ // The second search reuses the segmentation, so it cannot be slower than the first was.+ assert.ok(+ Date.now() - second <= cost + 50,+ `second search took ${Date.now() - second}ms against ${cost}ms`,+ );+});++test("a cluster is offered its longest spelling first", () => {+ // Both spellings of one cluster, and alternation takes the first that fits. Shortest first, the+ // bare `i` won, the match ended between the letter and its dot, and the fence threw the whole+ // occurrence away rather than reaching for the longer spelling.+ const dotted = buildTextIndex(el("DIV", [el("P", [text("İstanbul")])]));+ assert.deepEqual(findMatches(dotted, "i", 10), [{ start: 0, end: 2 }]);+ assert.deepEqual(findMatches(dotted, "istanbul", 10), [{ start: 0, end: 9 }]);+});++test("a portal is its own surface, whatever the workspace ended on", () => {+ // Nothing a portal paints carries on from text cut out of the workspace behind it. The block+ // branch clears the cut leaving any block tag, which covered a `DIV` scope and `DIV` portal and+ // hid it for everything else: the boundary belongs to the portal, not to the tags either side.+ const index = buildTextIndex(+ el("SPAN", [text(`${"x".repeat(MAX_NODE_CHARS)}\u1100`)]),+ [el("SPAN", [text("\uac00 hello")])],+ );+ assert.equal(findMatches(index, "\uac00", 10).length, 1);+});++test("the time between two searches is not charged to either", () => {+ // The budget is wall time, and it is measured in blocks, so a search that ends inside one leaves+ // the clock running. The next search closes that block and is billed for everything in between,+ // which is the reader thinking. Two cheap queries and a pause bought a whole-index scan.+ const probe = `+ let scans = 0;+ const Real = Intl.Segmenter;+ Intl.Segmenter = class {+ constructor(...args) { this.inner = new Real(...args); }+ segment(input) {+ const segments = this.inner.segment(input);+ return {+ containing: (at) => segments.containing(at),+ [Symbol.iterator]: () => { scans += 1; return segments[Symbol.iterator](); },+ };+ }+ };+ const { buildTextIndex, findMatches, MAX_MATCHES } = await import(${JSON.stringify(+ new URL(+ "../src/features/find-in-page/lib/find-text-index.ts",+ import.meta.url,+ ).href,+ )});+ const el = (tagName, childNodes) => ({+ nodeType: 1, tagName, childNodes, getAttribute: () => null,+ });+ // Hangul, so every candidate is past the fast path and does ask the segmenter.+ const syllable = (at) => String.fromCodePoint(0xac00 + (at % 11172));+ let body = "";+ for (let at = 0; at < 100000; at += 1) body += syllable(at);+ const index = buildTextIndex(el("DIV", [el("P", [{ nodeType: 3, data: body }])]));+ const wait = (ms) => new Promise((done) => setTimeout(done, ms));+ // Each query needs well under a block of checks, so on its own none can reach the budget.+ for (let round = 1; round <= 4; round += 1) {+ findMatches(index, syllable(round * 13) + syllable(round * 13 + 1), MAX_MATCHES);+ await wait(60);+ }+ if (scans !== 0) throw new Error("bought a scan out of the pauses between searches");+ `;+ const run = spawnSync(+ process.execPath,+ ["--experimental-strip-types", "--input-type=module", "--eval", probe],+ { encoding: "utf8" },+ );+ assert.equal(run.status, 0, run.stderr);+});++test("what a seek is allowed to cost is time, not a number of them", () => {+ // A seek is not one price: 0.2us into a page of Hangul and 1236us into a page of flags, six+ // thousand to one on the same length of text, so any count is far too small for one and far too+ // large for the other. Counting let a flag-heavy page spend twenty thousand of the expensive kind+ // on its first search. Counted here, since the point is that they stop after a handful.+ const probe = `+ let seeks = 0;+ const Real = Intl.Segmenter;+ Intl.Segmenter = class {+ constructor(...args) { this.inner = new Real(...args); }+ segment(input) {+ const segments = this.inner.segment(input);+ return {+ containing: (at) => { seeks += 1; return segments.containing(at); },+ [Symbol.iterator]: () => segments[Symbol.iterator](),+ };+ }+ };+ const { buildTextIndex, findMatches, MAX_MATCHES, MAX_NODE_CHARS } = await import(${JSON.stringify(+ new URL(+ "../src/features/find-in-page/lib/find-text-index.ts",+ import.meta.url,+ ).href,+ )});+ const el = (tagName, childNodes) => ({+ nodeType: 1, tagName, childNodes, getAttribute: () => null,+ });+ const flag = (at) => String.fromCodePoint(0x1f1e6 + (at % 26));+ let run = "";+ for (let at = 0; at < MAX_NODE_CHARS / 2; at += 1) run += flag(at);+ const nodes = [];+ for (let at = 0; at < 13; at += 1) nodes.push({ nodeType: 3, data: run });+ const index = buildTextIndex(el("DIV", [el("P", nodes)]));+ const started = Date.now();+ // A pair that straddles two flags, so every candidate is rejected and every one asks.+ findMatches(index, flag(1) + flag(2), MAX_MATCHES);+ const took = Date.now() - started;+ // A count-based cap spends about 20,000 of these; a budget stops inside a few blocks of them.+ if (seeks > 2000) throw new Error("seeks: " + seeks + " in " + took + "ms");+ `;+ const run = spawnSync(+ process.execPath,+ ["--experimental-strip-types", "--input-type=module", "--eval", probe],+ { encoding: "utf8" },+ );+ assert.equal(run.status, 0, run.stderr);+});++test("a capped search does not segment the whole index to answer its first pass", () => {+ // The scan that replaces the seeks costs a pass over the whole index, so what it is allowed to+ // replace matters as much as that it exists. On a flat count a bounded pass reaches the threshold+ // on a big enough index and buys the whole document to answer ten thousand cheap questions.+ const probe = `+ let scans = 0;+ let seeks = 0;+ const Real = Intl.Segmenter;+ Intl.Segmenter = class {+ constructor(...args) { this.inner = new Real(...args); }+ segment(input) {+ const segments = this.inner.segment(input);+ return {+ containing: (at) => { seeks += 1; return segments.containing(at); },+ [Symbol.iterator]: () => { scans += 1; return segments[Symbol.iterator](); },+ };+ }+ };+ const { buildTextIndex, findMatches, MAX_MATCHES, MAX_NODE_CHARS } = await import(${JSON.stringify(+ new URL(+ "../src/features/find-in-page/lib/find-text-index.ts",+ import.meta.url,+ ).href,+ )});+ const el = (tagName, childNodes) => ({+ nodeType: 1, tagName, childNodes, getAttribute: () => null,+ });+ const nodes = [];+ for (let at = 0; at < 2000000; at += MAX_NODE_CHARS) {+ nodes.push({ nodeType: 3, data: "\uac00".repeat(MAX_NODE_CHARS) });+ }+ const index = buildTextIndex(el("DIV", [el("P", nodes)]));+ // What a seek costs on THIS machine, since that is what the budget is spent in and it varies+ // by orders of magnitude between engines and hosts. Measured through the same wrapper, then+ // discounted so the count it feeds is not itself the thing under test.+ const sample = new Intl.Segmenter(undefined, { granularity: "grapheme" }).segment(index.text);+ sample.containing(0);+ const startedSample = performance.now();+ for (let at = 0; at < 200; at += 1) sample.containing((at * 9871) % index.text.length);+ const perSeek = (performance.now() - startedSample) / 200;+ seeks = 0;+ // Anchored at the top, so the cap stops the search after one bounded pass.+ const found = findMatches(index, "\uac00", MAX_MATCHES, 0);+ if (found.length !== MAX_MATCHES) throw new Error("expected a capped search, got " + found.length);+ if (seeks > 4 * MAX_MATCHES) throw new Error("seeks: " + seeks);+ // The budget is what one scan of this index would cost, so a bounded pass reaches it only+ // where seeking is expensive enough that the scan is the better buy anyway. Assert the waste+ // this guards against, which is scanning while the seeks were still the cheaper option.+ const budgetMs = index.text.length / 16000;+ if (scans !== 0 && seeks * perSeek < budgetMs / 2) {+ throw new Error(+ "scanned after " + seeks + " seeks costing " + (seeks * perSeek).toFixed(1) ++ "ms, well inside a budget of " + budgetMs.toFixed(0) + "ms"+ );+ }+ `;+ const run = spawnSync(+ process.execPath,+ ["--experimental-strip-types", "--input-type=module", "--eval", probe],+ { encoding: "utf8" },+ );+ assert.equal(run.status, 0, run.stderr);+});++test("a query that matches everywhere stops seeking the segmenter per candidate", () => {+ // `containing` seeks, which is why it replaced segmenting whole blocks, and a seek per candidate+ // undoes that: a capped search anchored near the end walks the candidates up to three times, so a+ // page of one repeated syllable asked for millions. Counted, so the assertion is not the clock.+ const probe = `+ let seeks = 0;+ const Real = Intl.Segmenter;+ Intl.Segmenter = class {+ constructor(...args) { this.inner = new Real(...args); }+ segment(input) {+ const segments = this.inner.segment(input);+ return {+ containing: (at) => { seeks += 1; return segments.containing(at); },+ [Symbol.iterator]: () => segments[Symbol.iterator](),+ };+ }+ };+ const { buildTextIndex, findMatches, MAX_MATCHES, MAX_NODE_CHARS } = await import(${JSON.stringify(+ new URL(+ "../src/features/find-in-page/lib/find-text-index.ts",+ import.meta.url,+ ).href,+ )});+ const el = (tagName, childNodes) => ({+ nodeType: 1, tagName, childNodes, getAttribute: () => null,+ });+ const total = 400000;+ const nodes = [];+ for (let at = 0; at < total; at += MAX_NODE_CHARS) {+ nodes.push({ nodeType: 3, data: "\uac00".repeat(MAX_NODE_CHARS) });+ }+ const index = buildTextIndex(el("DIV", [el("P", nodes)]));+ const found = findMatches(index, "\uac00", MAX_MATCHES, index.text.length);+ if (found.length !== MAX_MATCHES) throw new Error("expected a capped search, got " + found.length);+ // One pass over the boundaries replaces the seeks, so what is left is what the budget bought+ // before it. The number is not fixed: the cap is time, and a seek into Hangul is cheap, so+ // this shape gets tens of thousands where a page of flags would get tens. 1.6M without it.+ if (seeks > 200000) throw new Error("seeks per candidate: " + seeks);+ `;+ const run = spawnSync(+ process.execPath,+ ["--experimental-strip-types", "--input-type=module", "--eval", probe],+ { encoding: "utf8" },+ );+ assert.equal(run.status, 0, run.stderr);+});++test("a CRLF is one grapheme, which the fast path has to know about", () => {+ // The fast path rests on nothing below U+0300 joining, and CR before LF is the one thing there+ // that does (GB3). The sweep that proved the rest excused the line breaks, a collapsed newline+ // being its own grapheme. In a `<pre>` nothing is collapsed, the pair arrives intact, and every+ // character the shortcut looks at is ASCII, so it answered without asking anyone.+ const fence = el("PRE", [text("line\r\nnext")]);+ withStyles(new Map([[fence, { whiteSpace: "pre" }]]), () => {+ const index = buildTextIndex(fence);+ assert.equal(index.segments[0].preserved, true);+ assert.equal(index.text.includes("\r\n"), true);+ // The feed alone is the back half of a grapheme, so it is not a match on its own.+ assert.deepEqual(findMatches(index, "\n", 10), []);+ assert.deepEqual(findMatches(index, "\nnext", 10), []);+ // Asked for whole it is, and so is everything that was findable before.+ assert.equal(findMatches(index, "\r\n", 10).length, 1);+ assert.equal(findMatches(index, "\r\nnext", 10).length, 1);+ assert.equal(findMatches(index, "next", 10).length, 1);+ });+ // A feed with no return in front of it is its own grapheme and still findable.+ const alone = el("PRE", [text("line\nnext")]);+ withStyles(new Map([[alone, { whiteSpace: "pre" }]]), () => {+ assert.equal(findMatches(buildTextIndex(alone), "\n", 10).length, 1);+ });+});++test("a per-node cut does not hand back half a grapheme", () => {+ // The cut can land between a letter and its mark, where the text says the letter is whole.+ const node = text(`${"z".repeat(MAX_NODE_CHARS - 1)}Q\u0301tail`);+ const index = buildTextIndex(el("DIV", [el("P", [node, text(" after")])]));+ assert.equal(index.truncated, true);+ assert.equal(index.text[MAX_NODE_CHARS - 1], "q");+ assert.deepEqual(findMatches(index, "Q", 10), []);+ // The far side of the same cut, settled from the text the cut dropped rather than assumed.+ assert.equal(findMatches(index, "after", 10).length, 1);+});++test("what a clip dropped cannot reach past the end of its block", () => {+ // A Prepend at the end of the dropped suffix joined the next block's first character.+ const clipped = `${"z".repeat(MAX_NODE_CHARS)}dropped\u0600`;+ const index = buildTextIndex(+ el("SPAN", [el("DIV", [text(clipped)]), text("a")]),+ );+ assert.equal(index.truncated, true);+ assert.equal(index.text.endsWith(`${BLOCK_SEPARATOR}a`), true);+ assert.equal(findMatches(index, "a", 5).length, 1);+});++test("a cut inside a run reads back to an anchor, not to a fixed window", () => {+ // Indicators pair off from the START of a run, so an ODD run's last 32 units read as even.+ const ri = (n: number) => String.fromCodePoint(0x1f1e6 + n);+ const [a, b, c] = [ri(0), ri(1), ri(2)];+ const node = `x${a.repeat(49_998)}${b}${c}tail`;+ const index = buildTextIndex(+ el("DIV", [el("P", [text(node), text(" next")])]),+ );+ assert.equal(index.truncated, true);+ // The cut lands right after b, and c is dropped -- but the page pairs them.+ assert.equal(index.text.codePointAt(MAX_NODE_CHARS - 3), 0x1f1e7);+ assert.deepEqual(findMatches(index, b, 5), []);+ // An anchor a few characters back is still read exactly, so an ordinary cut still decides.+ const plain = buildTextIndex(+ el("DIV", [+ el("P", [text(`${"z".repeat(MAX_NODE_CHARS - 1)}Q tail`), text(" next")]),+ ]),+ );+ assert.equal(findMatches(plain, "Q", 5).length, 1);+});++test("the end of a truncated index is not a boundary by default", () => {+ // `at === text.length` is the one offset with nothing after it to read, and was accepted flat.+ const kids = [];+ for (let i = 0; i < 39; i += 1) kids.push(text("z".repeat(MAX_NODE_CHARS)));+ kids.push(text(`${"z".repeat(MAX_NODE_CHARS - 1)}Q`));+ kids.push(text("\u0301rest"));+ const index = buildTextIndex(el("DIV", [el("P", kids)]));+ assert.equal(index.text.length, MAX_INDEX_CHARS);+ assert.equal(index.truncated, true);+ assert.deepEqual(findMatches(index, "Q", 10), []);+});++test("a node left out entirely still says whether the end was a boundary", () => {+ // One code unit of room and a pair next means the whole node goes. That is not a reason to call+ // the end unknown: the node is still there to be read and its first code point answers it, as in+ // the ceiling check above. Assuming otherwise threw away the last match in the index.+ const nodes = [];+ for (+ let at = 0;+ at < MAX_INDEX_CHARS - 1 - MAX_NODE_CHARS;+ at += MAX_NODE_CHARS+ ) {+ nodes.push(text("z".repeat(MAX_NODE_CHARS)));+ }+ const used = nodes.length * MAX_NODE_CHARS;+ nodes.push(text(`${"z".repeat(MAX_INDEX_CHARS - 2 - used)}Q`));+ nodes.push(text(`${String.fromCodePoint(0x1f600)}tail`));+ const index = buildTextIndex(el("DIV", [el("P", nodes)]));+ // One unit short of the ceiling, so the pair could not fit and the node was dropped whole.+ assert.equal(index.text.length, MAX_INDEX_CHARS - 1);+ assert.equal(index.truncated, true);+ // The emoji begins its own grapheme, so what the index ends on is a boundary and stays findable.+ assert.equal(findMatches(index, "Q", 10).length, 1);+ // And when the next node does join, the end is still unknown.+ const joining = [...nodes.slice(0, -1), text("́tail")];+ const joined = buildTextIndex(el("DIV", [el("P", joining)]));+ assert.deepEqual(findMatches(joined, "Q", 10), []);+});++test("a mark on a space in the query survives the space flexing", () => {+ // Whitespace in a query matches any run of it, a soft-wrapped paragraph rendering as one line+ // while its node holds the newline. A mark on that space is part of the same grapheme and went+ // with it, so the match ended mid-grapheme and the fence discarded text that is on the page.+ const index = buildTextIndex(el("DIV", [el("P", [text(" ́")])]));+ assert.equal(findMatches(index, " ́", 10).length, 1);+ // The flexing itself is untouched: a plain space still matches a run of whitespace.+ const wrapped = buildTextIndex(el("DIV", [el("P", [text("one \n two")])]));+ assert.equal(findMatches(wrapped, "one two", 10).length, 1);+});++test("nothing below U+0300 can join a grapheme, which is what the fast path rests on", () => {+ // Latin prose skips the segmenter on four comparisons, and every one of them assumes no character+ // below U+0300 can extend a grapheme or be extended into one. The far side of a cut leans on the+ // same fact. Asserted over every code point rather than argued from the blocks they sit in.+ const segmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });+ const joiners: string[] = [];+ for (let code = 1; code < 0x300; code += 1) {+ // CR, LF and the other line breaks are their own cluster and never in an index unsplit.+ if (code >= 0x0a && code <= 0x0d) continue;+ const point = String.fromCodePoint(code);+ if (+ [...segmenter.segment(`${point}a`)].length === 1 ||+ [...segmenter.segment(`a${point}`)].length === 1+ ) {+ joiners.push(code.toString(16));+ }+ }+ assert.deepEqual(joiners, []);+});+test("a match with no geometry is aimed at through its nearest laid-out ancestor", async () => {const dom = await readFile(new URL("../src/features/find-in-page/lib/find-dom.ts", import.meta.url),"utf8",);- // Text inside a skipped subtree has a collapsed rect while the subtree's own box keeps its- // placeholder geometry, so the walk aims at that instead of giving up.+ // Such text has a collapsed rect while the subtree's own box keeps its placeholder geometry.assert.match(dom,/export function revealRect\(range: Range\): DOMRect \| null/,);assert.match(dom, /export function rangeTop\(range: Range\): number \| null/);- // And both readers go through it rather than reading the range rect directly.const engine = await readFile(new URL("../src/features/find-in-page/hooks/use-find-in-page.ts",@@ -1805,8 +2471,7 @@ test("a fresh query starts from the scroll container's top, not the window's", a),"utf8",);- // The thread viewport starts below the navbar and the chat header, so a match clipped just off- // the top of it still has a positive window-relative `top`.+ // The viewport starts below the navbar, so a match clipped off its top still has positive `top`.assert.match(engine, /top >= scrollViewportTop\(range\)/);assert.equal(/top >= 0/.test(engine), false);});@@ -1837,8 +2502,7 @@ test("re-indexing while the document changes is a throttle, and says so", async),"utf8",);- // A debounce would leave the count frozen and new text unfindable for as long as a reply takes- // to write. The name has to match the behaviour, which is what went wrong before.+ // A debounce would freeze the count for as long as a reply takes to write.assert.match(engine, /REINDEX_INTERVAL_MS/);assert.equal(/REINDEX_DEBOUNCE_MS/.test(engine), false);assert.match(engine, /A throttle rather than a debounce/);@@ -1859,8 +2523,7 @@ test("the bar has no border, and its buttons have a hover that shows", async ()false,"the bar took a border back",);- // The ghost variant's own `--muted/50` hover lands within a shade of this surface, so every- // button in the bar overrides it.+ // The ghost variant's own `--muted/50` hover lands within a shade of this surface.assert.match(bar, /hover:bg-black\/\[0\.06\] dark:hover:bg-white\/10/);assert.equal((bar.match(/className=\{FIND_BUTTON_CLASS\}/g) ?? []).length, 3);});@@ -1878,15 +2541,11 @@ test("a long query rewinds to its first character when focus leaves", async () =assert.match(bar, /onBlur=\{rewindToStart\}/);assert.match(bar, /input\.setSelectionRange\(0, 0\);/);assert.match(bar, /input\.scrollLeft = 0;/);- // The walk buttons must not trigger it: they cancel their own mousedown, so the field never- // loses focus and the caret stays where the reader left it.assert.match(bar, /onMouseDown=\{keepFocusInField\}/);});test("the observer watches the attributes a workspace switch flips", async () => {- // Chat and Images are both kept alive by the shell, so switching between them adds and removes- // nothing -- it flips `inert`. A childList observer would never hear it, and the bar would go on- // counting the workspace the user just left.+ // Switching between kept-alive workspaces flips `inert` rather than mutating children.const engine = await readFile(new URL("../src/features/find-in-page/hooks/use-find-in-page.ts",@@ -1895,11 +2554,10 @@ test("the observer watches the attributes a workspace switch flips", async () =>"utf8",);assert.match(engine, /attributeFilter: \[[^\]]*"inert"/);- // `open` too: toggling a `<details>` changes that and nothing else, while the body inside it- // goes from visible to not, so a collapsible opened after indexing would stay unfindable.+ // `open` too: it is all a `<details>` changes, while its body goes from visible to not.assert.match(engine, /attributeFilter: \[[^\]]*"open"/);- // And not the whole attribute stream: `class` changes on every hover. Scanned rather than- // matched, since the comments in between make a regex for this one backtrack badly.+ // Not the whole stream: `class` changes on every hover. Scanned, not matched, since the+ // comments in between make a regex backtrack badly.const opensAttributes = engine.indexOf("attributes: true,");const opensFilter = engine.indexOf("attributeFilter:", opensAttributes);assert.ok(opensAttributes !== -1 && opensFilter > opensAttributes);@@ -2008,16 +2666,12 @@ test("the observer watches the document, since a portal lands outside the scope");// A popover renders to the body: an observer on the scope alone never hears one open or close.assert.match(engine, /scope\?\.ownerDocument\?\.body \?\? scope/);- // And `data-state` is the only thing a dismissed one changes. It keeps its box until the- // animation that follows finishes, so without this it stays in the count after it is gone.+ // `data-state` is all a dismissed one changes, and it keeps its box until the animation ends.assert.match(engine, /attributeFilter: \[[^\]]*"data-state"/);});test("the rows progressive completion adds are re-anchored, not renumbered", async () => {- // Streaming APPENDS, so keeping the ordinal is right there. Progressive completion PREPENDS the- // older half of a thread, and match 3 of the tail is not match 3 of the whole conversation:- // keeping the number moves the highlight to an older match off screen, and the next step from- // there walks the reader backwards.+ // Progressive completion PREPENDS, and match 3 of the tail is not match 3 of the thread.const engine = await readFile(new URL("../src/features/find-in-page/hooks/use-find-in-page.ts",@@ -2029,8 +2683,7 @@ test("the rows progressive completion adds are re-anchored, not renumbered", asyengine,/completeProgressiveMounts\([\s\S]*?\.then\(\(\) => \{[\s\S]*?search\(false, reindex\(\)\);/,);- // Through `reindex`, which answers false when the completion brought nothing in, so a settled- // thread's 400ms probe does not take back an Enter pressed while it ran.+ // `reindex` answers false when nothing came in, so a settled thread's probe takes back no Enter.assert.equal(engine.includes("rebuild("), false);});@@ -2053,14 +2706,12 @@ test("Escape closes the bar from the walk buttons, not just the field", async ()assert.match(body, /event\.preventDefault\(\);/);assert.match(body, /event\.stopPropagation\(\);/);assert.match(body, /close\(\);/);- // Capture, so it runs ahead of the registry's own keydown listener rather than racing it.assert.match(effect, /window\.addEventListener\("keydown", onEscape, true\)/);assert.match(effect,/window\.removeEventListener\("keydown", onEscape, true\)/,);- // Two presses it deliberately does not take: a modal above the bar owns Escape, and an open- // popover, menu or listbox is dismissed by its own Escape first.+ // A modal above the bar owns Escape, and an open popover is dismissed by its own first.assert.match(body, /isSurfaceBackgrounded\(/);assert.match(body, /resolveDismissiblePortalSurfaces\(/);// And nothing left on the landmark, which would take the inside presses before the window does.@@ -2072,9 +2723,7 @@ test("Escape closes the bar from the walk buttons, not just the field", async ()});test("only threads this search can read are forced to finish mounting", async () => {- // The shell keeps every workspace mounted and marks the off-route ones `inert`. Completing- // globally would make a retained conversation mount every row it withheld, on a route where the- // walk then skips all of it.+ // Completing globally would make a retained conversation mount every row it withheld.const engine = await readFile(new URL("../src/features/find-in-page/hooks/use-find-in-page.ts",@@ -2093,8 +2742,7 @@ test("only threads this search can read are forced to finish mounting", async ()),"utf8",);- // The loop's exit has to read the filtered set too, or a completer this caller declined holds it- // open forever.+ // The exit has to read the filtered set, or a declined completer holds it open forever.assert.match(progressive,/if \(wanted\(\)\.length === 0 && \(observed \|\| Date\.now\(\) >= deadline\)\)/,@@ -2102,16 +2750,9 @@ test("only threads this search can read are forced to finish mounting", async ()});test("the chord is left to the browser when the scope is behind a modal", async () => {- // `useShortcut` prevents the event BEFORE calling the handler, so declining from inside the- // handler kills the chord for everyone: no bar, and no native find either.- const bar = await readFile(- new URL(- "../src/features/find-in-page/components/find-in-page.tsx",- import.meta.url,- ),- "utf8",- );- assert.match(bar, /claims: \(\) => !isSurfaceBackgrounded\(/);+ // `useShortcut` prevents the event BEFORE the handler, so declining inside it kills the chord.+ const controller = await readComponentSource();+ assert.match(controller, /claims: \(\) => !isSurfaceBackgrounded\(/);const shortcut = await readFile(new URL("../src/features/settings/hooks/use-shortcut.ts", import.meta.url),"utf8",@@ -2149,15 +2790,11 @@ test("closing the bar hands focus back to where it came from", async () => {const takeFocus = bar.indexOf("input.select();");assert.ok(capture > 0 && capture < takeFocus);assert.match(bar, /origin\.focus\(\);/);- // First answer only, and never the bar. StrictMode replays the effect in development, and by the- // second run the field has focus: read plainly, the bar would aim focus at its own input.+ // First answer only: StrictMode replays the effect, and by the second run the field has focus.assert.match(bar, /originRef\.current === null &&/);- // Against the bar's own element, not `data-find-skip`. The composer carries that attribute, and- // it is the single most likely place the chord is pressed from: matching on it would refuse to- // record exactly the origin this exists to restore.+ // Against the bar's element, not `data-find-skip`, which the composer carries.assert.match(bar, /barRef\.current\?\.contains\(active\) !== true/);assert.equal(bar.includes("closest(`[${FIND_SKIP_ATTRIBUTE}]`)"), false);- // And only when closing dropped focus: the reader having moved it is not an invitation.assert.match(bar,/if \(focused !== null && focused !== document\.body\) return;/,@@ -2165,8 +2802,7 @@ test("closing the bar hands focus back to where it came from", async () => {});test("the chat composer is out of the searchable scope", async () => {- // Its draft lives in a textarea the index cannot read, so all it leaves find is the pill labels:- // a search for "code" or "images" would land on the toolbar rather than the conversation.+ // Its draft lives in a textarea the index cannot read, leaving find only the pill labels.const thread = await readFile(new URL("../src/components/assistant-ui/thread.tsx", import.meta.url),"utf8",@@ -2186,7 +2822,6 @@ test("the reader is kept on the occurrence, not on the number", async () => {),"utf8",);- // Written where the active match is settled, read where the next list is built.assert.match(engine,/activeStartRef\.current = active >= 0 \? matches\[active\]\.start : null;/,@@ -2196,7 +2831,6 @@ test("the reader is kept on the occurrence, not on the number", async () => {const install = engine.indexOf("matchesRef.current = matches;", read - 400);assert.ok(read > 0 && read < install);assert.match(engine, /ordinalOfStart\(matches, wasAt\)/);- // And when the occurrence is gone, the reader's position decides rather than a stale number.assert.match(engine,/at === -1 \? firstMatchFromViewport\(index, matches\) : at/,@@ -2204,11 +2838,8 @@ test("the reader is kept on the occurrence, not on the number", async () => {});test("the ordinal survives an append and nothing else", async () => {- // One rule decides who keeps their place. A streaming reply only adds at the tail, so match 20 is- // still match 20. History arriving above, a workspace switch flipping `inert`, a breakpoint- // revealing a column: each renumbers the list, and the reader's number then points at unrelated- // text. An unchanged document is an append of nothing, which is why a rebuild that finds no news- // leaves an Enter pressed while it ran alone.+ // A streaming reply only adds at the tail; history above, `inert` flipping or a breakpoint+ // revealing a column each renumber the list.const engine = await readFile(new URL("../src/features/find-in-page/hooks/use-find-in-page.ts",@@ -2221,10 +2852,8 @@ test("the ordinal survives an append and nothing else", async () => {engine,/return renumbersMatches\(before, indexRef\.current, activeStartRef\.current\);/,);- // Every rebuild goes through it: no call site decides for itself that the numbering held.assert.equal(engine.includes("search(false, false)"), false);assert.equal((engine.match(/search\(false, reindex\(\)\)/g) ?? []).length, 2);- // Except a fresh open, which starts from the reader whatever the index says.assert.match(engine,/reindex\(\);\n\s*\/\/[^\n]*\n\s*search\(false, true\);/,@@ -2424,8 +3053,7 @@ test("history arriving above the reader still renumbers the list", () => {});test("a breakpoint that changes what is rendered invalidates the index", async () => {- // Crossing one hides or reveals whole columns (`hidden lg:flex`) with nothing in the DOM to- // observe, and the index reads computed visibility, so the observer alone cannot see it.+ // Crossing one reveals whole columns with nothing in the DOM to observe.const engine = await readFile(new URL("../src/features/find-in-page/hooks/use-find-in-page.ts",@@ -2435,8 +3063,6 @@ test("a breakpoint that changes what is rendered invalidates the index", async ();assert.match(engine, /window\.addEventListener\("resize", invalidate\);/);assert.match(engine, /window\.removeEventListener\("resize", invalidate\);/);- // Through the same throttle as a mutation, so dragging a window edge costs one rebuild an- // interval rather than one a frame.assert.match(engine,/const invalidate = \(\) => \{[\s\S]*?REINDEX_INTERVAL_MS\);/,@@ -2461,9 +3087,6 @@ test("closing preserves the query while leaving the shell forgets the session",});test("the capped window follows the reader, not the top of the document", () => {- // A single common letter in a long thread has far more matches than the cap. Keeping the first- // `limit` of them keeps only the top of the document, so a reader at the bottom is walked away- // from every occurrence beside them, to one they were not looking for.const body = `${"q".repeat(20_000)} needle ${"q".repeat(20_000)}`;const index = buildTextIndex(el("DIV", [el("P", [text(body)])]));const anchor = index.text.indexOf("needle");@@ -2475,7 +3098,6 @@ test("the capped window follows the reader, not the top of the document", () =>const aroundTheReader = findMatches(index, "q", limit, anchor);assert.equal(aroundTheReader.length, limit);- // Half either side, so the walk goes forward from where the reader is and back the other way.assert.equal(aroundTheReader.some((match) => match.start < anchor),true,@@ -2489,10 +3111,7 @@ test("the capped window follows the reader, not the top of the document", () =>});test("a capped window slides as matches are appended", () => {- // Which is why an ordinal cannot be kept across a rebuild even when the document only grew. The- // window recentres, so the same number is a different occurrence.- // The reader near the end, which is where a reply streams in: too few matches after them to fill- // half the window, so the window is pinned to the end of the list and the end is what moves.+ // The window recentres, so the same number is a different occurrence.const limit = 100;const before = `${"q".repeat(200)} needle ${"q".repeat(20)}`;const after = `${before}${"q".repeat(200)}`;@@ -2511,10 +3130,7 @@ test("a capped window slides as matches are appended", () => {);assert.equal(first.length, limit);assert.equal(second.length, limit);- // Same ordinal, different occurrence: the number moved under the reader.assert.notEqual(first[limit - 1].start, second[limit - 1].start);- // The occurrence itself is still in the list, at a different index. That is what the engine- // follows instead of the number.assert.equal(second.some((match) => match.start === first[limit - 1].start),true,@@ -2522,7 +3138,6 @@ test("a capped window slides as matches are appended", () => {});test("the window is only computed when the cap bites", () => {- // Under the cap this is the single pass it always was, whatever the anchor says.const index = buildTextIndex(el("DIV", [el("P", [text("q q q q q")])]));assert.deepEqual(findMatches(index, "q", 100, 8),@@ -2531,37 +3146,29 @@ test("the window is only computed when the cap bites", () => {});test("stopping the count early does not move the window", () => {- // `total` is only counted to keep the window off the end of the list, and it stops as soon as it- // is high enough to no longer do that. A stop that came too soon would drag the window back- // toward the top, which is the whole thing the anchor exists to prevent.+ // A stop that came too soon would drag the window back toward the top.const body = "q".repeat(500);const index = buildTextIndex(el("DIV", [el("P", [text(body)])]));const at = 200;const window = findMatches(index, "q", 50, at);assert.equal(window.length, 50);- // Centred on the reader: 25 kept behind them, the rest ahead.assert.equal(window[0].start, at - 25);assert.equal(window[window.length - 1].end, at + 25);- // And the matches past the window, which the count stops before reaching, are really there.assert.equal(findMatches(index, "q", 500, 0).length, 500);});test("the window stops at the ends of the list", () => {const body = `needle ${"q".repeat(500)}`;const index = buildTextIndex(el("DIV", [el("P", [text(body)])]));- // Anchored at the very start: nothing to keep before it, so the window is the first `limit`.const atTheTop = findMatches(index, "q", 50, 1);assert.equal(atTheTop[0].start, index.text.indexOf("q"));- // Anchored past the end: the window is the last `limit`, not a slice running off it.const atTheEnd = findMatches(index, "q", 50, index.text.length);assert.equal(atTheEnd.length, 50);assert.equal(atTheEnd[atTheEnd.length - 1].end, index.text.length);});test("clipped accessibility text is not searchable", () => {- // Tailwind's `sr-only` keeps a real box at full opacity and clips it to nothing, so- // `checkVisibility` calls it visible. The app has 46 of them; counted, they are matches with a- // highlight clipped away along with the text.+ // `sr-only` keeps a real box at full opacity, which `checkVisibility` calls visible.const label = el("SPAN", [text("Data input")]);const shown = el("SPAN", [text("Data output")]);withStyles(@@ -2575,7 +3182,6 @@ test("clipped accessibility text is not searchable", () => {assert.equal(index.text.includes("data output"), true);},);- // The legacy spelling of the same idiom.const legacy = el("SPAN", [text("Data input")]);withStyles(new Map([[legacy, { clip: "rect(0px, 0px, 0px, 0px)" }]]), () => {assert.equal(buildTextIndex(el("DIV", [legacy])).text, "");@@ -2583,9 +3189,7 @@ test("clipped accessibility text is not searchable", () => {});test("the counter says '+' only when the cap actually cut something off", () => {- // `findMatches` stops at the limit it is given, so a count equal to the cap cannot say whether it- // is the total or a floor: a page holding exactly MAX_MATCHES read as "more than MAX_MATCHES".- // Asking for one past it is the whole difference; the extra match is thrown away.+ // A count equal to the cap cannot say whether it is the total or a floor.const occurrences = (n: number) =>findMatches(buildTextIndex(el("DIV", [el("P", [text("a".repeat(n))])])),@@ -2595,7 +3199,6 @@ test("the counter says '+' only when the cap actually cut something off", () =>assert.equal(occurrences(MAX_MATCHES - 1) > MAX_MATCHES, false);assert.equal(occurrences(MAX_MATCHES) > MAX_MATCHES, false);assert.equal(occurrences(MAX_MATCHES + 1) > MAX_MATCHES, true);- // The count itself cannot tell the last two apart, which is why the flag exists.assert.equal(findMatches(buildTextIndex(el("DIV", [el("P", [text("a".repeat(MAX_MATCHES))])])),@@ -2621,15 +3224,12 @@ test("the cap flag is what the bar renders, not the count", async () => {/findMatches\(\s*\n\s*index,\s*\n\s*queryRef\.current,\s*\n\s*MAX_MATCHES \+ 1,/,);assert.match(engine, /cappedRef\.current = matches\.length > MAX_MATCHES;/);- // Trimmed back to the cap, so nothing downstream sees the probe match -- and trimmed from the- // end the reader is further from, since a window anchored near the bottom of a long thread ends- // at the document's last match rather than starting at its first.+ // Trimmed from the end the reader is further from: a window anchored near the bottom ends at+ // the document's last match.assert.match(engine,/if \(cappedRef\.current\) dropProbeFurthestFrom\(matches, anchoredAt\);/,);- // The anchor is captured as the thunk resolves it, so the trim costs no second layout read and- // still costs nothing under the cap, where the thunk is never called.assert.match(engine, /let anchoredAt: number \| null = null;/);assert.match(engine, /anchoredAt = viewportOffset\(index\);/);const bar = await readFile(@@ -2657,8 +3257,7 @@ test("Escape is left to the IME while it is composing", async () => {const guard = escapeHandler.indexOf("isImeComposing(event)");const consume = escapeHandler.indexOf("event.preventDefault()");assert.ok(guard > 0 && guard < consume);- // Safe to let through: the global listener stands aside for a composing event before it looks- // for a binding, so the bare-Escape decline cannot take it either.+ // Safe: the global listener stands aside for a composing event before looking for a binding.const shortcut = await readFile(new URL("../src/features/settings/hooks/use-shortcut.ts", import.meta.url),"utf8",@@ -2670,11 +3269,8 @@ test("Escape is left to the IME while it is composing", async () => {});test("the selection fallback only clears what it put there", () => {- // The engines without a highlight registry get a selection instead, and the bar paints once on- // opening, before any query is typed. Clearing unconditionally there throws away whatever the- // reader had highlighted to copy, and closing cannot give it back.- // Boundary points, since engines differ on whether `getRangeAt` hands back the object that was- // added. Two distinct start containers stand in for two distinct selections.+ // The bar paints once on opening, before any query, so clearing unconditionally there throws+ // away what the reader had selected. Boundary points, since engines differ on `getRangeAt`.const span = (name: string) =>({startContainer: name,@@ -2709,7 +3305,6 @@ test("the selection fallback only clears what it put there", () => {// Annotated, or `deepEqual`'s assertion signature narrows `ranges` to `never[]` from here on.assert.deepEqual(ranges, [] as Range[]);- // And a selection the reader made while the bar was open, over the match this had put there.selectRangeFallback(span("the active match"));ranges.length = 0;ranges.push(span("dragged over something else"));@@ -2721,10 +3316,7 @@ test("the selection fallback only clears what it put there", () => {});test("the generated-image actions are out of the index too", async () => {- // Same shape as the badge below, and the same reason. From `sm` up the action bar over a- // generated image is transparent until the card is hovered, so its "Edit" was counted and walked- // to under a highlight nobody can see. Every place that mounts persistently transparent text has- // to say so, since the index cannot tell one from a message still fading in.+ // Persistently transparent text has to say so, since the index cannot tell it from a fade-in.const tool = await readFile(new URL("../src/components/assistant-ui/tool-ui-image-generation.tsx",@@ -2741,10 +3333,8 @@ test("the generated-image actions are out of the index too", async () => {});test("a hover-only badge is out of the index", async () => {- // The response model label is mounted transparent and unclickable until the message is hovered.- // That is an affordance, not an entrance animation, so a match in it would be counted and walked- // to under a highlight nobody can see. Marked at the call site rather than by turning the opacity- // check on, which would drop a message still fading in.+ // An affordance, not an entrance animation, so it is marked at the call site rather than by+ // turning the opacity check on.const sheet = await readFile(new URL("../src/components/assistant-ui/message-response-details-sheet.tsx",@@ -2757,7 +3347,6 @@ test("a hover-only badge is out of the index", async () => {badge.slice(0, badge.indexOf("aui-response-model-badge")),/\{\.\.\.\{ \[FIND_SKIP_ATTRIBUTE\]: "" \}\}/,);- // The general rule stays off, so a fade-in is still findable.const index = await readFile(new URL("../src/features/find-in-page/lib/find-text-index.ts",@@ -2769,9 +3358,8 @@ test("a hover-only badge is out of the index", async () => {});test("a container query resizing the scope invalidates the index", async () => {- // A container query does not need the window to change: Images is an `@container` with labels on- // `@[50rem]`, so pinning or collapsing the sidebar crosses that breakpoint with no window resize- // and no mutation inside the scope.+ // Images is an `@container`, so pinning the sidebar crosses a breakpoint with no resize and+ // no mutation inside the scope.const engine = await readFile(new URL("../src/features/find-in-page/hooks/use-find-in-page.ts",@@ -2782,8 +3370,7 @@ test("a container query resizing the scope invalidates the index", async () => {assert.match(engine, /new ResizeObserver\(\(\) => \{/);assert.match(engine, /sized\.observe\(scope\);/);assert.match(engine, /sized\?\.disconnect\(\);/);- // The first delivery reports the size the scope already had, which is not news and would cost a- // flatten on every open.+ // The first delivery is the size the scope already had, and would cost a flatten on every open.assert.match(engine,/if \(!measured\) \{\s*\n\s*measured = true;\s*\n\s*return;/,