From 83ae6b790e5f1d361b5b128063f74bf253b55397 Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Wed, 22 Feb 2023 20:53:41 +0000 Subject: [PATCH] HTML API: Fix finding bookmarks set on closing tag WP_HTML_Tag_Processor. Setting a bookmark on a tag should set its "start" position before the opening "<", e.g.: {{{
Testing a Bookmark ----------------^ }}} The previous calculation assumed this is always one byte to the left from `$tag_name_starts_at`. However, in a closing tag that index points to a solidus symbol "/": {{{
Testing a Bookmark ----------------------------^ }}} The bookmark should therefore start two bytes before the tag name: {{{
Testing a Bookmark ---------------------------^ }}} This changeset achieves this by: * Using the correct starting index for closing tag bookmarks. * Adding `array( 'tag_closers' => 'visit' )` in `WP_HTML_Tag_Processor::seek()`. Follow-up to [55203]. Props zieladam, dmsnell, flixos90. Fixes #57787. See #57575. git-svn-id: https://develop.svn.wordpress.org/trunk@55407 602fd350-edb4-49c9-b593-d223f7449a82 --- .../html-api/class-wp-html-tag-processor.php | 4 ++-- .../html-api/wpHtmlTagProcessor-bookmark.php | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/html-api/class-wp-html-tag-processor.php b/src/wp-includes/html-api/class-wp-html-tag-processor.php index 42675ecd3c..044d1f0c36 100644 --- a/src/wp-includes/html-api/class-wp-html-tag-processor.php +++ b/src/wp-includes/html-api/class-wp-html-tag-processor.php @@ -722,7 +722,7 @@ class WP_HTML_Tag_Processor { } $this->bookmarks[ $name ] = new WP_HTML_Span( - $this->tag_name_starts_at - 1, + $this->tag_name_starts_at - ( $this->is_closing_tag ? 2 : 1 ), $this->tag_ends_at ); @@ -1504,7 +1504,7 @@ class WP_HTML_Tag_Processor { $this->bytes_already_parsed = $this->bookmarks[ $bookmark_name ]->start; $this->bytes_already_copied = $this->bytes_already_parsed; $this->output_buffer = substr( $this->html, 0, $this->bytes_already_copied ); - return $this->next_tag(); + return $this->next_tag( array( 'tag_closers' => 'visit' ) ); } /** diff --git a/tests/phpunit/tests/html-api/wpHtmlTagProcessor-bookmark.php b/tests/phpunit/tests/html-api/wpHtmlTagProcessor-bookmark.php index 04a6ae590c..eb6368c97d 100644 --- a/tests/phpunit/tests/html-api/wpHtmlTagProcessor-bookmark.php +++ b/tests/phpunit/tests/html-api/wpHtmlTagProcessor-bookmark.php @@ -63,6 +63,28 @@ class Tests_HtmlApi_wpHtmlTagProcessor_Bookmark extends WP_UnitTestCase { ); } + /** + * @ticket 57787 + * + * @covers WP_HTML_Tag_Processor::seek + */ + public function test_seeks_to_tag_closer_bookmark() { + $p = new WP_HTML_Tag_Processor( '
First
Second' ); + $p->next_tag( array( 'tag_closers' => 'visit' ) ); + $p->set_bookmark( 'first' ); + $p->next_tag( array( 'tag_closers' => 'visit' ) ); + $p->set_bookmark( 'second' ); + + $p->seek( 'first' ); + $p->seek( 'second' ); + + $this->assertSame( + 'DIV', + $p->get_tag(), + 'Did not seek to the intended bookmark location' + ); + } + /** * WP_HTML_Tag_Processor used to test for the diffs affecting * the adjusted bookmark position while simultaneously adjusting