Mateusz Samsel 6 роки тому
батько
коміт
57ef71d5ff

+ 37 - 0
packages/ckeditor5-paste-from-office/src/normalizer/googledocsnormalizer.js

@@ -0,0 +1,37 @@
+/**
+ * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
+ */
+
+/**
+ * @module paste-from-office/normalizer/googledocsnormalizer
+ */
+
+import { removeBoldTagWrapper } from '../filters/common';
+import UpcastWriter from '@ckeditor/ckeditor5-engine/src/view/upcastwriter';
+
+/**
+ * Normalizer fixing HTML syntax obtained from Google Docs.
+ *
+ * @implements module:paste-from-office/normalizer~Normalizer
+ */
+export default class GoogleDocsNormalizer {
+	/**
+	 * @inheritDoc
+	 */
+	isActive( htmlString ) {
+		return /id=("|')docs-internal-guid-[-0-9a-f]+("|')/.test( htmlString );
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	exec( data ) {
+		const writer = new UpcastWriter();
+
+		removeBoldTagWrapper( {
+			writer,
+			documentFragment: data.content
+		} );
+	}
+}

+ 53 - 0
packages/ckeditor5-paste-from-office/src/normalizer/mswordnormalizer.js

@@ -0,0 +1,53 @@
+/**
+ * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
+ */
+
+/**
+ * @module paste-from-office/normalizer/mswordnormalizer
+ */
+
+import { parseHtml } from '../filters/parse';
+import { transformListItemLikeElementsIntoLists } from '../filters/list';
+import { replaceImagesSourceWithBase64 } from '../filters/image';
+
+/**
+ * Normalizer fixing HTML syntax obtained from Microsoft Word documents.
+ *
+ * @implements module:paste-from-office/normalizer~Normalizer
+ */
+export default class MSWordNormalizer {
+	/**
+	 * @inheritDoc
+	 */
+	isActive( htmlString ) {
+		return /<meta\s*name="?generator"?\s*content="?microsoft\s*word\s*\d+"?\/?>/i.test( htmlString ) ||
+				/xmlns:o="urn:schemas-microsoft-com/i.test( htmlString );
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	exec( data ) {
+		const html = data.dataTransfer.getData( 'text/html' );
+
+		data.content = normalizeWordInput( html, data.dataTransfer );
+	}
+}
+
+//
+// Normalizes input pasted from Word to format suitable for editor {@link module:engine/model/model~Model}.
+//
+// @private
+// @param {String} input Word input.
+// @param {module:clipboard/datatransfer~DataTransfer} dataTransfer Data transfer instance.
+// @returns {module:engine/view/documentfragment~DocumentFragment} Normalized input.
+//
+function normalizeWordInput( input, dataTransfer ) {
+	const { body, stylesString } = parseHtml( input );
+
+	transformListItemLikeElementsIntoLists( body, stylesString );
+	replaceImagesSourceWithBase64( body, dataTransfer.getData( 'text/rtf' ) );
+
+	return body;
+}