Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/wp-includes/html-api/class-wp-html-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1461,8 +1461,8 @@ public function serialize_token(): string {

/*
* The HTML parser strips a leading newline immediately after the start
* tag of TEXTAREA, PRE, and LISTING elements. When serializing, prepend
* a leading newline to ensure the semantic HTML content is preserved.
* tag of HTML TEXTAREA, PRE, and LISTING elements. When serializing,
* prepend a leading newline to ensure the semantic HTML content is preserved.
*
* For example, `<pre>\n\nX</pre>` must not become `<pre>\nX</pre>` because its content
* has changed. However, `<pre>X</pre>` and `<pre>\nX</pre>` are _equivalent_.
Expand All @@ -1481,7 +1481,7 @@ public function serialize_token(): string {
*
* @see https://html.spec.whatwg.org/multipage/parsing.html
*/
if ( 'TEXTAREA' === $tag_name || 'PRE' === $tag_name || 'LISTING' === $tag_name ) {
if ( $in_html && ( 'TEXTAREA' === $tag_name || 'PRE' === $tag_name || 'LISTING' === $tag_name ) ) {
$html .= "\n";
}

Expand Down
82 changes: 82 additions & 0 deletions tests/phpunit/tests/html-api/wpHtmlProcessor-serialize.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,88 @@ public function test_normalize_special_leading_newline_handling( string $input,
$this->assertEqualHTML( $expected, $normalized_twice );
}

/**
* Ensures that the special leading newline rule applies only in the HTML namespace.
*
* @ticket 64607
*
* @dataProvider data_provider_special_leading_newline_namespace_serialization
*
* @param string $input HTML input containing a PRE, LISTING, or TEXTAREA element.
* @param string $expected Expected normalized output.
*/
public function test_special_leading_newline_rule_depends_on_namespace( string $input, string $expected ) {
$normalized = WP_HTML_Processor::normalize( $input );
$this->assertSame(
$expected,
$normalized,
'Should serialize special leading newlines according to the element namespace.'
);
$this->assertSame(
$expected,
WP_HTML_Processor::normalize( $normalized ),
'Normalizing already-normalized special leading newlines should not change them.'
);
}

/**
* Data provider.
*
* @return array[]
*/
public static function data_provider_special_leading_newline_namespace_serialization() {
return array(
'MathML TEXTAREA' => array(
'<math><textarea>X</textarea></math>',
'<math><textarea>X</textarea></math>',
),
'MathML TEXTAREA with leading newline' => array(
"<math><textarea>\nX</textarea></math>",
"<math><textarea>\nX</textarea></math>",
),
'SVG TEXTAREA' => array(
'<svg><textarea>X</textarea></svg>',
'<svg><textarea>X</textarea></svg>',
),
'SVG TEXTAREA with leading newline' => array(
"<svg><textarea>\nX</textarea></svg>",
"<svg><textarea>\nX</textarea></svg>",
),
'HTML TEXTAREA inside SVG HTML integration point' => array(
'<svg><foreignObject><textarea>X</textarea></foreignObject></svg>',
"<svg><foreignObject><textarea>\nX</textarea></foreignObject></svg>",
),
'HTML TEXTAREA with leading newline inside SVG HTML integration point' => array(
"<svg><foreignObject><textarea>\n\nX</textarea></foreignObject></svg>",
"<svg><foreignObject><textarea>\n\nX</textarea></foreignObject></svg>",
),
'HTML TEXTAREA inside MathML text integration point' => array(
'<math><mtext><textarea>X</textarea></mtext></math>',
"<math><mtext><textarea>\nX</textarea></mtext></math>",
),
'HTML TEXTAREA with leading newline inside MathML text integration point' => array(
"<math><mtext><textarea>\n\nX</textarea></mtext></math>",
"<math><mtext><textarea>\n\nX</textarea></mtext></math>",
),
'HTML TEXTAREA inside MathML HTML integration point' => array(
'<math><annotation-xml encoding="text/html"><textarea>X</textarea></annotation-xml></math>',
"<math><annotation-xml encoding=\"text/html\"><textarea>\nX</textarea></annotation-xml></math>",
),
'HTML TEXTAREA with leading newline inside MathML HTML integration point' => array(
"<math><annotation-xml encoding=\"text/html\"><textarea>\n\nX</textarea></annotation-xml></math>",
"<math><annotation-xml encoding=\"text/html\"><textarea>\n\nX</textarea></annotation-xml></math>",
),
'HTML PRE after exiting SVG foreign content' => array(
'<svg><pre>X</pre></svg>',
"<svg></svg><pre>\nX</pre>",
),
'HTML LISTING after exiting MathML foreign content' => array(
'<math><listing>X</listing></math>',
"<math></math><listing>\nX</listing>",
),
);
}

/**
* Ensures that fuzzer-discovered inputs do not emit native PHP errors.
*
Expand Down
Loading