@@ -128,16 +128,22 @@ export function installPageHelpers(): void {
128128 * Builds the page snapshot: a structural outline (headings, landmarks) with
129129 * interactive elements carrying numeric ids, walking open shadow roots and
130130 * same-origin iframes. Rebuilds the element registry as a side effect.
131+ * `markNew` false leaves the `new` markers out and records nothing as shown,
132+ * for internal reads (such as a text search) whose outline the model never sees.
131133 */
132- export function collectSnapshot ( startingElementId = 0 , elementId ?: number ) : unknown {
134+ export function collectSnapshot (
135+ startingElementId = 0 ,
136+ elementId : number | null = null ,
137+ markNew = true
138+ ) : unknown {
133139 const resolver = window . __simAgentResolveElement
134140 const scopedRoot =
135- elementId === undefined
141+ elementId === null
136142 ? undefined
137143 : resolver
138144 ? resolver ( elementId , false ) ?. element
139145 : window . __simAgentElements ?. [ elementId ]
140- if ( elementId !== undefined ) {
146+ if ( elementId !== null ) {
141147 if ( ! scopedRoot ?. isConnected ) return { error : 'stale' , reason : window . __simAgentStaleReason }
142148 if ( scopedRoot . ownerDocument !== document ) return { error : 'framed-snapshot' }
143149 }
@@ -238,9 +244,13 @@ export function collectSnapshot(startingElementId = 0, elementId?: number): unkn
238244 window . __simAgentElements = registry
239245 const previouslyShown = window . __simAgentShownElements
240246 const shown = previouslyShown ?? new WeakSet < Element > ( )
241- window . __simAgentShownElements = shown
247+ if ( markNew ) window . __simAgentShownElements = shown
242248 /** Whether no earlier snapshot of this document listed the element; the first snapshot marks nothing. */
243- const isNew = ( el : Element ) : boolean => previouslyShown !== undefined && ! shown . has ( el )
249+ const isNew = ( el : Element ) : boolean => markNew && previouslyShown !== undefined && ! shown . has ( el )
250+ /** Records an element whose line made it into the outline, so the next snapshot knows it. */
251+ const recordShown = ( el : Element ) : void => {
252+ if ( markNew ) shown . add ( el )
253+ }
244254 const lines : string [ ] = [ ]
245255 let truncated = false
246256 let refCount = 0
@@ -585,11 +595,11 @@ export function collectSnapshot(startingElementId = 0, elementId?: number): unkn
585595 }
586596 }
587597 if ( isNew ( el ) ) parts . push ( 'new' )
588- shown . add ( el )
589598 const suffix = parts . length > 0 ? ` ${ parts . join ( ' ' ) } ` : ''
590599 const lineIndex = lines . length
591600 if ( push ( `${ indent } - ${ role } ${ quote ( name ) } [ref=${ id } ]${ suffix } ` ) ) {
592601 refLineIndexes [ id ] = lineIndex
602+ recordShown ( el )
593603 }
594604 }
595605
@@ -608,9 +618,11 @@ export function collectSnapshot(startingElementId = 0, elementId?: number): unkn
608618 const id = registerElement ( el , roleFor ( el ) , text )
609619 textLineCount ++
610620 const marker = isNew ( el ) ? ' new' : ''
611- shown . add ( el )
612621 const lineIndex = lines . length
613- if ( push ( `${ indent } - text ${ quote ( text ) } [ref=${ id } ]${ marker } ` ) ) refLineIndexes [ id ] = lineIndex
622+ if ( push ( `${ indent } - text ${ quote ( text ) } [ref=${ id } ]${ marker } ` ) ) {
623+ refLineIndexes [ id ] = lineIndex
624+ recordShown ( el )
625+ }
614626 }
615627
616628 const headingLevel = ( el : Element ) : number | null => {
0 commit comments