Skip to content

Commit f7d9b1b

Browse files
committed
HTML API: Make WP_HTML_Processor::get_tag() namespace aware.
The HTML specification indicates that an HTML tag with the name "IMAGE" should be renamed as "IMG" and handled as if it were an "IMG", but this only applies to elements in the HTML namespace. In this patch the HTML Processor is updated to ensure that it doesn't remap the tag name when processing foreign content, such as SVG and MathML markup. Developed in https://github.com/wordpress/wordpress-develop/7330 Discussed in https://core.trac.wordpress.org/ticket/61656 Props dmsnell, jonsurrell. See #61576. git-svn-id: https://develop.svn.wordpress.org/trunk@59014 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 4dc4907 commit f7d9b1b

2 files changed

Lines changed: 58 additions & 11 deletions

File tree

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4727,17 +4727,13 @@ public function get_tag(): ?string {
47274727

47284728
$tag_name = parent::get_tag();
47294729

4730-
switch ( $tag_name ) {
4731-
case 'IMAGE':
4732-
/*
4733-
* > A start tag whose tag name is "image"
4734-
* > Change the token's tag name to "img" and reprocess it. (Don't ask.)
4735-
*/
4736-
return 'IMG';
4737-
4738-
default:
4739-
return $tag_name;
4740-
}
4730+
/*
4731+
* > A start tag whose tag name is "image"
4732+
* > Change the token's tag name to "img" and reprocess it. (Don't ask.)
4733+
*/
4734+
return ( 'IMAGE' === $tag_name && 'html' === $this->get_namespace() )
4735+
? 'IMG'
4736+
: $tag_name;
47414737
}
47424738

47434739
/**

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,57 @@ public function test_get_tag_is_null_once_document_is_finished() {
5454
$this->assertNull( $processor->get_tag() );
5555
}
5656

57+
/**
58+
* Ensures that the proper tag-name remapping happens for the `IMAGE` tag.
59+
*
60+
* An HTML parser should treat an IMAGE tag as if it were an IMG tag, but
61+
* only when found in the HTML namespace. As part of this rule, IMAGE tags
62+
* in the HTML namespace are also void elements, while those in foreign
63+
* content are not, making the self-closing flag significant.
64+
*
65+
* Example:
66+
*
67+
* // This input...
68+
* <image/><svg><image/></svg>
69+
*
70+
* // ...is equivalent to this normative HTML.
71+
* <img><svg><image/></svg>
72+
*
73+
* @ticket 61576
74+
*
75+
* @covers WP_HTML_Processor::get_tag
76+
*/
77+
public function test_get_tag_replaces_image_with_namespace_awareness() {
78+
$processor = WP_HTML_Processor::create_fragment( '<image/><svg><image/></svg>' );
79+
80+
$this->assertTrue(
81+
$processor->next_tag(),
82+
'Could not find initial "<image/>" tag: check test setup.'
83+
);
84+
85+
$this->assertSame(
86+
'IMG',
87+
$processor->get_tag(),
88+
'HTML tags with the name "IMAGE" should be remapped to "IMG"'
89+
);
90+
91+
$this->assertTrue(
92+
$processor->next_tag(),
93+
'Could not find "<svg>" tag: check test setup.'
94+
);
95+
96+
$this->assertTrue(
97+
$processor->next_tag(),
98+
'Could not find SVG "<image/>" tag: check test setup.'
99+
);
100+
101+
$this->assertSame(
102+
'IMAGE',
103+
$processor->get_tag(),
104+
'Should not remap "IMAGE" to "IMG" for foreign elements.'
105+
);
106+
}
107+
57108
/**
58109
* Ensures that the HTML Processor maintains its internal state through seek calls.
59110
*

0 commit comments

Comments
 (0)