Skip to content

Commit 1467750

Browse files
committed
HTML API: Prevent bookmarks from being set on virtual tokens
Fixes the issue when an HTML_Processor bookmark was set at a virtual token (a node in the resulting document that does not correspond to an HTML token present in the input string), seek behavior was unreliable. Props jonsurrell, gziolo. Fixes #62521. git-svn-id: https://develop.svn.wordpress.org/trunk@59502 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 6106332 commit 1467750

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,9 @@ public static function create_fragment( $html, $context = '<body>', $encoding =
303303
}
304304

305305
while ( $context_processor->next_tag() ) {
306-
$context_processor->set_bookmark( 'final_node' );
306+
if ( ! $context_processor->is_virtual() ) {
307+
$context_processor->set_bookmark( 'final_node' );
308+
}
307309
}
308310

309311
if (
@@ -5673,12 +5675,25 @@ public function seek( $bookmark_name ): bool {
56735675
* reaching for it, as inappropriate use could lead to broken
56745676
* HTML structure or unwanted processing overhead.
56755677
*
5678+
* Bookmarks cannot be set on tokens that do no appear in the original
5679+
* HTML text. For example, the HTML `<table><td>` stops at tags `TABLE`,
5680+
* `TBODY`, `TR`, and `TD`. The `TBODY` and `TR` tags do not appear in
5681+
* the original HTML and cannot be used as bookmarks.
5682+
*
56765683
* @since 6.4.0
56775684
*
56785685
* @param string $bookmark_name Identifies this particular bookmark.
56795686
* @return bool Whether the bookmark was successfully created.
56805687
*/
56815688
public function set_bookmark( $bookmark_name ): bool {
5689+
if ( $this->is_virtual() ) {
5690+
_doing_it_wrong(
5691+
__METHOD__,
5692+
__( 'Cannot set bookmarks on tokens that do no appear in the original HTML text.' ),
5693+
'6.8.0'
5694+
);
5695+
return false;
5696+
}
56825697
return parent::set_bookmark( "_{$bookmark_name}" );
56835698
}
56845699

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,17 @@ public static function data_processor_constructors(): array {
157157
'Fragment parser' => array( array( WP_HTML_Processor::class, 'create_fragment' ) ),
158158
);
159159
}
160+
161+
/**
162+
* @ticket 62521
163+
*
164+
* @expectedIncorrectUsage WP_HTML_Processor::set_bookmark
165+
*/
166+
public function test_bookmarks_not_allowed_on_virtual_nodes() {
167+
$processor = WP_HTML_Processor::create_full_parser( 'text' );
168+
$this->assertTrue( $processor->next_tag( 'BODY' ) );
169+
$this->assertFalse( $processor->set_bookmark( 'mark' ) );
170+
$this->assertTrue( $processor->next_token() );
171+
$this->assertTrue( $processor->set_bookmark( 'mark' ) );
172+
}
160173
}

0 commit comments

Comments
 (0)