Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge branch 't/ckeditor5/766'
Browse files Browse the repository at this point in the history
Fix: When pasting a plain text, single new line characters should be converted to `<br>`s. Closes ckeditor/ckeditor5#766.
  • Loading branch information
Reinmar committed Jun 11, 2018
2 parents b40ec4b + 50a27bf commit be21676
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
5 changes: 2 additions & 3 deletions src/utils/plaintexttohtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ export default function plainTextToHtml( text ) {
// Encode <>.
.replace( /</g, '&lt;' )
.replace( />/g, '&gt;' )
// Creates paragraphs for double line breaks and change single line breaks to spaces.
// In the future single line breaks may be converted into <br>s.
// Creates paragraphs for double line breaks and change single line breaks to <br>s.
.replace( /\n\n/g, '</p><p>' )
.replace( /\n/g, ' ' )
.replace( /\n/g, '<br>' )
// Preserve trailing spaces (only the first and last one – the rest is handled below).
.replace( /^\s/, '&nbsp;' )
.replace( /\s$/, '&nbsp;' )
Expand Down
17 changes: 13 additions & 4 deletions tests/utils/plaintexttohtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,24 @@ describe( 'plainTextToHtml()', () => {
expect( plainTextToHtml( 'x\n\ny\n\nz' ) ).to.equal( '<p>x</p><p>y</p><p>z</p>' );
} );

it( 'turns single line breaks into <br>s', () => {
expect( plainTextToHtml( 'x\ny\nz' ) ).to.equal( 'x<br>y<br>z' );
} );

it( 'turns double and single line breaks to a combination of paragraphs and <br>s', () => {
expect( plainTextToHtml( 'a\nb\n\nc\nd' ) ).to.equal( '<p>a<br>b</p><p>c<br>d</p>' );
} );

it( 'turns 3-5 subsequent new lines to a combination of paragraphs and <br>s', () => {
expect( plainTextToHtml( 'a\n\n\nb\n\n\n\nc\n\n\n\n\nd' ) )
.to.equal( '<p>a</p><p><br>b</p><p></p><p>c</p><p></p><p><br>d</p>' );
} );

it( 'preserves trailing spaces', () => {
expect( plainTextToHtml( ' x ' ) ).to.equal( '&nbsp;x&nbsp;' );
} );

it( 'preserve subsequent spaces', () => {
expect( plainTextToHtml( 'x y ' ) ).to.equal( 'x &nbsp;y &nbsp;' );
} );

it( 'turns single line breaks to spaces', () => {
expect( plainTextToHtml( 'x\ny\nz' ) ).to.equal( 'x y z' );
} );
} );

0 comments on commit be21676

Please sign in to comment.