From 87154ab87db079f7edfa8272f986c5181e4b5631 Mon Sep 17 00:00:00 2001 From: zieladam Date: Fri, 21 Apr 2023 13:30:11 +0000 Subject: [PATCH] HTML API: Fix a case where updates are overlooked when seeking to earlier locations. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This retains the WP_HTML_Tag_Processor attribute updates applied before calling seek() – they were erroneously erased in some cases. Props dmsnell. Fixes #58160. git-svn-id: https://develop.svn.wordpress.org/trunk@55675 602fd350-edb4-49c9-b593-d223f7449a82 --- .../html-api/class-wp-html-tag-processor.php | 2 ++ .../tests/html-api/wpHtmlTagProcessor.php | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+) 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 039a8f275b..8c41e73215 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 @@ -2142,6 +2142,8 @@ class WP_HTML_Tag_Processor { * to the end of the updated document and return. */ if ( $requires_no_updating && $this->bytes_already_copied > 0 ) { + $this->html = $this->output_buffer . substr( $this->html, $this->bytes_already_copied ); + $this->bytes_already_copied = strlen( $this->output_buffer ); return $this->output_buffer . substr( $this->html, $this->bytes_already_copied ); } diff --git a/tests/phpunit/tests/html-api/wpHtmlTagProcessor.php b/tests/phpunit/tests/html-api/wpHtmlTagProcessor.php index 86d6153957..15123748ae 100644 --- a/tests/phpunit/tests/html-api/wpHtmlTagProcessor.php +++ b/tests/phpunit/tests/html-api/wpHtmlTagProcessor.php @@ -456,6 +456,26 @@ class Tests_HtmlApi_wpHtmlTagProcessor extends WP_UnitTestCase { ); } + /** + * Ensures that when seeking to an earlier spot in the document that + * all previously-enqueued updates are applied as they ought to be. + * + * @ticket 58160 + */ + public function test_get_updated_html_applies_updates_to_content_after_seeking_to_before_parsed_bytes() { + $p = new WP_HTML_Tag_Processor( '
' ); + + $p->next_tag(); + $p->set_attribute( 'wonky', true ); + $p->next_tag(); + $p->set_bookmark( 'here' ); + + $p->next_tag( array( 'tag_closers' => 'visit' ) ); + $p->seek( 'here' ); + + $this->assertSame( '
', $p->get_updated_html() ); + } + /** * @ticket 56299 *