fix(desktop): resume sibling traversal after a fully-consumed text node match
Team review (#81778) found the walker terminated sibling traversal when a text node was entirely consumed by a match: `current = textNode ? ... : null` — after replaceChild detaches the original node, its `.nextSibling` reads null, so `<div>needle<span>needle</span></div>` searching "needle" matched only the first occurrence. Any JSX bare-text + element sibling pattern (tool rows, attachment rows) could silently drop later matches. Capture the text node's own `nextSibling` BEFORE the first replaceChild and resume the outer walker from it on full consumption. The `after` split path is unaffected (the trailing text node is re-scanned). Regression test pins `<div>needle<span>needle</span></div><p>needle</p>` → 3 matches. 17/17 scope tests pass. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -205,6 +205,21 @@ describe('performScopedFind', () => {
|
||||
expect(result.activeOrdinal).toBe(2)
|
||||
expect(surface.querySelectorAll('mark.find-hit').length).toBe(2)
|
||||
})
|
||||
|
||||
it('keeps searching sibling subtrees after a fully-consumed text node', () => {
|
||||
// Regression (#81778 review): a text node that was entirely consumed by
|
||||
// a match used to null the walker's `current`, terminating the sibling
|
||||
// traversal — `<div>needle<span>needle</span></div>` only matched the
|
||||
// first occurrence. The sibling subtree must still be searched.
|
||||
const surface = plantSurface(
|
||||
'surface',
|
||||
'<div>needle<span>needle</span></div><p>needle</p>'
|
||||
)
|
||||
const result = performScopedFind(surface, 'needle', { forward: true, findNext: false })
|
||||
|
||||
expect(result.count).toBe(3)
|
||||
expect(surface.querySelectorAll('mark.find-hit').length).toBe(3)
|
||||
})
|
||||
})
|
||||
|
||||
describe('scope lifecycle', () => {
|
||||
|
||||
@@ -150,6 +150,13 @@ function highlightMatches(root: Element, query: string): HTMLElement[] {
|
||||
// Search within this text node. Splits may invalidate `textNode`'s
|
||||
// identity, so we re-read it from the parent each iteration.
|
||||
let nodeValue = textNode.nodeValue ?? ''
|
||||
// Capture the text node's own next sibling BEFORE any replaceChild:
|
||||
// once the original node is detached, `.nextSibling` reads null and
|
||||
// the outer walker would stop, skipping every following sibling
|
||||
// subtree (`<div>needle<span>needle</span></div>` searching "needle"
|
||||
// matched only the first). `continueFrom` is the sibling AFTER this
|
||||
// text node — for a fully-consumed match, the walker resumes there.
|
||||
const continueFrom = textNode.nextSibling
|
||||
|
||||
while (true) {
|
||||
const lower = nodeValue.toLowerCase()
|
||||
@@ -205,7 +212,15 @@ function highlightMatches(root: Element, query: string): HTMLElement[] {
|
||||
nodeValue = afterNode.nodeValue ?? ''
|
||||
}
|
||||
|
||||
current = textNode ? textNode.nextSibling : null
|
||||
if (textNode) {
|
||||
current = textNode.nextSibling
|
||||
} else {
|
||||
// The whole text node was consumed by matches — `textNode` was
|
||||
// detached by replaceChild so its own `.nextSibling` is null. Resume
|
||||
// the outer walker from the parent's next sibling (captured before
|
||||
// the first replaceChild), so the sibling subtree is still searched.
|
||||
current = continueFrom
|
||||
}
|
||||
}
|
||||
|
||||
return marks
|
||||
|
||||
Reference in New Issue
Block a user