HTML; $doc = DOMCompat::newDocument( true ); $doc->loadHTML( $html ); DOMPostOrder::traverse( $doc->documentElement, static function ( Node $node ) use ( &$trace ) { if ( $node instanceof Element && $node->hasAttribute( 'id' ) ) { $trace[] = DOMCompat::getAttribute( $node, 'id' ); } } ); $this->assertSame( [ 'x1_1', 'x1_2', 'x1', 'x2_1', 'x2' ], $trace ); } }