Skip to content

Commit 7ceb839

Browse files
committed
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 #7164 Discussed in https://core.trac.wordpress.org/ticket/61576 Follow-up to [58868]. Props: dmsnell, jonsurrell. See #61576. git-svn-id: https://develop.svn.wordpress.org/trunk@58871 602fd350-edb4-49c9-b593-d223f7449a82
1 parent b9014d6 commit 7ceb839

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

src/wp-includes/html-api/class-wp-html-processor.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4239,21 +4239,22 @@ private function step_in_foreign_content(): bool {
42394239
/*
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;
42594260
}
@@ -4263,13 +4264,13 @@ private function step_in_foreign_content(): bool {
42634264
*/
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

42684270
/*
42694271
* > Any other end tag
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 ) {
42754276
// @todo Indicate a parse error once it's possible.

tests/phpunit/tests/html-api/wpHtmlProcessor.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,4 +521,14 @@ public function test_expects_closer_foreign_content_self_closing() {
521521
$this->assertSame( 'MATH', $processor->get_tag() );
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
}

0 commit comments

Comments
 (0)