Make WordPress Core

Changeset 57527


Ignore:
Timestamp:
02/02/2024 10:55:27 PM (14 months ago)
Author:
dmsnell
Message:

HTML API: Reset parser state after seeking to bookmark.

When parser states were introduced, nothing in the seek() method reset the
parser state. This is problematic because it could leave the parser in the
wrong state.

In this patch the parser state is reset so that it's properly adjusted on
the successive call to next_token().

Developed in https://github.com/WordPress/wordpress-develop/pull/6021
Discussed in https://core.trac.wordpress.org/ticket/60428

Follow-up to [57211]

Props dmsnell, kevin940726
Fixes #60428

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/html-api/class-wp-html-tag-processor.php

    r57506 r57527  
    23362336        // Point this tag processor before the sought tag opener and consume it.
    23372337        $this->bytes_already_parsed = $this->bookmarks[ $bookmark_name ]->start;
     2338        $this->parser_state         = self::STATE_READY;
    23382339        return $this->next_token();
    23392340    }
  • trunk/tests/phpunit/tests/html-api/wpHtmlTagProcessor-bookmark.php

    r57508 r57527  
    436436        $this->assertFalse( $processor->seek( 'bookmark' ), "$i-th seek() to the bookmark succeeded, even though it should exceed the allowed limit" );
    437437    }
     438
     439    /**
     440     * Ensures that it's possible to seek to an earlier location in a document even
     441     * after reaching the end of a document, when most functionality shuts down.
     442     *
     443     * @ticket 60428
     444     *
     445     * @dataProvider data_incomplete_html_with_target_nodes_for_seeking
     446     *
     447     * @param string $html_with_target_element HTML string containing a tag with a `target` attribute.
     448     */
     449    public function test_can_seek_after_document_ends( $html_with_target_element ) {
     450        $processor = new WP_HTML_Tag_Processor( $html_with_target_element );
     451
     452        $sought_tag_name = null;
     453        while ( $processor->next_tag() ) {
     454            if ( null !== $processor->get_attribute( 'target' ) ) {
     455                $processor->set_bookmark( 'target' );
     456                $sought_tag_name = $processor->get_tag();
     457            }
     458        }
     459
     460        $this->assertTrue(
     461            $processor->seek( 'target' ),
     462            'Should have been able to seek to the target bookmark after reaching the end of the document.'
     463        );
     464
     465        $this->assertSame(
     466            $sought_tag_name,
     467            $processor->get_tag(),
     468            "Should have found original target node instead of {$processor->get_tag()}."
     469        );
     470    }
     471
     472    /**
     473     * Data provider.
     474     *
     475     * @return array[].
     476     */
     477    public static function data_incomplete_html_with_target_nodes_for_seeking() {
     478        return array(
     479            'Compete document'    => array( '<div><img target></div>' ),
     480            'Incomplete document' => array( '<div><img target></div' ),
     481        );
     482    }
    438483}
Note: See TracChangeset for help on using the changeset viewer.