Skip to content

Commit 38f521a

Browse files
authored
Preformatted: save line breaks as characters (#14653)
* Preformatted: save line breaks as characters * Update e2e test
1 parent 8baf2bb commit 38f521a

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

packages/block-library/src/preformatted/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,14 @@ export const settings = {
6868
return (
6969
<RichText
7070
tagName="pre"
71+
// Ensure line breaks are normalised to HTML.
7172
value={ content.replace( /\n/g, '<br>' ) }
7273
onChange={ ( nextContent ) => {
7374
setAttributes( {
74-
content: nextContent,
75+
// Ensure line breaks are normalised to characters. This
76+
// saves space, is easier to read, and ensures display
77+
// filters work correctly.
78+
content: nextContent.replace( /<br ?\/?>/g, '\n' ),
7579
} );
7680
} }
7781
placeholder={ __( 'Write preformatted text…' ) }

packages/e2e-tests/specs/__snapshots__/adding-blocks.test.js.snap

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ exports[`adding blocks Should insert content using the placeholder and the regul
4242
<!-- /wp:quote -->
4343
4444
<!-- wp:preformatted -->
45-
<pre class=\\"wp-block-preformatted\\">Pre text<br><br>Foo</pre>
45+
<pre class=\\"wp-block-preformatted\\">Pre text
46+
47+
Foo</pre>
4648
<!-- /wp:preformatted -->
4749
4850
<!-- wp:shortcode -->

packages/e2e-tests/specs/blocks/__snapshots__/preformatted.test.js.snap

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ exports[`Preformatted should preserve character newlines 1`] = `
99

1010
exports[`Preformatted should preserve character newlines 2`] = `
1111
"<!-- wp:preformatted -->
12-
<pre class=\\"wp-block-preformatted\\">0<br>1<br>2</pre>
12+
<pre class=\\"wp-block-preformatted\\">0
13+
1
14+
2</pre>
1315
<!-- /wp:preformatted -->"
1416
`;

0 commit comments

Comments
 (0)