Skip to content

Commit

Permalink
HTML API: Expect closer on foreign content void lookalike elements.
Browse files Browse the repository at this point in the history
Ensure that `expects_closer` returns `false` on tags that look like void HTML tags, but are actually ''not'' void tags in foreign content.

Props jonsurrell, bernhard-reiter.
Fixes #62363.

git-svn-id: https://develop.svn.wordpress.org/trunk@59392 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
ockham committed Nov 12, 2024
1 parent 7b3d107 commit ed0ad98
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/wp-includes/html-api/class-wp-html-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ public function expects_closer( ?WP_HTML_Token $node = null ): ?bool {
// Doctype declarations.
'html' === $token_name ||
// Void elements.
self::is_void( $token_name ) ||
( 'html' === $token_namespace && self::is_void( $token_name ) ) ||
// Special atomic elements.
( 'html' === $token_namespace && in_array( $token_name, array( 'IFRAME', 'NOEMBED', 'NOFRAMES', 'SCRIPT', 'STYLE', 'TEXTAREA', 'TITLE', 'XMP' ), true ) ) ||
// Self-closing elements in foreign content.
Expand Down
25 changes: 25 additions & 0 deletions tests/phpunit/tests/html-api/wpHtmlProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,31 @@ public function test_expects_closer_foreign_content_self_closing() {
$this->assertTrue( $processor->expects_closer() );
}

/**
* Ensures that expects_closer works for void-like elements in foreign content.
*
* For example, `<svg><input>text` creates an `svg:input` that contains a text node.
* This input should not be treated as a void tag and _should_ expect a close tag.
*
* @dataProvider data_void_tags
*
* @ticket 62363
*/
public function test_expects_closer_foreign_content_not_void( string $void_tag ) {
$processor = WP_HTML_Processor::create_fragment( "<svg><{$void_tag}>" );

$this->assertTrue( $processor->next_tag( $void_tag ) );

// Some void-like tags will close the SVG element and be HTML tags.
if ( $processor->get_namespace() === 'svg' ) {
$this->assertSame( array( 'HTML', 'BODY', 'SVG', $void_tag ), $processor->get_breadcrumbs() );
$this->assertTrue( $processor->expects_closer() );
} else {
$this->assertSame( array( 'HTML', 'BODY', $void_tag ), $processor->get_breadcrumbs() );
$this->assertFalse( $processor->expects_closer() );
}
}

/**
* Ensures that self-closing foreign SCRIPT elements are properly found.
*
Expand Down

0 comments on commit ed0ad98

Please sign in to comment.