Make WordPress Core


Ignore:
Timestamp:
03/14/2024 03:10:55 PM (3 years ago)
Author:
swissspidy
Message:

Interactivity API: Do not propagate context from void tags to its siblings.

Resolves an issue where context on a void tag element such as <img> was incorrectly passed to following elements.
Adds tests.

Reviewed by gziolo.
Merges [57832] to the to the 6.5 branch.

Props santosguillamot, luisherranz, cbravobernal, dmsnell, gziolo, swissspidy.
Fixes #60768.

Location:
branches/6.5
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/6.5

  • branches/6.5/tests/phpunit/tests/interactivity-api/interactivity-api.php

    r57830 r57834  
    424424                $this->assertEquals( 'data-wp-context=\'{"amp":"T\u0026T"}\'', wp_interactivity_data_wp_context( array( 'amp' => 'T&T' ) ) );
    425425        }
     426
     427        /**
     428         * Tests that context from void tags is not propagated to next tags.
     429         *
     430         * @ticket 60768
     431         *
     432         * @covers wp_interactivity_process_directives_of_interactive_blocks
     433         */
     434        public function test_process_context_directive_in_void_tags() {
     435                register_block_type(
     436                        'test/custom-directive-block',
     437                        array(
     438                                'render_callback' => function () {
     439                                        return '<div data-wp-interactive="nameSpace" data-wp-context=\'{"text": "outer"}\'><input id="first-input" data-wp-context=\'{"text": "inner"}\' data-wp-bind--value="context.text" /><input id="second-input" data-wp-bind--value="context.text" /></div>';
     440                                },
     441                                'supports'        => array(
     442                                        'interactivity' => true,
     443                                ),
     444                        )
     445                );
     446                $post_content      = '<!-- wp:test/custom-directive-block /-->';
     447                $processed_content = do_blocks( $post_content );
     448                $processor         = new WP_HTML_Tag_Processor( $processed_content );
     449                $processor->next_tag(
     450                        array(
     451                                'tag_name' => 'input',
     452                                'id'       => 'first-input',
     453                        )
     454                );
     455                $first_input_value = $processor->get_attribute( 'value' );
     456                $processor->next_tag(
     457                        array(
     458                                'tag_name' => 'input',
     459                                'id'       => 'second-input',
     460                        )
     461                );
     462                $second_input_value = $processor->get_attribute( 'value' );
     463                unregister_block_type( 'test/custom-directive-block' );
     464                $this->assertEquals( 'inner', $first_input_value );
     465                $this->assertEquals( 'outer', $second_input_value );
     466        }
     467
     468        /**
     469         * Tests that namespace from void tags is not propagated to next tags.
     470         *
     471         * @ticket 60768
     472         *
     473         * @covers wp_interactivity_process_directives_of_interactive_blocks
     474         */
     475        public function test_process_interactive_directive_in_void_tags() {
     476                wp_interactivity_state(
     477                        'void',
     478                        array(
     479                                'text' => 'void',
     480                        )
     481                );
     482                register_block_type(
     483                        'test/custom-directive-block',
     484                        array(
     485                                'render_callback' => function () {
     486                                        return '<div data-wp-interactive="parent"><img data-wp-interactive="void" /><input data-wp-bind--value="state.text" /></div>';
     487                                },
     488                                'supports'        => array(
     489                                        'interactivity' => true,
     490                                ),
     491                        )
     492                );
     493                $post_content      = '<!-- wp:test/custom-directive-block /-->';
     494                $processed_content = do_blocks( $post_content );
     495                $processor         = new WP_HTML_Tag_Processor( $processed_content );
     496                $processor->next_tag( array( 'tag_name' => 'input' ) );
     497                $input_value = $processor->get_attribute( 'value' );
     498                unregister_block_type( 'test/custom-directive-block' );
     499                $this->assertNull( $input_value );
     500        }
    426501}
Note: See TracChangeset for help on using the changeset viewer.