From fd1cbd8980d67df161fa5c3ae4c7bf6f5fb49cc1 Mon Sep 17 00:00:00 2001 From: dmsnell Date: Sat, 10 Feb 2024 22:54:13 +0000 Subject: [PATCH] HTML API: Add subclassed has_bookmark() and fix seek() The WP_HTML_Processor::has_bookmark() method has not correctly reported bookmarks which have been set, because it wraps the given bookmark names when setting them. Additionally, WP_HTML_Processor::seek() does not seek to correct location if HTML has been updated because it wasn't flushing enqueued updates to the document. In this patch both problems are resolved and added tests guard these behaviors against future regressions. Developed in https://github.com/WordPress/wordpress-develop/pull/6039 Discussed in https://core.trac.wordpress.org/ticket/60474 Follow-up to [56274]. Props dmsnell, jonsurrell. Fixes #60474. Built from https://develop.svn.wordpress.org/trunk@57582 git-svn-id: http://core.svn.wordpress.org/trunk@57083 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 dd8e8d5f2d..51b6a5679e 100644 --- a/wp-includes/html-api/class-wp-html-processor.php +++ b/wp-includes/html-api/class-wp-html-processor.php @@ -1237,6 +1237,9 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor { * @return bool Whether the internal cursor was successfully moved to the bookmark's location. */ public function seek( $bookmark_name ) { + // Flush any pending updates to the document before beginning. + $this->get_updated_html(); + $actual_bookmark_name = "_{$bookmark_name}"; $processor_started_at = $this->state->current_token ? $this->bookmarks[ $this->state->current_token->bookmark_name ]->start @@ -1246,7 +1249,7 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor { switch ( $direction ) { case 'forward': - // When moving forwards, re-parse the document until reaching the same location as the original bookmark. + // When moving forwards, reparse the document until reaching the same location as the original bookmark. while ( $this->step() ) { if ( $bookmark_starts_at === $this->bookmarks[ $this->state->current_token->bookmark_name ]->start ) { return true; @@ -1368,6 +1371,18 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor { return parent::set_bookmark( "_{$bookmark_name}" ); } + /** + * Checks whether a bookmark with the given name exists. + * + * @since 6.5.0 + * + * @param string $bookmark_name Name to identify a bookmark that potentially exists. + * @return bool Whether that bookmark exists. + */ + public function has_bookmark( $bookmark_name ) { + return parent::has_bookmark( "_{$bookmark_name}" ); + } + /* * HTML Parsing Algorithms */ diff --git a/wp-includes/version.php b/wp-includes/version.php index b8a484db34..2dbe2e1856 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.5-alpha-57581'; +$wp_version = '6.5-alpha-57582'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.