Skip to content

Commit

Permalink
Merge pull request #8047 from ckeditor/i/7884
Browse files Browse the repository at this point in the history
Feature (clipboard): Improved line to paragraph/soft break retention when pasting from plain text. Closes #7884.

MINOR BREAKING CHANGE (clipboard): When pasting plain text each double line break is now treated as a paragraph separator, while a single line break is converted into a soft break. Formerly every single line break was treated as paragraph separation.
  • Loading branch information
jodator authored Sep 11, 2020
2 parents d96e8a8 + 759e5d5 commit a4b8996
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
8 changes: 5 additions & 3 deletions packages/ckeditor5-clipboard/src/utils/plaintexttohtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ export default function plainTextToHtml( text ) {
// Encode <>.
.replace( /</g, '&lt;' )
.replace( />/g, '&gt;' )
// Creates paragraphs for every line breaks.
.replace( /\n/g, '</p><p>' )
// Creates a paragraph for each double line break.
.replace( /\r?\n\r?\n/g, '</p><p>' )
// Creates a line break for each single line break.
.replace( /\r?\n/g, '<br>' )
// Preserve trailing spaces (only the first and last one – the rest is handled below).
.replace( /^\s/, '&nbsp;' )
.replace( /\s$/, '&nbsp;' )
// Preserve other subsequent spaces now.
.replace( /\s\s/g, ' &nbsp;' );

if ( text.indexOf( '</p><p>' ) > -1 ) {
if ( text.includes( '</p><p>' ) || text.includes( '<br>' ) ) {
// If we created paragraphs above, add the trailing ones.
text = `<p>${ text }</p>`;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ckeditor5-clipboard/tests/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe( 'Clipboard feature', () => {
clipboardPlugin.on( 'inputTransformation', ( evt, data ) => {
expect( data.content ).is.instanceOf( ViewDocumentFragment );
expect( data.dataTransfer ).to.equal( dataTransferMock );
expect( stringifyView( data.content ) ).to.equal( '<p>x</p><p></p><p>y z</p>' );
expect( stringifyView( data.content ) ).to.equal( '<p>x</p><p>y z</p>' );

done();
} );
Expand Down
18 changes: 13 additions & 5 deletions packages/ckeditor5-clipboard/tests/utils/plaintexttohtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,24 @@ describe( 'plainTextToHtml()', () => {
expect( plainTextToHtml( 'x y <z>' ) ).to.equal( 'x y &lt;z&gt;' );
} );

it( 'turns a single line break into paragraphs', () => {
expect( plainTextToHtml( 'x\ny\nz' ) ).to.equal( '<p>x</p><p>y</p><p>z</p>' );
it( 'turns double line breaks into paragraphs (Linux/Mac EOL style)', () => {
expect( plainTextToHtml( 'x\n\ny\n\nz' ) ).to.equal( '<p>x</p><p>y</p><p>z</p>' );
} );

it( 'turns double line breaks into paragraphs', () => {
expect( plainTextToHtml( 'x\n\ny\n\nz' ) ).to.equal( '<p>x</p><p></p><p>y</p><p></p><p>z</p>' );
it( 'turns double line breaks into paragraphs (Windows EOL style)', () => {
expect( plainTextToHtml( 'x\r\n\r\ny\r\n\r\nz' ) ).to.equal( '<p>x</p><p>y</p><p>z</p>' );
} );

it( 'turns single line breaks into soft breaks (Linux/Mac EOL style)', () => {
expect( plainTextToHtml( 'x\ny\nz' ) ).to.equal( '<p>x<br>y<br>z</p>' );
} );

it( 'turns single line breaks into soft breaks (Windows EOL style)', () => {
expect( plainTextToHtml( 'x\r\ny\r\nz' ) ).to.equal( '<p>x<br>y<br>z</p>' );
} );

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

it( 'preserves trailing spaces', () => {
Expand Down

0 comments on commit a4b8996

Please sign in to comment.