Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preformatted: save line breaks as characters #14653

Merged
merged 2 commits into from
Mar 27, 2019
Merged
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: 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

ellatrix marked this conversation as resolved.
Show resolved Hide resolved
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 -->"
`;