HTML API: Add tests for virtual node breadcrumbs and depth

Follow-up [58590].
See #61348.
Props jonsurrell, gziolo.



git-svn-id: https://develop.svn.wordpress.org/trunk@58592 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Greg Ziółkowski 2024-06-28 09:20:10 +00:00
parent 44e23253f8
commit 5effffccd0

View File

@ -573,4 +573,68 @@ HTML
'Should have retained breadcrumbs from bookmarked location after seeking backwards to it.'
);
}
/**
* Ensures that breadcrumbs are properly reported on virtual nodes.
*
* @ticket 61348
*
* @dataProvider data_virtual_nodes_breadcrumbs
*
* @covers WP_HTML_Processor::get_breadcrumbs
*/
public function test_breadcrumbs_on_virtual_nodes( string $html, int $token_position, string $expected_tag_name, string $expect_open_close, array $expected_breadcrumbs ) {
$processor = WP_HTML_Processor::create_fragment( $html );
for ( $i = 0; $i < $token_position; $i++ ) {
$processor->next_token();
}
$this->assertSame( $expected_tag_name, $processor->get_tag(), "Found incorrect tag name {$processor->get_tag()}." );
if ( 'open' === $expect_open_close ) {
$this->assertFalse( $processor->is_tag_closer(), "Found closer when opener expected at {$processor->get_tag()}." );
} else {
$this->assertTrue( $processor->is_tag_closer(), "Found opener when closer expected at {$processor->get_tag()}." );
}
$this->assertEquals( $expected_breadcrumbs, $processor->get_breadcrumbs(), "Found incorrect breadcrumbs in {$html}." );
}
/**
* Ensures that get_current_depth reports the correct depth on virtual nodes.
*
* @ticket 61348
*
* @dataProvider data_virtual_nodes_breadcrumbs
*
* @covers WP_HTML_Processor::get_current_depth
*/
public function test_depth_on_virtual_nodes( string $html, int $token_position, string $expected_tag_name, string $expect_open_close, array $expected_breadcrumbs ) {
$processor = WP_HTML_Processor::create_fragment( $html );
for ( $i = 0; $i < $token_position; $i++ ) {
$processor->next_token();
}
$this->assertSame( count( $expected_breadcrumbs ), $processor->get_current_depth(), "Found incorrect depth in {$html}." );
}
/**
* Data provider for virtual nodes breadcrumbs with the following shape of arrays:
* 0: string Input html.
* 1: int Token index to seek.
* 2: string Expected tag name.
* 3: string 'open' or 'close' indicating an opener or closer is expected.
* 4: array<string> Expected breadcrumbs.
*
* @return array[]
*/
public static function data_virtual_nodes_breadcrumbs() {
return array(
'Implied P tag opener on unmatched closer' => array( '</p>', 1, 'P', 'open', array( 'HTML', 'BODY', 'P' ) ),
'Implied heading tag closer on heading child' => array( '<h1><h2>', 2, 'H1', 'close', array( 'HTML', 'BODY' ) ),
'Implied A tag closer on A tag child' => array( '<a><a>', 2, 'A', 'close', array( 'HTML', 'BODY' ) ),
'Implied A tag closer on A tag descendent' => array( '<a><span><a>', 4, 'A', 'close', array( 'HTML', 'BODY' ) ),
);
}
}