diff --git a/packages/ckeditor5-clipboard/src/utils/plaintexttohtml.js b/packages/ckeditor5-clipboard/src/utils/plaintexttohtml.js index ee986bbe527..ea611fcedd3 100644 --- a/packages/ckeditor5-clipboard/src/utils/plaintexttohtml.js +++ b/packages/ckeditor5-clipboard/src/utils/plaintexttohtml.js @@ -18,15 +18,17 @@ export default function plainTextToHtml( text ) { // Encode <>. .replace( //g, '>' ) - // Creates paragraphs for every line breaks. - .replace( /\n/g, '

' ) + // Creates a paragraph for each double line break. + .replace( /\r?\n\r?\n/g, '

' ) + // Creates a line break for each single line break. + .replace( /\r?\n/g, '
' ) // Preserve trailing spaces (only the first and last one – the rest is handled below). .replace( /^\s/, ' ' ) .replace( /\s$/, ' ' ) // Preserve other subsequent spaces now. .replace( /\s\s/g, '  ' ); - if ( text.indexOf( '

' ) > -1 ) { + if ( text.includes( '

' ) || text.includes( '
' ) ) { // If we created paragraphs above, add the trailing ones. text = `

${ text }

`; } diff --git a/packages/ckeditor5-clipboard/tests/clipboard.js b/packages/ckeditor5-clipboard/tests/clipboard.js index 3ecc18eee7a..3caa2a3c6e9 100644 --- a/packages/ckeditor5-clipboard/tests/clipboard.js +++ b/packages/ckeditor5-clipboard/tests/clipboard.js @@ -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( '

x

y z

' ); + expect( stringifyView( data.content ) ).to.equal( '

x

y z

' ); done(); } ); diff --git a/packages/ckeditor5-clipboard/tests/utils/plaintexttohtml.js b/packages/ckeditor5-clipboard/tests/utils/plaintexttohtml.js index da66968305b..12ed325ce90 100644 --- a/packages/ckeditor5-clipboard/tests/utils/plaintexttohtml.js +++ b/packages/ckeditor5-clipboard/tests/utils/plaintexttohtml.js @@ -10,16 +10,24 @@ describe( 'plainTextToHtml()', () => { expect( plainTextToHtml( 'x y ' ) ).to.equal( 'x y <z>' ); } ); - it( 'turns a single line break into paragraphs', () => { - expect( plainTextToHtml( 'x\ny\nz' ) ).to.equal( '

x

y

z

' ); + it( 'turns double line breaks into paragraphs (Linux/Mac EOL style)', () => { + expect( plainTextToHtml( 'x\n\ny\n\nz' ) ).to.equal( '

x

y

z

' ); } ); - it( 'turns double line breaks into paragraphs', () => { - expect( plainTextToHtml( 'x\n\ny\n\nz' ) ).to.equal( '

x

y

z

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

x

y

z

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

x
y
z

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

x
y
z

' ); } ); it( 'turns combination of different amount of line breaks to paragraphs', () => { - expect( plainTextToHtml( 'a\nb\n\nc\n\n\nd' ) ).to.equal( '

a

b

c

d

' ); + expect( plainTextToHtml( 'a\n\nb\nc\n\n\n\nd\ne' ) ).to.equal( '

a

b
c

d
e

' ); } ); it( 'preserves trailing spaces', () => {