Skip to content

Commit

Permalink
Preformatted: save line breaks as characters (#14653)
Browse files Browse the repository at this point in the history
* Preformatted: save line breaks as characters

* Update e2e test
  • Loading branch information
ellatrix authored Mar 27, 2019
1 parent 8baf2bb commit 38f521a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
6 changes: 5 additions & 1 deletion packages/block-library/src/preformatted/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,14 @@ export const settings = {
return (
<RichText
tagName="pre"
// Ensure line breaks are normalised to HTML.
value={ content.replace( /\n/g, '<br>' ) }
onChange={ ( nextContent ) => {
setAttributes( {
content: nextContent,
// Ensure line breaks are normalised to characters. This
// saves space, is easier to read, and ensures display
// filters work correctly.
content: nextContent.replace( /<br ?\/?>/g, '\n' ),
} );
} }
placeholder={ __( 'Write preformatted text…' ) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ exports[`adding blocks Should insert content using the placeholder and the regul
<!-- /wp:quote -->
<!-- wp:preformatted -->
<pre class=\\"wp-block-preformatted\\">Pre text<br><br>Foo</pre>
<pre class=\\"wp-block-preformatted\\">Pre text
Foo</pre>
<!-- /wp:preformatted -->
<!-- wp:shortcode -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ exports[`Preformatted should preserve character newlines 1`] = `

exports[`Preformatted should preserve character newlines 2`] = `
"<!-- wp:preformatted -->
<pre class=\\"wp-block-preformatted\\">0<br>1<br>2</pre>
<pre class=\\"wp-block-preformatted\\">0
1
2</pre>
<!-- /wp:preformatted -->"
`;

0 comments on commit 38f521a

Please sign in to comment.