Skip to content

Commit f80516d

Browse files
committed
HTML API: Reset parser state after seeking to bookmark.
When parser states were introduced, nothing in the `seek()` method reset the parser state. This is problematic because it could leave the parser in the wrong state. In this patch the parser state is reset so that it's properly adjusted on the successive call to `next_token()`. Developed in #6021 Discussed in https://core.trac.wordpress.org/ticket/60428 Follow-up to [57211] Props dmsnell, kevin940726 Fixes #60428 git-svn-id: https://develop.svn.wordpress.org/trunk@57527 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 235cb39 commit f80516d

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/wp-includes/html-api/class-wp-html-tag-processor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2335,6 +2335,7 @@ public function seek( $bookmark_name ) {
23352335

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

tests/phpunit/tests/html-api/wpHtmlTagProcessor-bookmark.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,4 +435,49 @@ public function test_limits_the_number_of_seek_calls() {
435435
$this->setExpectedIncorrectUsage( 'WP_HTML_Tag_Processor::seek' );
436436
$this->assertFalse( $processor->seek( 'bookmark' ), "$i-th seek() to the bookmark succeeded, even though it should exceed the allowed limit" );
437437
}
438+
439+
/**
440+
* Ensures that it's possible to seek to an earlier location in a document even
441+
* after reaching the end of a document, when most functionality shuts down.
442+
*
443+
* @ticket 60428
444+
*
445+
* @dataProvider data_incomplete_html_with_target_nodes_for_seeking
446+
*
447+
* @param string $html_with_target_element HTML string containing a tag with a `target` attribute.
448+
*/
449+
public function test_can_seek_after_document_ends( $html_with_target_element ) {
450+
$processor = new WP_HTML_Tag_Processor( $html_with_target_element );
451+
452+
$sought_tag_name = null;
453+
while ( $processor->next_tag() ) {
454+
if ( null !== $processor->get_attribute( 'target' ) ) {
455+
$processor->set_bookmark( 'target' );
456+
$sought_tag_name = $processor->get_tag();
457+
}
458+
}
459+
460+
$this->assertTrue(
461+
$processor->seek( 'target' ),
462+
'Should have been able to seek to the target bookmark after reaching the end of the document.'
463+
);
464+
465+
$this->assertSame(
466+
$sought_tag_name,
467+
$processor->get_tag(),
468+
"Should have found original target node instead of {$processor->get_tag()}."
469+
);
470+
}
471+
472+
/**
473+
* Data provider.
474+
*
475+
* @return array[].
476+
*/
477+
public static function data_incomplete_html_with_target_nodes_for_seeking() {
478+
return array(
479+
'Compete document' => array( '<div><img target></div>' ),
480+
'Incomplete document' => array( '<div><img target></div' ),
481+
);
482+
}
438483
}

0 commit comments

Comments
 (0)