Skip to content

Commit 98fc44d

Browse files
Tests: Verify preservation of newlines in preformatted elements
1 parent 7b12056 commit 98fc44d

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

tests/phpunit/tests/formatting/stripHtmlNewlines.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,25 @@ public function test_strips_newlines_from_text_nodes() {
2525
'Newlines within and around inline elements should be stripped.'
2626
);
2727
}
28+
public function test_preserves_newlines_in_preformatted_elements() {
29+
$input = "<p>Normal\ntext</p>\n<pre>\nPreformatted\nlines\n</pre>\n<p>More\ntext</p>";
30+
$result = strip_html_newlines( $input );
31+
32+
$this->assertStringContainsString( 'Normal text', $result, 'Newlines in normal text should be stripped.' );
33+
$this->assertStringContainsString( 'More text', $result, 'Newlines in trailing paragraph should be stripped.' );
34+
$this->assertStringContainsString( "\nPreformatted\nlines\n", $result, 'Newlines inside <pre> should be preserved.' );
35+
36+
$preserved_cases = array(
37+
'code' => "<p>A\nB</p><code>x\ny</code>",
38+
'kbd' => "<p>A\nB</p><kbd>x\ny</kbd>",
39+
'script' => "<p>A\nB</p><script>x\ny</script>",
40+
'style' => "<p>A\nB</p><style>x\ny</style>",
41+
);
42+
43+
foreach ( $preserved_cases as $tag => $html ) {
44+
$out = strip_html_newlines( $html );
45+
$this->assertStringContainsString( 'A B', $out, "Text node newline should be stripped around <{$tag}>." );
46+
$this->assertStringContainsString( "x\ny", $out, "Newline inside <{$tag}> should be preserved." );
47+
}
48+
}
2849
}

0 commit comments

Comments
 (0)