mswordnormalizer.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. 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. */
  8. import { parseHtml } from '../filters/parse';
  9. import { transformListItemLikeElementsIntoLists } from '../filters/list';
  10. import { replaceImagesSourceWithBase64 } from '../filters/image';
  11. const msWordMatch1 = /<meta\s*name="?generator"?\s*content="?microsoft\s*word\s*\d+"?\/?>/i;
  12. const msWordMatch2 = /xmlns:o="urn:schemas-microsoft-com/i;
  13. /**
  14. * Normalizer for the content pasted from Microsoft Word.
  15. *
  16. * @implements module:paste-from-office/normalizer~Normalizer
  17. */
  18. export default class MSWordNormalizer {
  19. /**
  20. * Creates a new `MSWordNormalizer` instance.
  21. *
  22. * @param {module:engine/view/document~Document} document View document.
  23. */
  24. constructor( document ) {
  25. /**
  26. * @readonly
  27. * @type {module:engine/view/document~Document}
  28. */
  29. this.document = document;
  30. }
  31. /**
  32. * @inheritDoc
  33. */
  34. isActive( htmlString ) {
  35. return msWordMatch1.test( htmlString ) || msWordMatch2.test( htmlString );
  36. }
  37. /**
  38. * @inheritDoc
  39. */
  40. execute( data ) {
  41. const { body, stylesString } = parseHtml( data.dataTransfer.getData( 'text/html' ), this.document.stylesProcessor );
  42. transformListItemLikeElementsIntoLists( body, stylesString );
  43. replaceImagesSourceWithBase64( body, data.dataTransfer.getData( 'text/rtf' ) );
  44. data.content = body;
  45. }
  46. }