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 #89 from ckeditor/i/5645+2095
Browse files Browse the repository at this point in the history
Fix: Fixed various issues with oddly formatted space run spans.
  • Loading branch information
Mgsy authored Nov 7, 2019
2 parents fd7ba1d + e817996 commit 2cd7b0f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/filters/space.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export function normalizeSpacing( htmlString ) {
// 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( /(<span\s+style=['"]mso-spacerun:yes['"]>[\s]*?)[\r\n]+(\s*<\/span>)/g, '$1$2' )
.replace( /<span\s+style=['"]mso-spacerun:yes['"]><\/span>/g, '' )
.replace( / <\//g, '\u00A0</' )
.replace( / <o:p><\/o:p>/g, '\u00A0<o:p></o:p>' )
// Remove <o:p> block filler from empty paragraph. Safari uses \u00A0 instead of &nbsp;.
Expand All @@ -41,7 +41,10 @@ export function normalizeSpacerunSpans( htmlDocument ) {
htmlDocument.querySelectorAll( 'span[style*=spacerun]' ).forEach( el => {
// Use `el.childNodes[ 0 ].data.length` instead of `el.innerText.length`. For `el.innerText.length` which
// contains spaces mixed with `&nbsp;` Edge browser returns incorrect length.
const innerTextLength = el.childNodes[ 0 ].data.length;
const innerTextLength = ( el.childNodes &&
el.childNodes[ 0 ] &&
el.childNodes[ 0 ].data &&
el.childNodes[ 0 ].data.length ) || 0;

el.innerHTML = Array( innerTextLength + 1 ).join( '\u00A0 ' ).substr( 0, innerTextLength );
} );
Expand Down
36 changes: 36 additions & 0 deletions tests/filters/space.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ describe( 'PasteFromOffice - filters', () => {

expect( normalizeSpacing( input ) ).to.equal( expected );
} );

// ckeditor5#2095
it( 'should detect space spans which are split into multiple lines', () => {
const input =
'<p><span style=\'font-size:13.0pt;line-height:150%;\n' +
'font-family:"Times New Roman",serif\'><span\n' +
'style=\'mso-spacerun:yes\'>\n' +
'</span><span style=\'mso-spacerun:yes\'> </span><span\n' +
'style=\'mso-spacerun:yes\'> </span><span style=\'mso-spacerun:yes\'> </span>Test<o:p></o:p></span></p>';

const expected =
'<p><span style=\'font-size:13.0pt;line-height:150%;\nfont-family:"Times New Roman",serif\'>' +
'<span style=\'mso-spacerun:yes\'>  </span>' +
'<span\nstyle=\'mso-spacerun:yes\'>  </span>' +
'<span style=\'mso-spacerun:yes\'> </span>Test<o:p></o:p></span></p>';

expect( normalizeSpacing( input ) ).to.equal( expected );
} );
} );

describe( 'normalizeSpacerunSpans()', () => {
Expand All @@ -100,6 +118,24 @@ describe( 'PasteFromOffice - filters', () => {

expect( htmlDocument.body.innerHTML.replace( /'/g, '"' ).replace( /: /g, ':' ) ).to.equal( expected );
} );

// ckeditor5#5645
it( 'should normalize spaces inside special "span.spacerun" elements that contain no data', () => {
const input = '<p> <span style=\'mso-spacerun:yes\'> </span>Foo</p>' +
'<p> Baz <span style=\'mso-spacerun:yes\'></span></p>';

const expected = '<p> <span style="mso-spacerun:yes">&nbsp; &nbsp;</span>Foo</p>' +
'<p> Baz <span style="mso-spacerun:yes"></span></p>';

const domParser = new DOMParser();
const htmlDocument = domParser.parseFromString( input, 'text/html' );

expect( htmlDocument.body.innerHTML.replace( /'/g, '"' ).replace( /: /g, ':' ) ).to.not.equal( expected );

normalizeSpacerunSpans( htmlDocument );

expect( htmlDocument.body.innerHTML.replace( /'/g, '"' ).replace( /: /g, ':' ) ).to.equal( expected );
} );
} );
} );
} );

0 comments on commit 2cd7b0f

Please sign in to comment.