mswordnormalizer.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. constructor( document ) {
  20. /**
  21. * @readonly
  22. * @type {module:engine/view/document~Document}
  23. */
  24. this.document = document;
  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, stylesString } = parseHtml( data.dataTransfer.getData( 'text/html' ), this.document.stylesProcessor );
  37. transformListItemLikeElementsIntoLists( body, stylesString );
  38. replaceImagesSourceWithBase64( body, data.dataTransfer.getData( 'text/rtf' ) );
  39. data.content = body;
  40. }
  41. }