Skip to content

Commit f149add

Browse files
committed
HTML API: Respect namespace in open element lookup.
Prevent foreign elements from incorrectly satisfying checks for open HTML elements. Developed in WordPress#12353. Props jonsurrell, dmsnell. See #65372. git-svn-id: https://develop.svn.wordpress.org/trunk@62575 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 23a6bbf commit f149add

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

‎src/wp-includes/html-api/class-wp-html-open-elements.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,16 @@ public function at( int $nth ): ?WP_HTML_Token {
128128
}
129129

130130
/**
131-
* Reports if a node of a given name is in the stack of open elements.
131+
* Reports if an HTML element of a given name is on the stack of open elements.
132132
*
133133
* @since 6.7.0
134134
*
135-
* @param string $node_name Name of node for which to check.
135+
* @param string $node_name Name of HTML element for which to check.
136136
* @return bool Whether a node of the given name is in the stack of open elements.
137137
*/
138138
public function contains( string $node_name ): bool {
139139
foreach ( $this->walk_up() as $item ) {
140-
if ( $node_name === $item->node_name ) {
140+
if ( 'html' === $item->namespace && $node_name === $item->node_name ) {
141141
return true;
142142
}
143143
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,26 @@ public function test_template_tag_closes_html_template_element() {
642642
$this->assertSame( array( 'HTML', 'BODY', 'DIV' ), $processor->get_breadcrumbs() );
643643
}
644644

645+
/**
646+
* Ensures foreign TEMPLATE elements do not satisfy HTML template handling.
647+
*
648+
* @ticket 65372
649+
*/
650+
public function test_unmatched_template_closer_after_mathml_template_is_ignored() {
651+
$processor = WP_HTML_Processor::create_fragment( '<math><template><mi><c></template>here' );
652+
653+
$this->assertTrue( $processor->next_tag( 'C' ), 'Failed to find C tag.' );
654+
$this->assertTrue( $processor->next_token(), 'Failed to advance past the C tag.' );
655+
656+
// Closing HTML </template> tag should be ignored, advancing to "here" text without modifying breadcrumbs.
657+
$this->assertSame( '#text', $processor->get_token_type(), 'Failed to reach text node.' );
658+
$this->assertSame( 'here', $processor->get_modifiable_text() );
659+
$this->assertSame(
660+
array( 'HTML', 'BODY', 'MATH', 'TEMPLATE', 'MI', 'C', '#text' ),
661+
$processor->get_breadcrumbs(),
662+
);
663+
}
664+
645665
/**
646666
* Ensures that the tag processor is case sensitive when removing CSS classes in no-quirks mode.
647667
*

0 commit comments

Comments
 (0)