Преглед изворни кода

Handle Safari specific spacing.

Krzysztof Krztoń пре 7 година
родитељ
комит
4ec7f96472

+ 18 - 4
packages/ckeditor5-paste-from-office/src/filters/utils.js

@@ -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 `&nbsp; ` 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 / &nbsp; 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 `&nbsp; ` pairs. This prevents spaces from being removed during further DOM/View processing
 // (see especially {@link module:engine/view/domconverter~DomConverter#_processDataFromDomText}).

+ 3 - 3
packages/ckeditor5-paste-from-office/tests/_data/spacing/multi-line/normalized.safari.word2016.html

@@ -1,11 +1,11 @@
 <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 15.693333625793457px; font-size: 11pt; font-family: Calibri, sans-serif; caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; text-indent: 5.25pt;">
-	<span>2Foo<span class="Apple-converted-space"> </span><span>  </span>3Bar4<span class="Apple-converted-space"> </span><span>   </span><o:p></o:p></span>
+	<span>2Foo &nbsp; 3Bar4 &nbsp; &nbsp;<o:p></o:p></span>
 </p>
 
 <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 15.693333625793457px; font-size: 11pt; font-family: Calibri, sans-serif; caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; text-indent: 5.25pt;">
-	<span>03<span>  <span class="Apple-converted-space"> </span></span><o:p></o:p></span>
+	<span>03 &nbsp;&nbsp;<o:p></o:p></span>
 </p>
 
 <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 15.693333625793457px; font-size: 11pt; font-family: Calibri, sans-serif; caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; text-indent: 5.25pt;">
-	<span><span> <span class="Apple-converted-space"> </span></span>21<span class="Apple-converted-space"> </span><o:p></o:p></span>
+	<span>&nbsp; 21&nbsp;<o:p></o:p></span>
 </p>

+ 1 - 3
packages/ckeditor5-paste-from-office/tests/_data/spacing/single-line/normalized.safari.word2016.html

@@ -1,5 +1,3 @@
 <p class="MsoNormal" style="margin: 0cm 0cm 8pt; line-height: 15.693333625793457px; font-size: 11pt; font-family: Calibri, sans-serif; caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;">
-	<span>
-		<span>  </span>2Foo <span>  </span>3Bar4 <span>   </span><o:p></o:p>
-	</span>
+	<span>&nbsp; 2Foo &nbsp; 3Bar4 &nbsp; &nbsp;<o:p></o:p></span>
 </p>