Skip to content

Commit

Permalink
Add and improve HTML processor bookmark tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sirreal committed Nov 6, 2024
1 parent d5bf14c commit 388bf19
Showing 1 changed file with 48 additions and 3 deletions.
51 changes: 48 additions & 3 deletions tests/phpunit/tests/html-api/wpHtmlProcessor-bookmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,27 @@
*/
class Tests_HtmlApi_WpHtmlProcessor_Bookmark extends WP_UnitTestCase {
/**
* Ensures that bookmarks can be set and seeked to for the full processor.
*
* @ticket 62290
*/
public function test_full_processor_seek() {
public function test_full_processor_seek_same_location() {
$bookmark_name = 'the-bookmark';
$processor = WP_HTML_Processor::create_full_parser( '<html><body><div>' );
$this->assertTrue( $processor->next_tag( 'BODY' ) );
$this->assertTrue( $processor->set_bookmark( $bookmark_name ), 'Failed to set bookmark.' );
$this->assertTrue( $processor->has_bookmark( $bookmark_name ), 'Failed has_bookmark check.' );

// Confirm the bookmark works.
$this->assertTrue( $processor->seek( $bookmark_name ), 'Failed to seek to bookmark.' );
$this->assertSame( 'BODY', $processor->get_tag() );
$this->assertTrue( $processor->next_tag() );
$this->assertSame( 'DIV', $processor->get_tag() );
$this->assertSame( array( 'HTML', 'BODY', 'DIV' ), $processor->get_breadcrumbs() );
}

/**
* @ticket 62290
*/
public function test_full_processor_seek_backward() {
$bookmark_name = 'the-bookmark';
$processor = WP_HTML_Processor::create_full_parser( '<html><body><div>' );
$this->assertTrue( $processor->next_tag( 'BODY' ) );
Expand All @@ -31,4 +47,33 @@ public function test_full_processor_seek() {
$this->assertTrue( $processor->seek( $bookmark_name ), 'Failed to seek to bookmark.' );
$this->assertSame( 'BODY', $processor->get_tag() );
}

/**
* @ticket 62290
*/
public function test_full_processor_seek_forward() {
$processor = WP_HTML_Processor::create_full_parser( '<html><body><div one></div><div two>' );
$this->assertTrue( $processor->next_tag( 'BODY' ) );
$this->assertTrue( $processor->set_bookmark( 'body' ), 'Failed to set bookmark "body".' );
$this->assertTrue( $processor->has_bookmark( 'body' ), 'Failed "body" has_bookmark check.' );

// Move past the bookmark so it must scan backwards.
$this->assertTrue( $processor->next_tag( 'DIV' ) );
$this->assertTrue( $processor->get_attribute( 'one' ) );
$this->assertTrue( $processor->set_bookmark( 'one' ), 'Failed to set bookmark "one".' );
$this->assertTrue( $processor->has_bookmark( 'one' ), 'Failed "one" has_bookmark check.' );

// Seek back.
$this->assertTrue( $processor->seek( 'body' ), 'Failed to seek to bookmark "body".' );
$this->assertSame( 'BODY', $processor->get_tag() );

// Seek forward and continue processing.
$this->assertTrue( $processor->seek( 'one' ), 'Failed to seek to bookmark "one".' );
$this->assertSame( 'DIV', $processor->get_tag() );
$this->assertTrue( $processor->get_attribute( 'one' ) );
$this->assertTrue( $processor->next_tag() );

$this->assertSame( 'DIV', $processor->get_tag() );
$this->assertTrue( $processor->get_attribute( 'two' ) );
}
}

0 comments on commit 388bf19

Please sign in to comment.