Browse Source

When converting plain text to HTML use <br>s instead of spaces to represent new lines.

Piotrek Koszuliński 7 years ago
parent
commit
c7b03a50b5

+ 2 - 3
packages/ckeditor5-clipboard/src/utils/plaintexttohtml.js

@@ -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;' )

+ 13 - 4
packages/ckeditor5-clipboard/tests/utils/plaintexttohtml.js

@@ -14,6 +14,19 @@ 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;' );
 	} );
@@ -21,8 +34,4 @@ describe( 'plainTextToHtml()', () => {
 	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' );
-	} );
 } );