Skip to content

Commit 7e9ef66

Browse files
committed
HTML API: Preserve newlines when normalizing special elements.
Ensures normalization preserves content in `PRE`, `LISTING`, and `TEXTAREA` elements. These elements ignore a single leading newline during parsing. Normalization now injects a newline after the tag opener to trigger this behavior, preventing significant newlines from being incorrectly stripped. Developed in WordPress/wordpress-develop#10871. Props jonsurrell, dmsnell, mukesh27. Fixes #64607. Built from https://develop.svn.wordpress.org/trunk@61747 git-svn-id: http://core.svn.wordpress.org/trunk@61053 1a063a9b-81f0-0310-95a4-ce76da25c4cd
1 parent f29ad11 commit 7e9ef66

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,6 +1414,32 @@ public function serialize_token(): string {
14141414

14151415
$html .= '>';
14161416

1417+
/*
1418+
* The HTML parser strips a leading newline immediately after the start
1419+
* tag of TEXTAREA, PRE, and LISTING elements. When serializing, prepend
1420+
* a leading newline to ensure the semantic HTML content is preserved.
1421+
*
1422+
* For example, `<pre>\n\nX</pre>` must not become `<pre>\nX</pre>` because its content
1423+
* has changed. However, `<pre>X</pre>` and `<pre>\nX</pre>` are _equivalent_.
1424+
*
1425+
* > A start tag whose tag name is "textarea"
1426+
* > …
1427+
* > If the next token is a U+000A LINE FEED (LF) character token, then ignore
1428+
* > that token and move on to the next one. (Newlines at the start of textarea
1429+
* > elements are ignored as an authoring convenience.)
1430+
*
1431+
* > A start tag whose tag name is one of: "pre", "listing"
1432+
* > …
1433+
* > If the next token is a U+000A LINE FEED (LF) character token, then ignore
1434+
* > that token and move on to the next one. (Newlines at the start of pre blocks
1435+
* > are ignored as an authoring convenience.)
1436+
*
1437+
* @see https://html.spec.whatwg.org/multipage/parsing.html
1438+
*/
1439+
if ( 'TEXTAREA' === $tag_name || 'PRE' === $tag_name || 'LISTING' === $tag_name ) {
1440+
$html .= "\n";
1441+
}
1442+
14171443
// Flush out self-contained elements.
14181444
if ( $in_html && in_array( $tag_name, array( 'IFRAME', 'NOEMBED', 'NOFRAMES', 'SCRIPT', 'STYLE', 'TEXTAREA', 'TITLE', 'XMP' ), true ) ) {
14191445
$text = $this->get_modifiable_text();

wp-includes/version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @global string $wp_version
1818
*/
19-
$wp_version = '7.0-beta1-61746';
19+
$wp_version = '7.0-beta1-61747';
2020

2121
/**
2222
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

0 commit comments

Comments
 (0)