Make WordPress Core

Changeset 58871


Ignore:
Timestamp:
08/08/2024 05:02:46 PM (2 years ago)
Author:
dmsnell
Message:

HTML API: Test and fix SVG script handling.

When support was added for foreign content, an ambiguity in the HTML specification led to code that followed the wrong path when encountering a self-closing SCRIPT element in the SVG namespace. Further, a fallthrough was discovered during manual testing.

This patch adds a new test to assert the proper behaviors and fixes these issues. In the case of the SCRIPT element, the outcome was the same with the wrong code path, making the defect benign. In the case of the fallthrough, the wrong behavior would occur.

The updates in this patch also resolves a todo relating to the spec ambiguity.

Developed in ​https://github.com/wordpress/wordpress-develop/pull/7164
Discussed in https://core.trac.wordpress.org/ticket/61576

Follow-up to [58868].

Props: dmsnell, jonsurrell.
See #61576.

Location:
trunk
Files:
2 edited

Legend:

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

    r58870 r58871  
    42404240                         * > If the token has its self-closing flag set, then run
    42414241                         * > the appropriate steps from the following list:
     4242                         * >
     4243                         * >   ↪ the token's tag name is "script", and the new current node is in the SVG namespace
     4244                         * >         Acknowledge the token's self-closing flag, and then act as
     4245                         * >         described in the steps for a "script" end tag below.
     4246                         * >
     4247                         * >   ↪ Otherwise
     4248                         * >         Pop the current node off the stack of open elements and
     4249                         * >         acknowledge the token's self-closing flag.
     4250                         *
     4251                         * Since the rules for SCRIPT below indicate to pop the element off of the stack of
     4252                         * open elements, which is the same for the Otherwise condition, there's no need to
     4253                         * separate these checks. The difference comes when a parser operates with the scripting
     4254                         * flag enabled, and executes the script, which this parser does not support.
    42424255                         */
    42434256                        if ( $this->state->current_token->has_self_closing_flag ) {
    4244                                 if ( 'SCRIPT' === $this->state->current_token->node_name && 'svg' === $this->state->current_token->namespace ) {
    4245                                         /*
    4246                                          * > Acknowledge the token's self-closing flag, and then act as
    4247                                          * > described in the steps for a "script" end tag below.
    4248                                          *
    4249                                          * @todo Verify that this shouldn't be handled by the rule for
    4250                                          *       "An end tag whose name is 'script', if the current node
    4251                                          *       is an SVG script element."
    4252                                          */
    4253                                         goto in_foreign_content_any_other_end_tag;
    4254                                 } else {
    4255                                         $this->state->stack_of_open_elements->pop();
    4256                                 }
     4257                                $this->state->stack_of_open_elements->pop();
    42574258                        }
    42584259                        return true;
    … …  
    42644265                if ( $this->is_tag_closer() && 'SCRIPT' === $this->state->current_token->node_name && 'svg' === $this->state->current_token->namespace ) {
    42654266                        $this->state->stack_of_open_elements->pop();
     4267                        return true;
    42664268                }
    42674269
    … …  
    42704272                 */
    42714273                if ( $this->is_tag_closer() ) {
    4272                         in_foreign_content_any_other_end_tag:
    42734274                        $node = $this->state->stack_of_open_elements->current_node();
    42744275                        if ( $tag_name !== $node->node_name ) {
  • trunk/tests/phpunit/tests/html-api/wpHtmlProcessor.php

    r58870 r58871  
    522522                $this->assertTrue( $processor->expects_closer() );
    523523        }
     524
     525        /**
     526         * Ensures that self-closing foreign SCRIPT elements are properly found.
     527         *
     528         * @ticket 61576
     529         */
     530        public function test_foreign_content_script_self_closing() {
     531                $processor = WP_HTML_Processor::create_fragment( '<svg><script />' );
     532                $this->assertTrue( $processor->next_tag( 'script' ) );
     533        }
    524534}
Note: See TracChangeset for help on using the changeset viewer.