Przeglądaj źródła

Merge pull request #52 from ckeditor/kobsy-t/49

Fix: Handle "spacerun spans" with mixed whitespaces. Closes #49. Closes #50.

Huge thanks to [Matt Kobs](https://github.com/kobsy) for this contribution!
Piotrek Koszuliński 7 lat temu
rodzic
commit
b9c029da2b

+ 7 - 2
packages/ckeditor5-paste-from-office/src/filters/space.js

@@ -17,10 +17,15 @@
  * @returns {String} Input HTML with spaces normalized.
  */
 export function normalizeSpacing( htmlString ) {
-	return normalizeSafariSpaceSpans( normalizeSafariSpaceSpans( htmlString ) ) // Run normalization two times to cover nested spans.
+	// Run normalizeSafariSpaceSpans() two times to cover nested spans.
+	return normalizeSafariSpaceSpans( normalizeSafariSpaceSpans( htmlString ) )
+		// Remove all \r\n from "spacerun spans" so the last replace line doesn't strip all whitespaces.
+		.replace( /(<span style=['"]mso-spacerun:yes['"]>[\s]*?)[\r\n]+(\s*<\/span>)/g, '$1$2' )
+		.replace( /<span style=['"]mso-spacerun:yes['"]><\/span>/g, '' )
 		.replace( / <\//g, '\u00A0</' )
 		.replace( / <o:p><\/o:p>/g, '\u00A0<o:p></o:p>' )
-		.replace( />(\s*(\r\n?|\n)\s*)+</g, '><' );
+		// Remove all whitespaces when they contain any \r or \n.
+		.replace( />(\s*[\r\n]\s*)</g, '><' );
 }
 
 /**

+ 35 - 0
packages/ckeditor5-paste-from-office/tests/filters/space.js

@@ -24,6 +24,41 @@ describe( 'Filters', () => {
 				expect( normalizeSpacing( input ) ).to.equal( expected );
 			} );
 
+			it( 'should remove newlines from spacerun spans #1', () => {
+				const input = '<span style=\'mso-spacerun:yes\'>  \n</span>';
+				const expected = '<span style=\'mso-spacerun:yes\'> \u00A0</span>';
+
+				expect( normalizeSpacing( input ) ).to.equal( expected );
+			} );
+
+			it( 'should remove newlines from spacerun spans #2', () => {
+				const input = '<span style=\'mso-spacerun:yes\'> \r\n</span>';
+				const expected = '<span style=\'mso-spacerun:yes\'>\u00A0</span>';
+
+				expect( normalizeSpacing( input ) ).to.equal( expected );
+			} );
+
+			it( 'should remove newlines from spacerun spans #3', () => {
+				const input = '<span style=\'mso-spacerun:yes\'>  \r\n\n  </span>';
+				const expected = '<span style=\'mso-spacerun:yes\'>   \u00A0</span>';
+
+				expect( normalizeSpacing( input ) ).to.equal( expected );
+			} );
+
+			it( 'should remove newlines from spacerun spans #4', () => {
+				const input = '<span style=\'mso-spacerun:yes\'>\n\n\n  </span>';
+				const expected = '<span style=\'mso-spacerun:yes\'> \u00A0</span>';
+
+				expect( normalizeSpacing( input ) ).to.equal( expected );
+			} );
+
+			it( 'should remove newlines from spacerun spans #5', () => {
+				const input = '<span style=\'mso-spacerun:yes\'>\n\n</span>';
+				const expected = '';
+
+				expect( normalizeSpacing( input ) ).to.equal( expected );
+			} );
+
 			it( 'should remove multiline sequences of whitespaces', () => {
 				const input = '<p>Foo</p> \n\n   \n<p>Bar</p>   \r\n\r\n  <p>Baz</p>';
 				const expected = '<p>Foo</p><p>Bar</p><p>Baz</p>';