|
|
@@ -10,7 +10,6 @@
|
|
|
/* globals DOMParser */
|
|
|
|
|
|
import DomConverter from '@ckeditor/ckeditor5-engine/src/view/domconverter';
|
|
|
-import normalizeClipboardHtml from '@ckeditor/ckeditor5-clipboard/src/utils/normalizeclipboarddata';
|
|
|
import { NBSP_FILLER } from '@ckeditor/ckeditor5-engine/src/view/filler';
|
|
|
|
|
|
const domParser = new DOMParser();
|
|
|
@@ -30,7 +29,7 @@ const domConverter = new DomConverter( { blockFiller: NBSP_FILLER } );
|
|
|
*/
|
|
|
export function parseHtml( htmlString ) {
|
|
|
// Parse htmlString as native Document object.
|
|
|
- const htmlDocument = domParser.parseFromString( normalizeEndTagsPrecedingSpace( htmlString ), 'text/html' );
|
|
|
+ const htmlDocument = domParser.parseFromString( normalizeSpacing( htmlString ), 'text/html' );
|
|
|
|
|
|
normalizeSpacerunSpans( htmlDocument );
|
|
|
|
|
|
@@ -96,12 +95,27 @@ function extractStyles( htmlDocument ) {
|
|
|
//
|
|
|
// @param {String} htmlString HTML string in which spacing should be normalized.
|
|
|
// @returns {String} Input HTML with spaces normalized.
|
|
|
-function normalizeEndTagsPrecedingSpace( htmlString ) {
|
|
|
- return normalizeClipboardHtml( htmlString )
|
|
|
+function normalizeSpacing( htmlString ) {
|
|
|
+ return normalizeSafariSpaceSpans( normalizeSafariSpaceSpans( htmlString ) ) // Run normalization two times to cover nested spans.
|
|
|
.replace( / <\//g, '\u00A0</' )
|
|
|
.replace( / <o:p><\/o:p>/g, '\u00A0<o:p></o:p>' );
|
|
|
}
|
|
|
|
|
|
+// Normalizes specific spacing generated by Safari when content pasted from Word (`<span class="Apple-converted-space"> </span>`)
|
|
|
+// by replacing all spaces sequences longer than 1 space with ` ` pairs. This prevents spaces from being removed during
|
|
|
+// further DOM/View processing (see especially {@link module:engine/view/domconverter~DomConverter#_processDataFromDomText}).
|
|
|
+//
|
|
|
+// This function is similar to {@link module:clipboard/utils/normalizeclipboarddata normalizeClipboardData util} but uses
|
|
|
+// regular spaces / sequence for replacement.
|
|
|
+//
|
|
|
+// @param {String} htmlString HTML string in which spacing should be normalized
|
|
|
+// @returns {String} Input HTML with spaces normalized.
|
|
|
+function normalizeSafariSpaceSpans( htmlString ) {
|
|
|
+ return htmlString.replace( /<span(?: class="Apple-converted-space"|)>(\s+)<\/span>/g, ( fullMatch, spaces ) => {
|
|
|
+ return spaces.length === 1 ? ' ' : Array( spaces.length + 1 ).join( '\u00A0 ' ).substr( 0, spaces.length );
|
|
|
+ } );
|
|
|
+}
|
|
|
+
|
|
|
// Normalizes spacing in special Word `spacerun spans` (`<span style='mso-spacerun:yes'>\s+</span>`) by replacing
|
|
|
// all spaces with ` ` pairs. This prevents spaces from being removed during further DOM/View processing
|
|
|
// (see especially {@link module:engine/view/domconverter~DomConverter#_processDataFromDomText}).
|