Make WordPress Core


Ignore:
Timestamp:
05/03/2023 12:09:22 PM (21 months ago)
Author:
Bernhard Reiter
Message:

HTML API: Accumulate shift for internal parsing pointer.

A bug was discovered where where the parser wasn't returning to the
start of the affected tag after making some updates.

In few words, the Tag Processor has not been treating its own internal
pointer bytes_already_parsed the same way it treats its bookmarks.
That is, when updates are applied to the input document and then
get_updated_html() is called, the internal pointer transfers to
the newly-updated content as if no updates had been applied since
the previous call to get_updated_html().

In this patch we're creating a new "shift accumulator" to account for
all of the updates that accrue before calling get_updated_html().
This accumulated shift will be applied when swapping the input document
with the output buffer, which should result in the pointer pointing to
the same logical spot in the document it did before the udpate.

In effect this patch adds a single workaround for treating the
internal pointer like a bookmark, plus a temporary pointer which points
to the beginning of the current tag when calling get_updated_html().
This will preserve the assumption that updating a document doesn't
move that pointer, or shift which tag is currently matched.

Props dmsnell, zieladam.
Merges [55706] to the 6.2 branch.
Fixes #58179.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/6.2/tests/phpunit/tests/html-api/wpHtmlTagProcessor.php

    r55707 r55708  
    520520        $p = new WP_HTML_Tag_Processor( 'abc</title>' );
    521521        $this->assertTrue( $p->next_tag( array( 'tag_closers' => 'visit' ) ), 'Did not find the </title> tag closer when there was no tag opener' );
     522    }
     523
     524    /**
     525     * Verifies that updates to a document before calls to `get_updated_html()` don't
     526     * lead to the Tag Processor jumping to the wrong tag after the updates.
     527     *
     528     * @ticket 58179
     529     *
     530     * @covers WP_HTML_Tag_Processor::get_updated_html
     531     */
     532    public function test_internal_pointer_returns_to_original_spot_after_inserting_content_before_cursor() {
     533        $tags = new WP_HTML_Tag_Processor( '<div>outside</div><section><div><img>inside</div></section>' );
     534
     535        $tags->next_tag();
     536        $tags->add_class( 'foo' );
     537        $tags->next_tag( 'section' );
     538
     539        // Return to this spot after moving ahead.
     540        $tags->set_bookmark( 'here' );
     541
     542        // Move ahead.
     543        $tags->next_tag( 'img' );
     544        $tags->seek( 'here' );
     545        $this->assertSame( '<div class="foo">outside</div><section><div><img>inside</div></section>', $tags->get_updated_html() );
     546        $this->assertSame( 'SECTION', $tags->get_tag() );
     547        $this->assertFalse( $tags->is_tag_closer() );
    522548    }
    523549
     
    14731499
    14741500        $p = new WP_HTML_Tag_Processor( $input );
    1475         $this->assertTrue( $p->next_tag( 'div' ), 'Querying an existing tag did not return true' );
     1501        $this->assertTrue( $p->next_tag( 'div' ), 'Did not find first DIV tag in input.' );
    14761502        $p->set_attribute( 'data-details', '{ "key": "value" }' );
    14771503        $p->add_class( 'is-processed' );
     
    14831509                )
    14841510            ),
    1485             'Querying an existing tag did not return true'
     1511            'Did not find the first BtnGroup DIV tag'
    14861512        );
    14871513        $p->remove_class( 'BtnGroup' );
     
    14951521                )
    14961522            ),
    1497             'Querying an existing tag did not return true'
     1523            'Did not find the second BtnGroup DIV tag'
    14981524        );
    14991525        $p->remove_class( 'BtnGroup' );
     
    15081534                )
    15091535            ),
    1510             'Querying an existing tag did not return true'
     1536            'Did not find third BUTTON tag with "btn" CSS class'
    15111537        );
    15121538        $p->remove_attribute( 'class' );
    1513         $this->assertFalse( $p->next_tag( 'non-existent' ), 'Querying a non-existing tag did not return false' );
     1539        $this->assertFalse( $p->next_tag( 'non-existent' ), "Found a {$p->get_tag()} tag when none should have been found." );
    15141540        $p->set_attribute( 'class', 'test' );
    15151541        $this->assertSame( $expected_output, $p->get_updated_html(), 'Calling get_updated_html after updating the attributes did not return the expected HTML' );
Note: See TracChangeset for help on using the changeset viewer.