Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/wp-includes/html-api/class-wp-html-tag-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2335,6 +2335,7 @@ public function seek( $bookmark_name ) {

// Point this tag processor before the sought tag opener and consume it.
$this->bytes_already_parsed = $this->bookmarks[ $bookmark_name ]->start;
$this->parser_state = self::STATE_READY;
return $this->next_token();
}

Expand Down
45 changes: 45 additions & 0 deletions tests/phpunit/tests/html-api/wpHtmlTagProcessor-bookmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,4 +435,49 @@ public function test_limits_the_number_of_seek_calls() {
$this->setExpectedIncorrectUsage( 'WP_HTML_Tag_Processor::seek' );
$this->assertFalse( $processor->seek( 'bookmark' ), "$i-th seek() to the bookmark succeeded, even though it should exceed the allowed limit" );
}

/**
* Ensures that it's possible to seek to an earlier location in a document even
* after reaching the end of a document, when most functionality shuts down.
*
* @ticket 60428
*
* @dataProvider data_incomplete_html_with_target_nodes_for_seeking
*
* @param string $html_with_target_element HTML string containing a tag with a `target` attribute.
*/
public function test_can_seek_after_document_ends( $html_with_target_element ) {
$processor = new WP_HTML_Tag_Processor( $html_with_target_element );

$sought_tag_name = null;
while ( $processor->next_tag() ) {
if ( null !== $processor->get_attribute( 'target' ) ) {
$processor->set_bookmark( 'target' );
$sought_tag_name = $processor->get_tag();
}
}

$this->assertTrue(
$processor->seek( 'target' ),
'Should have been able to seek to the target bookmark after reaching the end of the document.'
);

$this->assertSame(
$sought_tag_name,
$processor->get_tag(),
"Should have found original target node instead of {$processor->get_tag()}."
);
}

/**
* Data provider.
*
* @return array[].
*/
public static function data_incomplete_html_with_target_nodes_for_seeking() {
return array(
'Compete document' => array( '<div><img target></div>' ),
'Incomplete document' => array( '<div><img target></div' ),
);
}
}