|
|
@@ -29,7 +29,9 @@ const domConverter = new DomConverter( { blockFiller: NBSP_FILLER } );
|
|
|
*/
|
|
|
export function parseHtml( htmlString ) {
|
|
|
// Parse htmlString as native Document object.
|
|
|
- const htmlDocument = domParser.parseFromString( htmlString, 'text/html' );
|
|
|
+ const htmlDocument = domParser.parseFromString( normalizeEndTagsPrecedingSpace( htmlString ), 'text/html' );
|
|
|
+
|
|
|
+ normalizeSpacerunSpans( htmlDocument );
|
|
|
|
|
|
// Get `innerHTML` first as transforming to View modifies the source document.
|
|
|
const bodyString = htmlDocument.body.innerHTML;
|
|
|
@@ -86,3 +88,26 @@ function extractStyles( htmlDocument ) {
|
|
|
stylesString: stylesString.join( ' ' )
|
|
|
};
|
|
|
}
|
|
|
+
|
|
|
+// Replaces last space preceding elements closing tag with ` `. Such operation prevents spaces from being removed
|
|
|
+// during further DOM/View processing (see especially {@link module:engine/view/domconverter~DomConverter#_processDataFromDomText}).
|
|
|
+// This method also takes into account Word specific `<o:p></o:p>` empty tags.
|
|
|
+//
|
|
|
+// @param {String} htmlString HTML string in which spacing should be normalized.
|
|
|
+// @returns {String} Input HTML with spaces normalized.
|
|
|
+function normalizeEndTagsPrecedingSpace( htmlString ) {
|
|
|
+ return htmlString
|
|
|
+ .replace( / <\//g, '\u00A0</' )
|
|
|
+ .replace( / <o:p><\/o:p>/g, '\u00A0<o:p></o:p>' );
|
|
|
+}
|
|
|
+
|
|
|
+// Normalizes spacing in special Word `spacerun spans` (`<span style='mso-spacerun:yes'>\s+</span>`) by replacing
|
|
|
+// all spaces with ` `'s. This prevents spaces from being removed during further DOM/View processing
|
|
|
+// (see especially {@link module:engine/view/domconverter~DomConverter#_processDataFromDomText}).
|
|
|
+//
|
|
|
+// @param {Document} htmlDocument Native `Document` object in which spacing should be normalized.
|
|
|
+function normalizeSpacerunSpans( htmlDocument ) {
|
|
|
+ htmlDocument.querySelectorAll( 'span[style*=spacerun]' ).forEach( el => {
|
|
|
+ el.innerHTML = Array( el.innerText.length + 1 ).join( '\u00A0' );
|
|
|
+ } );
|
|
|
+}
|