HTML API: Fix a case where updates are overlooked when seeking to earlier locations.

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
This commit is contained in:
zieladam 2023-04-21 13:30:11 +00:00
parent fea66becf5
commit 87154ab87d
2 changed files with 22 additions and 0 deletions

View File

@ -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 );
}

View File

@ -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( '<div><img hidden></div>' );
$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( '<div wonky><img hidden></div>', $p->get_updated_html() );
}
/**
* @ticket 56299
*