From 70af002cdb2fcd2913452bc55a4be37a9ba0a88d Mon Sep 17 00:00:00 2001 From: gziolo Date: Tue, 10 Dec 2024 09:13:20 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@59502 git-svn-id: http://core.svn.wordpress.org/trunk@58888 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../html-api/class-wp-html-processor.php | 17 ++++++++++++++++- wp-includes/version.php | 2 +- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/wp-includes/html-api/class-wp-html-processor.php b/wp-includes/html-api/class-wp-html-processor.php index 7b156d8a60..8d2a476769 100644 --- a/wp-includes/html-api/class-wp-html-processor.php +++ b/wp-includes/html-api/class-wp-html-processor.php @@ -303,7 +303,9 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor { } while ( $context_processor->next_tag() ) { - $context_processor->set_bookmark( 'final_node' ); + if ( ! $context_processor->is_virtual() ) { + $context_processor->set_bookmark( 'final_node' ); + } } if ( @@ -5673,12 +5675,25 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor { * reaching for it, as inappropriate use could lead to broken * HTML structure or unwanted processing overhead. * + * Bookmarks cannot be set on tokens that do no appear in the original + * HTML text. For example, the HTML `
` stops at tags `TABLE`, + * `TBODY`, `TR`, and `TD`. The `TBODY` and `TR` tags do not appear in + * the original HTML and cannot be used as bookmarks. + * * @since 6.4.0 * * @param string $bookmark_name Identifies this particular bookmark. * @return bool Whether the bookmark was successfully created. */ public function set_bookmark( $bookmark_name ): bool { + if ( $this->is_virtual() ) { + _doing_it_wrong( + __METHOD__, + __( 'Cannot set bookmarks on tokens that do no appear in the original HTML text.' ), + '6.8.0' + ); + return false; + } return parent::set_bookmark( "_{$bookmark_name}" ); } diff --git a/wp-includes/version.php b/wp-includes/version.php index a040087f3b..c7e61f20a4 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.8-alpha-59501'; +$wp_version = '6.8-alpha-59502'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.