tableclipboard.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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. /* globals document */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  8. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  9. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  10. import TableEditing from '../src/tableediting';
  11. import { assertSelectedCells, modelTable, viewTable } from './_utils/utils';
  12. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  13. import TableClipboard from '../src/tableclipboard';
  14. describe( 'table clipboard', () => {
  15. let editor, model, modelRoot, tableSelection, viewDocument, element;
  16. beforeEach( async () => {
  17. element = document.createElement( 'div' );
  18. document.body.appendChild( element );
  19. editor = await ClassicTestEditor.create( element, {
  20. plugins: [ TableEditing, TableClipboard, Paragraph, Clipboard ]
  21. } );
  22. model = editor.model;
  23. modelRoot = model.document.getRoot();
  24. viewDocument = editor.editing.view.document;
  25. tableSelection = editor.plugins.get( 'TableSelection' );
  26. setModelData( model, modelTable( [
  27. [ '00[]', '01', '02' ],
  28. [ '10', '11', '12' ],
  29. [ '20', '21', '22' ]
  30. ] ) );
  31. } );
  32. afterEach( async () => {
  33. await editor.destroy();
  34. element.remove();
  35. } );
  36. describe( 'Clipboard integration', () => {
  37. describe( 'copy', () => {
  38. it( 'should do nothing for normal selection in table', () => {
  39. const dataTransferMock = createDataTransfer();
  40. const spy = sinon.spy();
  41. viewDocument.on( 'clipboardOutput', spy );
  42. viewDocument.fire( 'copy', {
  43. dataTransfer: dataTransferMock,
  44. preventDefault: sinon.spy()
  45. } );
  46. sinon.assert.calledOnce( spy );
  47. } );
  48. it( 'should copy selected table cells as a standalone table', () => {
  49. const preventDefaultSpy = sinon.spy();
  50. tableSelection._setCellSelection(
  51. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  52. modelRoot.getNodeByPath( [ 0, 1, 2 ] )
  53. );
  54. const data = {
  55. dataTransfer: createDataTransfer(),
  56. preventDefault: preventDefaultSpy
  57. };
  58. viewDocument.fire( 'copy', data );
  59. sinon.assert.calledOnce( preventDefaultSpy );
  60. expect( data.dataTransfer.getData( 'text/html' ) ).to.equal( viewTable( [
  61. [ '01', '02' ],
  62. [ '11', '12' ]
  63. ] ) );
  64. } );
  65. it( 'should trim selected table to a selection rectangle (inner cell with colspan, no colspan after trim)', () => {
  66. setModelData( model, modelTable( [
  67. [ '00[]', '01', '02' ],
  68. [ '10', { contents: '11', colspan: 2 } ],
  69. [ '20', '21', '22' ]
  70. ] ) );
  71. tableSelection._setCellSelection(
  72. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  73. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  74. );
  75. assertClipboardContentOnMethod( 'copy', viewTable( [
  76. [ '00', '01' ],
  77. [ '10', '11' ],
  78. [ '20', '21' ]
  79. ] ) );
  80. } );
  81. it( 'should trim selected table to a selection rectangle (inner cell with colspan, has colspan after trim)', () => {
  82. setModelData( model, modelTable( [
  83. [ '00[]', '01', '02' ],
  84. [ { contents: '10', colspan: 3 } ],
  85. [ '20', '21', '22' ]
  86. ] ) );
  87. tableSelection._setCellSelection(
  88. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  89. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  90. );
  91. assertClipboardContentOnMethod( 'copy', viewTable( [
  92. [ '00', '01' ],
  93. [ { contents: '10', colspan: 2 } ],
  94. [ '20', '21' ]
  95. ] ) );
  96. } );
  97. it( 'should trim selected table to a selection rectangle (inner cell with rowspan, no colspan after trim)', () => {
  98. setModelData( model, modelTable( [
  99. [ '00[]', '01', '02' ],
  100. [ '10', { contents: '11', rowspan: 2 }, '12' ],
  101. [ '20', '21', '22' ]
  102. ] ) );
  103. tableSelection._setCellSelection(
  104. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  105. modelRoot.getNodeByPath( [ 0, 1, 2 ] )
  106. );
  107. assertClipboardContentOnMethod( 'copy', viewTable( [
  108. [ '00', '01', '02' ],
  109. [ '10', '11', '12' ]
  110. ] ) );
  111. } );
  112. it( 'should trim selected table to a selection rectangle (inner cell with rowspan, has rowspan after trim)', () => {
  113. setModelData( model, modelTable( [
  114. [ '00[]', { contents: '01', rowspan: 3 }, '02' ],
  115. [ '10', '12' ],
  116. [ '20', '22' ]
  117. ] ) );
  118. tableSelection._setCellSelection(
  119. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  120. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  121. );
  122. assertClipboardContentOnMethod( 'copy', viewTable( [
  123. [ '00', { contents: '01', rowspan: 2 }, '02' ],
  124. [ '10', '12' ]
  125. ] ) );
  126. } );
  127. it( 'should prepend spanned columns with empty cells (outside cell with colspan)', () => {
  128. setModelData( model, modelTable( [
  129. [ '00[]', '01', '02' ],
  130. [ { contents: '10', colspan: 2 }, '12' ],
  131. [ '20', '21', '22' ]
  132. ] ) );
  133. tableSelection._setCellSelection(
  134. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  135. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  136. );
  137. assertClipboardContentOnMethod( 'copy', viewTable( [
  138. [ '01', '02' ],
  139. [ ' ', '12' ],
  140. [ '21', '22' ]
  141. ] ) );
  142. } );
  143. it( 'should prepend spanned columns with empty cells (outside cell with rowspan)', () => {
  144. setModelData( model, modelTable( [
  145. [ '00[]', { contents: '01', rowspan: 2 }, '02' ],
  146. [ '10', '12' ],
  147. [ '20', '21', '22' ]
  148. ] ) );
  149. tableSelection._setCellSelection(
  150. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  151. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  152. );
  153. assertClipboardContentOnMethod( 'copy', viewTable( [
  154. [ '10', ' ', '12' ],
  155. [ '20', '21', '22' ]
  156. ] ) );
  157. } );
  158. it( 'should fix selected table to a selection rectangle (hardcore case)', () => {
  159. // This test check how previous simple rules run together (mixed prepending and trimming).
  160. // In the example below a selection is set from cell "32" to "88"
  161. //
  162. // Input table: Copied table:
  163. //
  164. // +----+----+----+----+----+----+----+----+----+
  165. // | 00 | 01 | 02 | 03 | 04 | 06 | 07 | 08 |
  166. // +----+----+ +----+ +----+----+----+
  167. // | 10 | 11 | | 13 | | 16 | 17 | 18 |
  168. // +----+----+ +----+ +----+----+----+ +----+----+----+---------+----+----+
  169. // | 20 | 21 | | 23 | | 26 | | 21 | | 23 | | | 26 | |
  170. // +----+----+ +----+ +----+----+----+ +----+----+----+----+----+----+----+
  171. // | 30 | 31 | | 33 | | 36 | 37 | | 31 | | 33 | | | 36 | 37 |
  172. // +----+----+----+----+ +----+----+----+ +----+----+----+----+----+----+----+
  173. // | 40 | | 46 | 47 | 48 | | | | | | | 46 | 47 |
  174. // +----+----+----+----+ +----+----+----+ ==> +----+----+----+----+----+----+----+
  175. // | 50 | 51 | 52 | 53 | | 56 | 57 | 58 | | 51 | 52 | 53 | | | 56 | 57 |
  176. // +----+----+----+----+----+----+ +----+----+ +----+----+----+----+----+----+----+
  177. // | 60 | 61 | 64 | 65 | | 67 | 68 | | 61 | | | 64 | 65 | | 67 |
  178. // +----+----+----+----+----+----+ +----+----+ +----+----+----+----+----+----+----+
  179. // | 70 | 71 | 72 | 73 | 74 | 75 | | 77 | 78 | | 71 | 72 | 73 | 74 | 75 | | 77 |
  180. // +----+ +----+----+----+----+ +----+----+ +----+----+----+----+----+----+----+
  181. // | 80 | | 82 | 83 | 84 | 85 | | 87 | 88 |
  182. // +----+----+----+----+----+----+----+----+----+
  183. //
  184. setModelData( model, modelTable( [
  185. [ '00', '01', { contents: '02', rowspan: 4 }, '03', { contents: '04', colspan: 2, rowspan: 7 }, '07', '07', '08' ],
  186. [ '10', '11', '13', '17', '17', '18' ],
  187. [ '20', '21', '23', { contents: '27', colspan: 3 } ],
  188. [ '30', '31', '33', '37', { contents: '37', colspan: 2 } ],
  189. [ { contents: '40', colspan: 4 }, '47', '47', '48' ],
  190. [ '50', '51', '52', '53', { contents: '57', rowspan: 4 }, '57', '58' ],
  191. [ '60', { contents: '61', colspan: 3 }, '67', '68' ],
  192. [ '70', { contents: '71', rowspan: 2 }, '72', '73', '74', '75', '77', '78' ],
  193. [ '80', '82', '83', '84', '85', '87', '88' ]
  194. ] ) );
  195. tableSelection._setCellSelection(
  196. modelRoot.getNodeByPath( [ 0, 2, 1 ] ),
  197. modelRoot.getNodeByPath( [ 0, 7, 6 ] )
  198. );
  199. assertClipboardContentOnMethod( 'copy', viewTable( [
  200. [ '21', ' ', '23', ' ', ' ', { contents: '27', colspan: 2 } ],
  201. [ '31', ' ', '33', ' ', ' ', '37', '37' ],
  202. [ ' ', ' ', ' ', ' ', ' ', '47', '47' ],
  203. [ '51', '52', '53', ' ', ' ', { contents: '57', rowspan: 3 }, '57' ],
  204. [ { contents: '61', colspan: 3 }, ' ', ' ', ' ', '67' ],
  205. [ '71', '72', '73', '74', '75', '77' ]
  206. ] ) );
  207. } );
  208. it( 'should update table heading attributes (selection with headings)', () => {
  209. setModelData( model, modelTable( [
  210. [ '00', '01', '02', '03', '04' ],
  211. [ '10', '11', '12', '13', '14' ],
  212. [ '20', '21', '22', '23', '24' ],
  213. [ '30', '31', '32', '33', '34' ],
  214. [ '40', '41', '42', '43', '44' ]
  215. ], { headingRows: 3, headingColumns: 2 } ) );
  216. tableSelection._setCellSelection(
  217. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  218. modelRoot.getNodeByPath( [ 0, 3, 3 ] )
  219. );
  220. assertClipboardContentOnMethod( 'copy', viewTable( [
  221. [ '11', '12', '13' ],
  222. [ '21', '22', '23' ],
  223. [ { contents: '31', isHeading: true }, '32', '33' ] // TODO: bug in viewTable
  224. ], { headingRows: 2, headingColumns: 1 } ) );
  225. } );
  226. it( 'should update table heading attributes (selection without headings)', () => {
  227. setModelData( model, modelTable( [
  228. [ '00', '01', '02', '03', '04' ],
  229. [ '10', '11', '12', '13', '14' ],
  230. [ '20', '21', '22', '23', '24' ],
  231. [ '30', '31', '32', '33', '34' ],
  232. [ '40', '41', '42', '43', '44' ]
  233. ], { headingRows: 3, headingColumns: 2 } ) );
  234. tableSelection._setCellSelection(
  235. modelRoot.getNodeByPath( [ 0, 3, 2 ] ),
  236. modelRoot.getNodeByPath( [ 0, 4, 4 ] )
  237. );
  238. assertClipboardContentOnMethod( 'copy', viewTable( [
  239. [ '32', '33', '34' ],
  240. [ '42', '43', '44' ]
  241. ] ) );
  242. } );
  243. } );
  244. describe( 'cut', () => {
  245. it( 'should not block clipboardOutput if no multi-cell selection', () => {
  246. setModelData( model, modelTable( [
  247. [ '[00]', '01', '02' ],
  248. [ '10', '11', '12' ],
  249. [ '20', '21', '22' ]
  250. ] ) );
  251. const dataTransferMock = createDataTransfer();
  252. viewDocument.fire( 'cut', {
  253. dataTransfer: dataTransferMock,
  254. preventDefault: sinon.spy()
  255. } );
  256. expect( dataTransferMock.getData( 'text/html' ) ).to.equal( '00' );
  257. } );
  258. it( 'should be preventable', () => {
  259. tableSelection._setCellSelection(
  260. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  261. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  262. );
  263. viewDocument.on( 'clipboardOutput', evt => evt.stop(), { priority: 'high' } );
  264. viewDocument.fire( 'cut', {
  265. dataTransfer: createDataTransfer(),
  266. preventDefault: sinon.spy()
  267. } );
  268. assertEqualMarkup( getModelData( model ), modelTable( [
  269. [ { contents: '00', isSelected: true }, { contents: '01', isSelected: true }, '02' ],
  270. [ { contents: '10', isSelected: true }, { contents: '11', isSelected: true }, '12' ],
  271. [ '20', '21', '22' ]
  272. ] ) );
  273. } );
  274. it( 'is clears selected table cells', () => {
  275. const spy = sinon.spy();
  276. viewDocument.on( 'clipboardOutput', spy );
  277. tableSelection._setCellSelection(
  278. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  279. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  280. );
  281. viewDocument.fire( 'cut', {
  282. dataTransfer: createDataTransfer(),
  283. preventDefault: sinon.spy()
  284. } );
  285. assertEqualMarkup( getModelData( model ), modelTable( [
  286. [ '', '', '02' ],
  287. [ '', '[]', '12' ],
  288. [ '20', '21', '22' ]
  289. ] ) );
  290. } );
  291. it( 'should copy selected table cells as a standalone table', () => {
  292. const preventDefaultSpy = sinon.spy();
  293. tableSelection._setCellSelection(
  294. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  295. modelRoot.getNodeByPath( [ 0, 1, 2 ] )
  296. );
  297. const data = {
  298. dataTransfer: createDataTransfer(),
  299. preventDefault: preventDefaultSpy
  300. };
  301. viewDocument.fire( 'cut', data );
  302. sinon.assert.calledOnce( preventDefaultSpy );
  303. expect( data.dataTransfer.getData( 'text/html' ) ).to.equal( viewTable( [
  304. [ '01', '02' ],
  305. [ '11', '12' ]
  306. ] ) );
  307. } );
  308. it( 'should be disabled in a readonly mode', () => {
  309. const preventDefaultStub = sinon.stub();
  310. editor.isReadOnly = true;
  311. tableSelection._setCellSelection(
  312. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  313. modelRoot.getNodeByPath( [ 0, 1, 2 ] )
  314. );
  315. const data = {
  316. dataTransfer: createDataTransfer(),
  317. preventDefault: preventDefaultStub
  318. };
  319. viewDocument.fire( 'cut', data );
  320. editor.isReadOnly = false;
  321. expect( data.dataTransfer.getData( 'text/html' ) ).to.be.undefined;
  322. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  323. [ '00', '01', '02' ],
  324. [ '10', '11', '12' ],
  325. [ '20', '21', '22' ]
  326. ] ) );
  327. sinon.assert.calledOnce( preventDefaultStub );
  328. } );
  329. } );
  330. describe( 'paste', () => {
  331. beforeEach( () => {
  332. setModelData( model, modelTable( [
  333. [ '00[]', '01', '02', '03' ],
  334. [ '10', '11', '12', '13' ],
  335. [ '20', '21', '22', '23' ],
  336. [ '30', '31', '32', '33' ]
  337. ] ) );
  338. } );
  339. describe( 'pasted table is equal ot selected area', () => {
  340. it( 'should be disabled in a readonly mode', () => {
  341. editor.isReadOnly = true;
  342. tableSelection._setCellSelection(
  343. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  344. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  345. );
  346. const data = pasteTable( [
  347. [ 'aa', 'ab' ],
  348. [ 'ba', 'bb' ]
  349. ] );
  350. editor.isReadOnly = false;
  351. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  352. [ '00', '01', '02', '03' ],
  353. [ '10', '11', '12', '13' ],
  354. [ '20', '21', '22', '23' ],
  355. [ '30', '31', '32', '33' ]
  356. ] ) );
  357. sinon.assert.calledOnce( data.preventDefault );
  358. } );
  359. it( 'should allow normal paste if no table cells are selected', () => {
  360. const data = {
  361. dataTransfer: createDataTransfer(),
  362. preventDefault: sinon.spy(),
  363. stopPropagation: sinon.spy()
  364. };
  365. data.dataTransfer.setData( 'text/html', '<p>foo</p>' );
  366. viewDocument.fire( 'paste', data );
  367. editor.isReadOnly = false;
  368. assertEqualMarkup( getModelData( model ), modelTable( [
  369. [ '00foo[]', '01', '02', '03' ],
  370. [ '10', '11', '12', '13' ],
  371. [ '20', '21', '22', '23' ],
  372. [ '30', '31', '32', '33' ]
  373. ] ) );
  374. } );
  375. it( 'pastes simple table to a simple table fragment - at the beginning of a table', () => {
  376. tableSelection._setCellSelection(
  377. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  378. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  379. );
  380. pasteTable( [
  381. [ 'aa', 'ab' ],
  382. [ 'ba', 'bb' ]
  383. ] );
  384. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  385. [ 'aa', 'ab', '02', '03' ],
  386. [ 'ba', 'bb', '12', '13' ],
  387. [ '20', '21', '22', '23' ],
  388. [ '30', '31', '32', '33' ]
  389. ] ) );
  390. assertSelectedCells( model, [
  391. [ 1, 1, 0, 0 ],
  392. [ 1, 1, 0, 0 ],
  393. [ 0, 0, 0, 0 ],
  394. [ 0, 0, 0, 0 ]
  395. ] );
  396. } );
  397. it( 'pastes simple table to a simple table fragment - at the end of a table', () => {
  398. tableSelection._setCellSelection(
  399. modelRoot.getNodeByPath( [ 0, 2, 2 ] ),
  400. modelRoot.getNodeByPath( [ 0, 3, 3 ] )
  401. );
  402. pasteTable( [
  403. [ 'aa', 'ab' ],
  404. [ 'ba', 'bb' ]
  405. ] );
  406. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  407. [ '00', '01', '02', '03' ],
  408. [ '10', '11', '12', '13' ],
  409. [ '20', '21', 'aa', 'ab' ],
  410. [ '30', '31', 'ba', 'bb' ]
  411. ] ) );
  412. assertSelectedCells( model, [
  413. [ 0, 0, 0, 0 ],
  414. [ 0, 0, 0, 0 ],
  415. [ 0, 0, 1, 1 ],
  416. [ 0, 0, 1, 1 ]
  417. ] );
  418. } );
  419. it( 'pastes simple table to a simple table fragment - in the middle of a table', () => {
  420. tableSelection._setCellSelection(
  421. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  422. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  423. );
  424. pasteTable( [
  425. [ 'aa', 'ab' ],
  426. [ 'ba', 'bb' ]
  427. ] );
  428. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  429. [ '00', '01', '02', '03' ],
  430. [ '10', 'aa', 'ab', '13' ],
  431. [ '20', 'ba', 'bb', '23' ],
  432. [ '30', '31', '32', '33' ]
  433. ] ) );
  434. assertSelectedCells( model, [
  435. [ 0, 0, 0, 0 ],
  436. [ 0, 1, 1, 0 ],
  437. [ 0, 1, 1, 0 ],
  438. [ 0, 0, 0, 0 ]
  439. ] );
  440. } );
  441. it( 'pastes simple table to a simple table fragment - whole table selected', () => {
  442. tableSelection._setCellSelection(
  443. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  444. modelRoot.getNodeByPath( [ 0, 3, 3 ] )
  445. );
  446. pasteTable( [
  447. [ 'aa', 'ab', 'ac', 'ad' ],
  448. [ 'ba', 'bb', 'bc', 'bd' ],
  449. [ 'ca', 'cb', 'cc', 'cd' ],
  450. [ 'da', 'db', 'dc', 'dd' ]
  451. ] );
  452. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  453. [ 'aa', 'ab', 'ac', 'ad' ],
  454. [ 'ba', 'bb', 'bc', 'bd' ],
  455. [ 'ca', 'cb', 'cc', 'cd' ],
  456. [ 'da', 'db', 'dc', 'dd' ]
  457. ] ) );
  458. assertSelectedCells( model, [
  459. [ 1, 1, 1, 1 ],
  460. [ 1, 1, 1, 1 ],
  461. [ 1, 1, 1, 1 ],
  462. [ 1, 1, 1, 1 ]
  463. ] );
  464. } );
  465. } );
  466. } );
  467. } );
  468. function assertClipboardContentOnMethod( method, expectedViewTable ) {
  469. const data = {
  470. dataTransfer: createDataTransfer(),
  471. preventDefault: sinon.spy()
  472. };
  473. viewDocument.fire( method, data );
  474. expect( data.dataTransfer.getData( 'text/html' ) ).to.equal( expectedViewTable );
  475. }
  476. function pasteTable( tableData ) {
  477. const data = {
  478. dataTransfer: createDataTransfer(),
  479. preventDefault: sinon.spy(),
  480. stopPropagation: sinon.spy()
  481. };
  482. data.dataTransfer.setData( 'text/html', viewTable( tableData ) );
  483. viewDocument.fire( 'paste', data );
  484. return data;
  485. }
  486. function createDataTransfer() {
  487. const store = new Map();
  488. return {
  489. setData( type, data ) {
  490. store.set( type, data );
  491. },
  492. getData( type ) {
  493. return store.get( type );
  494. }
  495. };
  496. }
  497. } );