spacing.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  8. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  9. import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
  10. import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
  11. import Underline from '@ckeditor/ckeditor5-basic-styles/src/underline';
  12. import PasteFromOffice from '../../../src/pastefromoffice';
  13. import { setData, stringify } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  14. import { pasteHtml } from '../../_utils/utils';
  15. import simple from '../../_data/spacing/simple/input.word2016.html';
  16. import singleLine from '../../_data/spacing/single-line/input.word2016.html';
  17. import multiLine from '../../_data/spacing/multi-line/input.word2016.html';
  18. describe( 'Spacing – integration', () => {
  19. let element, editor, insertedModel;
  20. before( () => {
  21. element = document.createElement( 'div' );
  22. document.body.appendChild( element );
  23. return ClassicTestEditor
  24. .create( element, { plugins: [ Clipboard, Paragraph, Bold, Italic, Underline, PasteFromOffice ] } )
  25. .then( editorInstance => {
  26. editor = editorInstance;
  27. const model = editor.model;
  28. const insertContent = model.insertContent;
  29. sinon.stub( editor.model, 'insertContent' ).callsFake( ( content, selection ) => {
  30. // Save model string representation now as it may change after `insertContent()` function call
  31. // so accessing it later may not work as it may have empty/changed structure.
  32. insertedModel = stringify( content );
  33. insertContent.call( model, content, selection );
  34. } );
  35. } );
  36. } );
  37. beforeEach( () => {
  38. setData( editor.model, '<paragraph>[]</paragraph>' );
  39. } );
  40. afterEach( () => {
  41. insertedModel = null;
  42. } );
  43. after( () => {
  44. sinon.restore();
  45. editor.destroy();
  46. element.remove();
  47. } );
  48. // Pastes (after cleaning up garbage markup):
  49. //
  50. // <p><span>Foo Bar </span></p>
  51. //
  52. // which should result in the same output as pasting:
  53. //
  54. // <p>Foo Bar </p>
  55. it( 'pastes line with single space', () => {
  56. const expectedModel = '<paragraph>Foo Bar </paragraph>';
  57. expectContent( simple, expectedModel );
  58. } );
  59. // Pastes (after cleaning up garbage markup):
  60. //
  61. // <p><span>
  62. // <span style='mso-spacerun:yes'> </span>
  63. // 2Foo <span style='mso-spacerun:yes'> </span>
  64. // 3Bar4 <span style='mso-spacerun:yes'> </span><o:p></o:p>
  65. // </span></p>
  66. //
  67. // which should result in the same output as pasting:
  68. //
  69. // <p> 2Foo 3Bar4 </p>
  70. it( 'pastes single line with multiple spaces', () => {
  71. const expectedModel = '<paragraph> 2Foo 3Bar4 </paragraph>';
  72. expectContent( singleLine, expectedModel );
  73. } );
  74. // Pastes (after cleaning up garbage markup):
  75. //
  76. // <p><span>2Foo 3Bar4 <o:p></o:p></span></p>
  77. // <p><span>03 <o:p></o:p></span></p>
  78. // <p><span> 21 <o:p></o:p></span></p>
  79. //
  80. // which should result in the same output as pasting:
  81. //
  82. // <p>2Foo 3Bar4 </p>
  83. // <p>03 </p>
  84. // <p> 21 </p>
  85. it( 'pastes multiple lines with multiple spaces', () => {
  86. const expectedModel = '<paragraph>2Foo 3Bar4 </paragraph>' +
  87. '<paragraph>03 </paragraph>' +
  88. '<paragraph> 21 </paragraph>';
  89. expectContent( multiLine, expectedModel );
  90. } );
  91. function expectContent( input, expectedModel ) {
  92. pasteHtml( editor, input );
  93. expect( insertedModel.replace( /\u00A0/g, '#' ).replace( /&nbsp;/g, '#' ) )
  94. .to.equal( expectedModel.replace( /\u00A0/g, '#' ).replace( /&nbsp;/g, '#' ) );
  95. }
  96. } );