Make WordPress Core

Changeset 59250


Ignore:
Timestamp:
10/18/2024 01:29:04 PM (22 months ago)
Author:
gziolo
Message:

HTML API: Fix the position update after changing the modifiable text when length differs

There was an edge case detected for updating the text placed without the wrapping HTML tag with set_modifiable_text.

Props gziolo, jonsurrell.
Fixes #62241.

Location:
trunk
Files:
2 edited

Legend:

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

    r59076 r59250  
    24602460
    24612461                        // Accumulate shift of the given pointer within this function call.
    2462                         if ( $diff->start <= $shift_this_point ) {
     2462                        if ( $diff->start < $shift_this_point ) {
    24632463                                $accumulated_shift_for_given_point += $shift;
    24642464                        }
  • trunk/tests/phpunit/tests/html-api/wpHtmlTagProcessorModifiableText.php

    r58866 r59250  
    4141
    4242        /**
     43         * Data provider.
     44         *
     45         * @return array[]
     46         */
     47        public static function data_get_modifiable_text_replacements() {
     48                return array(
     49                        'shorter'     => array( 'just some text', 'shorter text' ),
     50                        'same length' => array( 'just some text', 'different text' ),
     51                        'longer'      => array( 'just some text', 'a bit longer text' ),
     52                );
     53        }
     54
     55        /**
    4356         * Ensures that `get_modifiable_text()` reads enqueued updates when read
    4457         * from after writing; guarantees consistency through writes.
    4558         *
    4659         * @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 );
     60         * @ticket 62241
     61         *
     62         * @dataProvider data_get_modifiable_text_replacements
     63         *
     64         * @param string $initial     Initial text.
     65         * @param string $replacement Replacement text.
     66         */
     67        public function test_get_modifiable_text_is_consistent_after_writes( $initial, $replacement ) {
     68                $processor = new WP_HTML_Tag_Processor( $initial );
    5269                $processor->next_token();
    5370
     
    5976
    6077                $this->assertSame(
    61                         $before,
     78                        $initial,
    6279                        $processor->get_modifiable_text(),
    6380                        'Should have found initial test text: check test setup.'
    6481                );
    6582
    66                 $processor->set_modifiable_text( $after );
    67                 $this->assertSame(
    68                         $after,
     83                $processor->set_modifiable_text( $replacement );
     84                $this->assertSame(
     85                        $replacement,
    6986                        $processor->get_modifiable_text(),
    7087                        'Should have found enqueued updated text.'
    7188                );
    72 
    73                 $processor->get_updated_html();
    74                 $this->assertSame(
    75                         $after,
     89                $this->assertSame(
     90                        $replacement,
     91                        $processor->get_updated_html(),
     92                        'Should match updated HTML.'
     93                );
     94                $this->assertSame(
     95                        $replacement,
     96                        $processor->get_modifiable_text(),
     97                        'Should have found updated text.'
     98                );
     99        }
     100
     101        /**
     102         * Ensures that `get_modifiable_text()` reads enqueued updates when read from
     103         * after writing; guarantees consistency through writes after closed tag element.
     104         *
     105         * @ticket 62241
     106         *
     107         * @dataProvider data_get_modifiable_text_replacements
     108         *
     109         * @param string $initial     Initial text.
     110         * @param string $replacement Replacement text.
     111         */
     112        public function test_get_modifiable_text_is_consistent_after_writes_when_text_after_closed_tag_element( $initial, $replacement ) {
     113                $html_before = '<p>some content</p>';
     114                $processor   = new WP_HTML_Tag_Processor( $html_before . $initial );
     115                // Move to the text node after the closing p tag.
     116                $processor->next_token();
     117                $processor->next_token();
     118                $processor->next_token();
     119                $processor->next_token();
     120
     121                $this->assertSame(
     122                        '#text',
     123                        $processor->get_token_name(),
     124                        "Should have found text node but found '{$processor->get_token_name()}' instead: check test setup."
     125                );
     126
     127                $this->assertSame(
     128                        $initial,
     129                        $processor->get_modifiable_text(),
     130                        'Should have found initial test text: check test setup.'
     131                );
     132
     133                $processor->set_modifiable_text( $replacement );
     134                $this->assertSame(
     135                        $replacement,
     136                        $processor->get_modifiable_text(),
     137                        'Should have found enqueued updated text.'
     138                );
     139
     140                $this->assertSame(
     141                        $html_before . $replacement,
     142                        $processor->get_updated_html(),
     143                        'Should match updated HTML.'
     144                );
     145
     146                $this->assertSame(
     147                        $replacement,
    76148                        $processor->get_modifiable_text(),
    77149                        'Should have found updated text.'
Note: See TracChangeset for help on using the changeset viewer.