Skip to content

Commit

Permalink
HTML API: Add tests for virtual node breadcrumbs and depth
Browse files Browse the repository at this point in the history
Follow-up [58590].
See #61348.
Props jonsurrell, gziolo.



git-svn-id: https://develop.svn.wordpress.org/trunk@58592 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
gziolo committed Jun 28, 2024
1 parent 44e2325 commit 5effffc
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions tests/phpunit/tests/html-api/wpHtmlProcessorBreadcrumbs.php
Original file line number Diff line number Diff line change
Expand Up @@ -573,4 +573,68 @@ public function test_retains_proper_bookmarks_after_seeking_back_to_closed_eleme
'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' ) ),
);
}
}

0 comments on commit 5effffc

Please sign in to comment.