UNPKG

1.57 kBJavaScriptView Raw
1/**
2 * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
3 * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4 */
5/**
6 * @module paste-from-office/normalizers/mswordnormalizer
7 */
8import { transformListItemLikeElementsIntoLists } from '../filters/list.js';
9import { replaceImagesSourceWithBase64 } from '../filters/image.js';
10import removeMSAttributes from '../filters/removemsattributes.js';
11const msWordMatch1 = /<meta\s*name="?generator"?\s*content="?microsoft\s*word\s*\d+"?\/?>/i;
12const msWordMatch2 = /xmlns:o="urn:schemas-microsoft-com/i;
13/**
14 * Normalizer for the content pasted from Microsoft Word.
15 */
16export default class MSWordNormalizer {
17 /**
18 * Creates a new `MSWordNormalizer` instance.
19 *
20 * @param document View document.
21 */
22 constructor(document, hasMultiLevelListPlugin = false) {
23 this.document = document;
24 this.hasMultiLevelListPlugin = hasMultiLevelListPlugin;
25 }
26 /**
27 * @inheritDoc
28 */
29 isActive(htmlString) {
30 return msWordMatch1.test(htmlString) || msWordMatch2.test(htmlString);
31 }
32 /**
33 * @inheritDoc
34 */
35 execute(data) {
36 const { body: documentFragment, stylesString } = data._parsedData;
37 transformListItemLikeElementsIntoLists(documentFragment, stylesString, this.hasMultiLevelListPlugin);
38 replaceImagesSourceWithBase64(documentFragment, data.dataTransfer.getData('text/rtf'));
39 removeMSAttributes(documentFragment);
40 data.content = documentFragment;
41 }
42}