Skip to content

Commit ccdb111

Browse files
committed
HTML API: Preserve XMP raw text serialization
1 parent 7ec1aa1 commit ccdb111

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,6 +1498,7 @@ public function serialize_token(): string {
14981498

14991499
case 'SCRIPT':
15001500
case 'STYLE':
1501+
case 'XMP':
15011502
break;
15021503

15031504
default:

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,30 @@ public function test_style_contents_are_not_escaped() {
134134
);
135135
}
136136

137+
/**
138+
* Ensures that XMP contents are not escaped, as they are not parsed like text nodes are.
139+
*
140+
* XMP contents are parsed as raw text: character references are never decoded.
141+
* Escaping the contents would change the document, e.g. a "<" would be replaced
142+
* by the literal text "&lt;" after serializing and re-parsing.
143+
*
144+
* @ticket 65372
145+
*/
146+
public function test_xmp_contents_are_not_escaped() {
147+
$normalized = WP_HTML_Processor::normalize( "<xmp>1 < 2 &amp; apples > or\x00anges</xmp>" );
148+
149+
$this->assertSame(
150+
"<xmp>1 < 2 &amp; apples > or\u{FFFD}anges</xmp>",
151+
$normalized,
152+
'Should have preserved text inside an XMP element, except for replacing NULL bytes.'
153+
);
154+
$this->assertSame(
155+
$normalized,
156+
WP_HTML_Processor::normalize( $normalized ),
157+
'Normalizing already-normalized XMP should not escape the raw text again.'
158+
);
159+
}
160+
137161
public function test_unexpected_closing_tags_are_removed() {
138162
$this->assertSame(
139163
WP_HTML_Processor::normalize( 'one</div>two</span>three' ),
@@ -281,6 +305,7 @@ public static function data_tokens_with_null_bytes() {
281305
'Foreign content text' => array( "<svg>one\x00two</svg>", "<svg>one\u{FFFD}two</svg>" ),
282306
'SCRIPT content' => array( "<script>alert(\x00)</script>", "<script>alert(\u{FFFD})</script>" ),
283307
'STYLE content' => array( "<style>\x00 {}</style>", "<style>\u{FFFD} {}</style>" ),
308+
'XMP content' => array( "<xmp>a\x00b</xmp>", "<xmp>a\u{FFFD}b</xmp>" ),
284309
'Comment text' => array( "<!-- \x00 -->", "<!-- \u{FFFD} -->" ),
285310
);
286311
}

0 commit comments

Comments
 (0)