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

Commit

Permalink
Merge pull request #52 from ckeditor/kobsy-t/49
Browse files Browse the repository at this point in the history
Fix: Handle "spacerun spans" with mixed whitespaces. Closes #49. Closes #50.

Huge thanks to [Matt Kobs](https://github.com/kobsy) for this contribution!
  • Loading branch information
Reinmar authored Feb 15, 2019
2 parents 4759492 + e8ba4d4 commit 7fb132f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/filters/space.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '><' );
}

/**
Expand Down
35 changes: 35 additions & 0 deletions tests/filters/space.js
Original file line number Diff line number Diff line change
Expand Up @@ -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>';
Expand Down

0 comments on commit 7fb132f

Please sign in to comment.