Make WordPress Core

Changeset 58866


Ignore:
Timestamp:
08/08/2024 04:24:03 AM (10 months ago)
Author:
dmsnell
Message:

HTML API: Ensure that get_modifiable_text() reads enqueued updates.

When set_modifiable_text() was added to the Tag Processor, it was considered that the same information could be queried after setting its value and before proceeding to the next token, but unfortunately overlooked that if the starting modifiable text length was zero, then the read in get_modifiable_text() would ignore enqueued updates.

In this patch, get_modifiable_text() will read any enqueued values before reading from the input HTML document to ensure consistency.

Follow-up to [58829].
Props dmsnell, jonsurrell, ramonopoly.
Fixes #61617.

Location:
trunk
Files:
2 edited

Legend:

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

    r58858 r58866  
    615615     * @since 6.5.0
    616616     *
    617      * @var string
     617     * @var int
    618618     */
    619619    private $text_length;
     
    28952895     */
    28962896    public function get_modifiable_text(): string {
    2897         if ( null === $this->text_starts_at || 0 === $this->text_length ) {
     2897        $has_enqueued_update = isset( $this->lexical_updates['modifiable text'] );
     2898
     2899        if ( ! $has_enqueued_update && ( null === $this->text_starts_at || 0 === $this->text_length ) ) {
    28982900            return '';
    28992901        }
    29002902
    2901         $text = isset( $this->lexical_updates['modifiable text'] )
     2903        $text = $has_enqueued_update
    29022904            ? $this->lexical_updates['modifiable text']->text
    29032905            : substr( $this->html, $this->text_starts_at, $this->text_length );
  • trunk/tests/phpunit/tests/html-api/wpHtmlTagProcessorModifiableText.php

    r58829 r58866  
    3838            );
    3939        }
     40    }
     41
     42    /**
     43     * Ensures that `get_modifiable_text()` reads enqueued updates when read
     44     * from after writing; guarantees consistency through writes.
     45     *
     46     * @ticket 61617
     47     */
     48    public function test_get_modifiable_text_is_consistent_after_writes() {
     49        $before    = 'just some text';
     50        $after     = 'different text';
     51        $processor = new WP_HTML_Tag_Processor( $before );
     52        $processor->next_token();
     53
     54        $this->assertSame(
     55            '#text',
     56            $processor->get_token_name(),
     57            "Should have found text node but found '{$processor->get_token_name()}' instead: check test setup."
     58        );
     59
     60        $this->assertSame(
     61            $before,
     62            $processor->get_modifiable_text(),
     63            'Should have found initial test text: check test setup.'
     64        );
     65
     66        $processor->set_modifiable_text( $after );
     67        $this->assertSame(
     68            $after,
     69            $processor->get_modifiable_text(),
     70            'Should have found enqueued updated text.'
     71        );
     72
     73        $processor->get_updated_html();
     74        $this->assertSame(
     75            $after,
     76            $processor->get_modifiable_text(),
     77            'Should have found updated text.'
     78        );
     79    }
     80
     81    /**
     82     * Ensures that `get_modifiable_text()` reads enqueued updates when read from after
     83     * writing when starting from an empty text; guarantees consistency through writes.
     84     *
     85     * @ticket 61617
     86     */
     87    public function test_get_modifiable_text_is_consistent_after_writes_to_empty_text() {
     88        $after     = 'different text';
     89        $processor = new WP_HTML_Tag_Processor( '<script></script>' );
     90        $processor->next_token();
     91
     92        $this->assertSame(
     93            'SCRIPT',
     94            $processor->get_token_name(),
     95            "Should have found text node but found '{$processor->get_token_name()}' instead: check test setup."
     96        );
     97
     98        $this->assertSame(
     99            '',
     100            $processor->get_modifiable_text(),
     101            'Should have found initial test text: check test setup.'
     102        );
     103
     104        $processor->set_modifiable_text( $after );
     105        $this->assertSame(
     106            $after,
     107            $processor->get_modifiable_text(),
     108            'Should have found enqueued updated text.'
     109        );
     110
     111        $processor->get_updated_html();
     112        $this->assertSame(
     113            $after,
     114            $processor->get_modifiable_text(),
     115            'Should have found updated text.'
     116        );
    40117    }
    41118
Note: See TracChangeset for help on using the changeset viewer.