@@ -40,6 +40,7 @@ export type AriaTreeOptions = {
4040 mode : 'ai' | 'expect' | 'codegen' | 'autoexpect' ;
4141 refPrefix ?: string ;
4242 doNotRenderActive ?: boolean ;
43+ depth ?: number ;
4344} ;
4445
4546type InternalOptions = {
@@ -563,6 +564,10 @@ function filterSnapshotDiff(nodes: (aria.AriaNode | string)[], statusMap: Map<ar
563564 return result ;
564565}
565566
567+ function indent ( depth : number ) : string {
568+ return ' ' . repeat ( depth ) ;
569+ }
570+
566571export function renderAriaTree ( ariaSnapshot : AriaSnapshot , publicOptions : AriaTreeOptions , previousSnapshot ?: AriaSnapshot ) : string {
567572 const options = toInternalOptions ( publicOptions ) ;
568573 const lines : string [ ] = [ ] ;
@@ -576,10 +581,12 @@ export function renderAriaTree(ariaSnapshot: AriaSnapshot, publicOptions: AriaTr
576581 if ( previousSnapshot )
577582 nodesToRender = filterSnapshotDiff ( nodesToRender , statusMap ) ;
578583
579- const visitText = ( text : string , indent : string ) => {
584+ const visitText = ( text : string , depth : number ) => {
585+ if ( publicOptions . depth && depth > publicOptions . depth )
586+ return ;
580587 const escaped = yamlEscapeValueIfNeeded ( renderString ( text ) ) ;
581588 if ( escaped )
582- lines . push ( indent + '- text: ' + escaped ) ;
589+ lines . push ( indent ( depth ) + '- text: ' + escaped ) ;
583590 } ;
584591
585592 const createKey = ( ariaNode : aria . AriaNode , renderCursorPointer : boolean ) : string => {
@@ -623,19 +630,24 @@ export function renderAriaTree(ariaSnapshot: AriaSnapshot, publicOptions: AriaTr
623630 return ariaNode ?. children . length === 1 && typeof ariaNode . children [ 0 ] === 'string' && ! Object . keys ( ariaNode . props ) . length ? ariaNode . children [ 0 ] : undefined ;
624631 } ;
625632
626- const visit = ( ariaNode : aria . AriaNode , indent : string , renderCursorPointer : boolean ) => {
633+ const visit = ( ariaNode : aria . AriaNode , depth : number , renderCursorPointer : boolean ) => {
634+ if ( publicOptions . depth && depth > publicOptions . depth )
635+ return ;
636+
627637 // Replace the whole subtree with a single reference when possible.
628638 if ( statusMap . get ( ariaNode ) === 'same' && ariaNode . ref ) {
629- lines . push ( indent + `- ref=${ ariaNode . ref } [unchanged]` ) ;
639+ lines . push ( indent ( depth ) + `- ref=${ ariaNode . ref } [unchanged]` ) ;
630640 return ;
631641 }
632642
633643 // When producing a diff, add <changed> marker to all diff roots.
634- const isDiffRoot = ! ! previousSnapshot && ! indent ;
635- const escapedKey = indent + '- ' + ( isDiffRoot ? '<changed> ' : '' ) + yamlEscapeKeyIfNeeded ( createKey ( ariaNode , renderCursorPointer ) ) ;
644+ const isDiffRoot = ! ! previousSnapshot && ! depth ;
645+ const escapedKey = indent ( depth ) + '- ' + ( isDiffRoot ? '<changed> ' : '' ) + yamlEscapeKeyIfNeeded ( createKey ( ariaNode , renderCursorPointer ) ) ;
636646 const singleInlinedTextChild = getSingleInlinedTextChild ( ariaNode ) ;
647+ const isAtDepthLimit = ! ! publicOptions . depth && depth === publicOptions . depth ;
648+ const hasNoChildren = ! singleInlinedTextChild && ( ! ariaNode . children . length || isAtDepthLimit ) ;
637649
638- if ( ! ariaNode . children . length && ! Object . keys ( ariaNode . props ) . length ) {
650+ if ( hasNoChildren && ! Object . keys ( ariaNode . props ) . length ) {
639651 // Leaf node without children.
640652 lines . push ( escapedKey ) ;
641653 } else if ( singleInlinedTextChild !== undefined ) {
@@ -649,24 +661,23 @@ export function renderAriaTree(ariaSnapshot: AriaSnapshot, publicOptions: AriaTr
649661 // Node with (optional) props and some children.
650662 lines . push ( escapedKey + ':' ) ;
651663 for ( const [ name , value ] of Object . entries ( ariaNode . props ) )
652- lines . push ( indent + ' - /' + name + ': ' + yamlEscapeValueIfNeeded ( value ) ) ;
664+ lines . push ( indent ( depth + 1 ) + ' - /' + name + ': ' + yamlEscapeValueIfNeeded ( value ) ) ;
653665
654- const childIndent = indent + ' ' ;
655666 const inCursorPointer = ! ! ariaNode . ref && renderCursorPointer && aria . hasPointerCursor ( ariaNode ) ;
656667 for ( const child of ariaNode . children ) {
657668 if ( typeof child === 'string' )
658- visitText ( includeText ( ariaNode , child ) ? child : '' , childIndent ) ;
669+ visitText ( includeText ( ariaNode , child ) ? child : '' , depth + 1 ) ;
659670 else
660- visit ( child , childIndent , renderCursorPointer && ! inCursorPointer ) ;
671+ visit ( child , depth + 1 , renderCursorPointer && ! inCursorPointer ) ;
661672 }
662673 }
663674 } ;
664675
665676 for ( const nodeToRender of nodesToRender ) {
666677 if ( typeof nodeToRender === 'string' )
667- visitText ( nodeToRender , '' ) ;
678+ visitText ( nodeToRender , 0 ) ;
668679 else
669- visit ( nodeToRender , '' , ! ! options . renderCursorPointer ) ;
680+ visit ( nodeToRender , 0 , ! ! options . renderCursorPointer ) ;
670681 }
671682 return lines . join ( '\n' ) ;
672683}
0 commit comments