8
0

tableclipboard.js 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  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 to the 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. describe( 'no spans', () => {
  376. it( 'handles simple table paste to a simple table fragment - at the beginning of a table', () => {
  377. tableSelection.setCellSelection(
  378. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  379. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  380. );
  381. pasteTable( [
  382. [ 'aa', 'ab' ],
  383. [ 'ba', 'bb' ]
  384. ] );
  385. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  386. [ 'aa', 'ab', '02', '03' ],
  387. [ 'ba', 'bb', '12', '13' ],
  388. [ '20', '21', '22', '23' ],
  389. [ '30', '31', '32', '33' ]
  390. ] ) );
  391. assertSelectedCells( model, [
  392. [ 1, 1, 0, 0 ],
  393. [ 1, 1, 0, 0 ],
  394. [ 0, 0, 0, 0 ],
  395. [ 0, 0, 0, 0 ]
  396. ] );
  397. } );
  398. it( 'handles simple table paste to a simple table fragment - at the end of a table', () => {
  399. tableSelection.setCellSelection(
  400. modelRoot.getNodeByPath( [ 0, 2, 2 ] ),
  401. modelRoot.getNodeByPath( [ 0, 3, 3 ] )
  402. );
  403. pasteTable( [
  404. [ 'aa', 'ab' ],
  405. [ 'ba', 'bb' ]
  406. ] );
  407. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  408. [ '00', '01', '02', '03' ],
  409. [ '10', '11', '12', '13' ],
  410. [ '20', '21', 'aa', 'ab' ],
  411. [ '30', '31', 'ba', 'bb' ]
  412. ] ) );
  413. assertSelectedCells( model, [
  414. [ 0, 0, 0, 0 ],
  415. [ 0, 0, 0, 0 ],
  416. [ 0, 0, 1, 1 ],
  417. [ 0, 0, 1, 1 ]
  418. ] );
  419. } );
  420. it( 'handles simple table paste to a simple table fragment - in the middle of a table', () => {
  421. tableSelection.setCellSelection(
  422. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  423. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  424. );
  425. pasteTable( [
  426. [ 'aa', 'ab' ],
  427. [ 'ba', 'bb' ]
  428. ] );
  429. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  430. [ '00', '01', '02', '03' ],
  431. [ '10', 'aa', 'ab', '13' ],
  432. [ '20', 'ba', 'bb', '23' ],
  433. [ '30', '31', '32', '33' ]
  434. ] ) );
  435. assertSelectedCells( model, [
  436. [ 0, 0, 0, 0 ],
  437. [ 0, 1, 1, 0 ],
  438. [ 0, 1, 1, 0 ],
  439. [ 0, 0, 0, 0 ]
  440. ] );
  441. } );
  442. it( 'handles simple row paste to a simple row fragment - in the middle of a table', () => {
  443. tableSelection.setCellSelection(
  444. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  445. modelRoot.getNodeByPath( [ 0, 1, 2 ] )
  446. );
  447. pasteTable( [
  448. [ 'aa', 'ab' ]
  449. ] );
  450. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  451. [ '00', '01', '02', '03' ],
  452. [ '10', 'aa', 'ab', '13' ],
  453. [ '20', '21', '22', '23' ],
  454. [ '30', '31', '32', '33' ]
  455. ] ) );
  456. assertSelectedCells( model, [
  457. [ 0, 0, 0, 0 ],
  458. [ 0, 1, 1, 0 ],
  459. [ 0, 0, 0, 0 ],
  460. [ 0, 0, 0, 0 ]
  461. ] );
  462. } );
  463. it( 'handles simple column paste to a simple column fragment - in the middle of a table', () => {
  464. tableSelection.setCellSelection(
  465. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  466. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  467. );
  468. pasteTable( [
  469. [ 'aa' ],
  470. [ 'ba' ]
  471. ] );
  472. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  473. [ '00', '01', '02', '03' ],
  474. [ '10', 'aa', '12', '13' ],
  475. [ '20', 'ba', '22', '23' ],
  476. [ '30', '31', '32', '33' ]
  477. ] ) );
  478. assertSelectedCells( model, [
  479. [ 0, 0, 0, 0 ],
  480. [ 0, 1, 0, 0 ],
  481. [ 0, 1, 0, 0 ],
  482. [ 0, 0, 0, 0 ]
  483. ] );
  484. } );
  485. it( 'handles simple table paste to a simple table fragment - whole table selected', () => {
  486. tableSelection.setCellSelection(
  487. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  488. modelRoot.getNodeByPath( [ 0, 3, 3 ] )
  489. );
  490. pasteTable( [
  491. [ 'aa', 'ab', 'ac', 'ad' ],
  492. [ 'ba', 'bb', 'bc', 'bd' ],
  493. [ 'ca', 'cb', 'cc', 'cd' ],
  494. [ 'da', 'db', 'dc', 'dd' ]
  495. ] );
  496. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  497. [ 'aa', 'ab', 'ac', 'ad' ],
  498. [ 'ba', 'bb', 'bc', 'bd' ],
  499. [ 'ca', 'cb', 'cc', 'cd' ],
  500. [ 'da', 'db', 'dc', 'dd' ]
  501. ] ) );
  502. assertSelectedCells( model, [
  503. [ 1, 1, 1, 1 ],
  504. [ 1, 1, 1, 1 ],
  505. [ 1, 1, 1, 1 ],
  506. [ 1, 1, 1, 1 ]
  507. ] );
  508. } );
  509. } );
  510. describe( 'pasted table has spans', () => {
  511. it( 'handles pasting table that has cell with colspan', () => {
  512. tableSelection.setCellSelection(
  513. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  514. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  515. );
  516. pasteTable( [
  517. [ { colspan: 2, contents: 'aa' } ],
  518. [ 'ba', 'bb' ]
  519. ] );
  520. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  521. [ '00', '01', '02', '03' ],
  522. [ '10', { colspan: 2, contents: 'aa' }, '13' ],
  523. [ '20', 'ba', 'bb', '23' ],
  524. [ '30', '31', '32', '33' ]
  525. ] ) );
  526. /* eslint-disable no-multi-spaces */
  527. assertSelectedCells( model, [
  528. [ 0, 0, 0, 0 ],
  529. [ 0, 1, 0 ],
  530. [ 0, 1, 1, 0 ],
  531. [ 0, 0, 0, 0 ]
  532. ] );
  533. /* eslint-enable no-multi-spaces */
  534. } );
  535. it( 'handles pasting table that has many cells with various colspan', () => {
  536. tableSelection.setCellSelection(
  537. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  538. modelRoot.getNodeByPath( [ 0, 3, 2 ] )
  539. );
  540. pasteTable( [
  541. [ 'aa', { colspan: 2, contents: 'ab' } ],
  542. [ { colspan: 3, contents: 'ba' } ],
  543. [ 'ca', 'cb', 'cc' ],
  544. [ { colspan: 2, contents: 'da' }, 'dc' ]
  545. ] );
  546. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  547. [ 'aa', { colspan: 2, contents: 'ab' }, '03' ],
  548. [ { colspan: 3, contents: 'ba' }, '13' ],
  549. [ 'ca', 'cb', 'cc', '23' ],
  550. [ { colspan: 2, contents: 'da' }, 'dc', '33' ]
  551. ] ) );
  552. /* eslint-disable no-multi-spaces */
  553. assertSelectedCells( model, [
  554. [ 1, 1, 0 ],
  555. [ 1, 0 ],
  556. [ 1, 1, 1, 0 ],
  557. [ 1, 1, 0 ]
  558. ] );
  559. /* eslint-enable no-multi-spaces */
  560. } );
  561. it( 'handles pasting table that has cell with rowspan', () => {
  562. tableSelection.setCellSelection(
  563. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  564. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  565. );
  566. pasteTable( [
  567. [ { rowspan: 2, contents: 'aa' }, 'ab' ],
  568. [ 'bb' ]
  569. ] );
  570. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  571. [ '00', '01', '02', '03' ],
  572. [ '10', { rowspan: 2, contents: 'aa' }, 'ab', '13' ],
  573. [ '20', 'bb', '23' ],
  574. [ '30', '31', '32', '33' ]
  575. ] ) );
  576. /* eslint-disable no-multi-spaces */
  577. assertSelectedCells( model, [
  578. [ 0, 0, 0, 0 ],
  579. [ 0, 1, 1, 0 ],
  580. [ 0, 1, 0 ],
  581. [ 0, 0, 0, 0 ]
  582. ] );
  583. /* eslint-enable no-multi-spaces */
  584. } );
  585. it( 'handles pasting table that has many cells with various rowspan', () => {
  586. tableSelection.setCellSelection(
  587. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  588. modelRoot.getNodeByPath( [ 0, 2, 3 ] )
  589. );
  590. pasteTable( [
  591. [ 'aa', { rowspan: 3, contents: 'ab' }, { rowspan: 2, contents: 'ac' }, 'ad' ],
  592. [ { rowspan: 2, contents: 'ba' }, 'bd' ],
  593. [ 'cc', 'cd' ]
  594. ] );
  595. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  596. [ 'aa', { rowspan: 3, contents: 'ab' }, { rowspan: 2, contents: 'ac' }, 'ad' ],
  597. [ { rowspan: 2, contents: 'ba' }, 'bd' ],
  598. [ 'cc', 'cd' ],
  599. [ '30', '31', '32', '33' ]
  600. ] ) );
  601. /* eslint-disable no-multi-spaces */
  602. assertSelectedCells( model, [
  603. [ 1, 1, 1, 1 ],
  604. [ 1, 1 ],
  605. [ 1, 1 ],
  606. [ 0, 0, 0 ]
  607. ] );
  608. /* eslint-enable no-multi-spaces */
  609. } );
  610. it( 'handles pasting multi-spanned table', () => {
  611. setModelData( model, modelTable( [
  612. [ '00', '01', '02', '03', '04', '05' ],
  613. [ '10', '11', '12', '13', '14', '15' ],
  614. [ '20', '21', '22', '23', '24', '25' ],
  615. [ '30', '31', '32', '33', '34', '35' ],
  616. [ '40', '41', '42', '43', '44', '45' ]
  617. ] ) );
  618. tableSelection.setCellSelection(
  619. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  620. modelRoot.getNodeByPath( [ 0, 3, 4 ] )
  621. );
  622. // +----+----+----+----+----+
  623. // | aa | ac | ad | ae |
  624. // +----+----+----+----+ +
  625. // | ba | bb | |
  626. // +----+ +----+
  627. // | ca | | ce |
  628. // + +----+----+----+----+
  629. // | | db | dc | dd |
  630. // +----+----+----+----+----+
  631. pasteTable( [
  632. [ { contents: 'aa', colspan: 2 }, 'ac', 'ad', { contents: 'ae', rowspan: 2 } ],
  633. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 } ],
  634. [ { contents: 'ca', rowspan: 2 }, 'ce' ],
  635. [ 'db', 'dc', { contents: 'dd', colspan: 2 } ]
  636. ] );
  637. // +----+----+----+----+----+----+
  638. // | aa | ac | ad | ae | 05 |
  639. // +----+----+----+----+ +----+
  640. // | ba | bb | | 15 |
  641. // +----+ +----+----+
  642. // | ca | | ce | 25 |
  643. // + +----+----+----+----+----+
  644. // | | db | dc | dd | 35 |
  645. // +----+----+----+----+----+----+
  646. // | 40 | 41 | 42 | 43 | 44 | 45 |
  647. // +----+----+----+----+----+----+
  648. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  649. [ { contents: 'aa', colspan: 2 }, 'ac', 'ad', { contents: 'ae', rowspan: 2 }, '05' ],
  650. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 }, '15' ],
  651. [ { contents: 'ca', rowspan: 2 }, 'ce', '25' ],
  652. [ 'db', 'dc', { contents: 'dd', colspan: 2 }, '35' ],
  653. [ '40', '41', '42', '43', '44', '45' ]
  654. ] ) );
  655. /* eslint-disable no-multi-spaces */
  656. assertSelectedCells( model, [
  657. [ 1, 1, 1, 1, 0 ],
  658. [ 1, 1, 0 ],
  659. [ 1, 1, 0 ],
  660. [ 1, 1, 1, 0 ],
  661. [ 0, 0, 0, 0, 0, 0 ]
  662. ] );
  663. /* eslint-enable no-multi-spaces */
  664. } );
  665. } );
  666. describe( 'content table has spans', () => {
  667. it( 'handles pasting simple table over a table with colspans (no colspan exceeds selection)', () => {
  668. setModelData( model, modelTable( [
  669. [ '00[]', '01', '02', '03' ],
  670. [ { colspan: 3, contents: '10' }, '13' ],
  671. [ { colspan: 2, contents: '20' }, '22', '23' ],
  672. [ '30', '31', { colspan: 2, contents: '31' } ]
  673. ] ) );
  674. tableSelection.setCellSelection(
  675. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  676. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  677. );
  678. pasteTable( [
  679. [ 'aa', 'ab', 'ac' ],
  680. [ 'ba', 'bb', 'bc' ],
  681. [ 'ca', 'cb', 'cc' ]
  682. ] );
  683. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  684. [ 'aa', 'ab', 'ac', '03' ],
  685. [ 'ba', 'bb', 'bc', '13' ],
  686. [ 'ca', 'cb', 'cc', '23' ],
  687. [ '30', '31', { colspan: 2, contents: '31' } ]
  688. ] ) );
  689. /* eslint-disable no-multi-spaces */
  690. assertSelectedCells( model, [
  691. [ 1, 1, 1, 0 ],
  692. [ 1, 1, 1, 0 ],
  693. [ 1, 1, 1, 0 ],
  694. [ 0, 0, 0 ]
  695. ] );
  696. /* eslint-enable no-multi-spaces */
  697. } );
  698. it( 'handles pasting simple table over a table with rowspans (no rowspan exceeds selection)', () => {
  699. setModelData( model, modelTable( [
  700. [ '00', { rowspan: 3, contents: '01' }, { rowspan: 2, contents: '02' }, '03' ],
  701. [ { rowspan: 2, contents: '10' }, '13' ],
  702. [ '22', '23' ],
  703. [ '30', '31', '32', '33' ]
  704. ] ) );
  705. tableSelection.setCellSelection(
  706. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  707. modelRoot.getNodeByPath( [ 0, 2, 0 ] )
  708. );
  709. pasteTable( [
  710. [ 'aa', 'ab', 'ac' ],
  711. [ 'ba', 'bb', 'bc' ],
  712. [ 'ca', 'cb', 'cc' ]
  713. ] );
  714. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  715. [ 'aa', 'ab', 'ac', '03' ],
  716. [ 'ba', 'bb', 'bc', '13' ],
  717. [ 'ca', 'cb', 'cc', '23' ],
  718. [ '30', '31', '32', '33' ]
  719. ] ) );
  720. assertSelectedCells( model, [
  721. [ 1, 1, 1, 0 ],
  722. [ 1, 1, 1, 0 ],
  723. [ 1, 1, 1, 0 ],
  724. [ 0, 0, 0, 0 ]
  725. ] );
  726. } );
  727. it( 'handles pasting simple table over table with multi-spans (no span exceeds selection)', () => {
  728. // +----+----+----+----+----+----+
  729. // | 00 | 02 | 03 | 05 |
  730. // + + + +----+
  731. // | | | | 15 |
  732. // +----+----+----+ +----+
  733. // | 20 | 21 | | 25 |
  734. // + +----+----+----+----+----+
  735. // | | 31 | 32 | 34 | 35 |
  736. // +----+----+----+----+----+----+
  737. // | 40 | 41 | 42 | 43 | 44 | 45 |
  738. // +----+----+----+----+----+----+
  739. setModelData( model, modelTable( [
  740. [
  741. { contents: '00', colspan: 2, rowspan: 2 },
  742. { contents: '02', rowspan: 2 },
  743. { contents: '03', colspan: 2, rowspan: 3 },
  744. '05'
  745. ],
  746. [ '15' ],
  747. [ { contents: '20', rowspan: 2 }, { contents: '21', colspan: 2 }, '25' ],
  748. [ '31', { contents: '32', colspan: 2 }, '34', '35' ],
  749. [ '40', '41', '42', '43', '44', '45' ]
  750. ] ) );
  751. tableSelection.setCellSelection(
  752. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  753. modelRoot.getNodeByPath( [ 0, 3, 2 ] )
  754. );
  755. pasteTable( [
  756. [ 'aa', 'ab', 'ac', 'ad', 'ae' ],
  757. [ 'ba', 'bb', 'bc', 'bd', 'be' ],
  758. [ 'ca', 'cb', 'cc', 'cd', 'ce' ],
  759. [ 'da', 'db', 'dc', 'dd', 'de' ]
  760. ] );
  761. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  762. [ 'aa', 'ab', 'ac', 'ad', 'ae', '05' ],
  763. [ 'ba', 'bb', 'bc', 'bd', 'be', '15' ],
  764. [ 'ca', 'cb', 'cc', 'cd', 'ce', '25' ],
  765. [ 'da', 'db', 'dc', 'dd', 'de', '35' ],
  766. [ '40', '41', '42', '43', '44', '45' ]
  767. ] ) );
  768. assertSelectedCells( model, [
  769. [ 1, 1, 1, 1, 1, 0 ],
  770. [ 1, 1, 1, 1, 1, 0 ],
  771. [ 1, 1, 1, 1, 1, 0 ],
  772. [ 1, 1, 1, 1, 1, 0 ],
  773. [ 0, 0, 0, 0, 0, 0 ]
  774. ] );
  775. } );
  776. it( 'handles pasting table that has cell with colspan (last row in selection is spanned)', () => {
  777. // +----+----+----+----+
  778. // | 00 | 01 | 02 | 03 |
  779. // +----+----+----+----+
  780. // | 10 | 11 | 13 |
  781. // + + +----+
  782. // | | | 23 |
  783. // +----+----+----+----+
  784. // | 30 | 31 | 32 | 33 |
  785. // +----+----+----+----+
  786. setModelData( model, [
  787. [ '00', '01', '02', '03' ],
  788. [ { contents: '10', rowspan: 2 }, { contents: '11', colspan: 2, rowspan: 2 }, '13' ],
  789. [ '23' ],
  790. [ '30', '31', '32', '33' ]
  791. ] );
  792. tableSelection.setCellSelection(
  793. modelRoot.getNodeByPath( [ 0, 0, 2 ] ),
  794. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  795. );
  796. pasteTable( [
  797. [ 'aa', 'ab', 'ac' ],
  798. [ 'ba', 'bb', 'bc' ],
  799. [ 'ca', 'cb', 'cc' ]
  800. ] );
  801. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  802. [ 'aa', 'ab', 'ac', '03' ],
  803. [ 'ba', 'bb', 'bc', '13' ],
  804. [ 'ca', 'cb', 'cc', '23' ],
  805. [ '30', '31', '32', '33' ]
  806. ] ) );
  807. assertSelectedCells( model, [
  808. [ 1, 1, 1, 0 ],
  809. [ 1, 1, 1, 0 ],
  810. [ 1, 1, 1, 0 ],
  811. [ 0, 0, 0, 0 ]
  812. ] );
  813. } );
  814. } );
  815. describe( 'content and paste tables have spans', () => {
  816. it( 'handles pasting colspanned table over table with colspans (no colspan exceeds selection)', () => {
  817. setModelData( model, modelTable( [
  818. [ '00[]', '01', '02', '03' ],
  819. [ { colspan: 3, contents: '10' }, '13' ],
  820. [ { colspan: 2, contents: '20' }, '22', '23' ],
  821. [ '30', '31', { colspan: 2, contents: '31' } ]
  822. ] ) );
  823. tableSelection.setCellSelection(
  824. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  825. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  826. );
  827. pasteTable( [
  828. [ 'aa', { colspan: 2, contents: 'ab' } ],
  829. [ { colspan: 3, contents: 'ba' } ],
  830. [ 'ca', 'cb', 'cc' ]
  831. ] );
  832. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  833. [ 'aa', { colspan: 2, contents: 'ab' }, '03' ],
  834. [ { colspan: 3, contents: 'ba' }, '13' ],
  835. [ 'ca', 'cb', 'cc', '23' ],
  836. [ '30', '31', { colspan: 2, contents: '31' } ]
  837. ] ) );
  838. /* eslint-disable no-multi-spaces */
  839. assertSelectedCells( model, [
  840. [ 1, 1, 0 ],
  841. [ 1, 0 ],
  842. [ 1, 1, 1, 0 ],
  843. [ 0, 0, 0 ]
  844. ] );
  845. /* eslint-enable no-multi-spaces */
  846. } );
  847. it( 'handles pasting rowspanned table over table with rowspans (no rowspan exceeds selection)', () => {
  848. setModelData( model, modelTable( [
  849. [ { rowspan: 3, contents: '00' }, { rowspan: 2, contents: '01' }, '02', '03' ],
  850. [ { rowspan: 2, contents: '12' }, '13' ],
  851. [ '21', '23' ],
  852. [ '30', '31', '32', '33' ]
  853. ] ) );
  854. tableSelection.setCellSelection(
  855. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  856. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  857. );
  858. pasteTable( [
  859. [ 'aa', { rowspan: 3, contents: 'ab' }, { rowspan: 2, contents: 'ac' }, 'ad' ],
  860. [ { rowspan: 2, contents: 'ba' }, 'bd' ],
  861. [ 'cc', 'cd' ]
  862. ] );
  863. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  864. [ 'aa', { rowspan: 3, contents: 'ab' }, { rowspan: 2, contents: 'ac' }, 'ad' ],
  865. [ { rowspan: 2, contents: 'ba' }, 'bd' ],
  866. [ 'cc', 'cd' ],
  867. [ '30', '31', '32', '33' ]
  868. ] ) );
  869. /* eslint-disable no-multi-spaces */
  870. assertSelectedCells( model, [
  871. [ 1, 1, 1, 1 ],
  872. [ 1, 1 ],
  873. [ 1, 1 ],
  874. [ 0, 0, 0, 0 ]
  875. ] );
  876. /* eslint-enable no-multi-spaces */
  877. } );
  878. it( 'handles pasting multi-spanned table over table with multi-spans (no span exceeds selection)', () => {
  879. // +----+----+----+----+----+----+
  880. // | 00 | 02 | 03 | 05 |
  881. // + + + +----+
  882. // | | | | 15 |
  883. // +----+----+----+ +----+
  884. // | 20 | 21 | | 25 |
  885. // + +----+----+----+----+----+
  886. // | | 31 | 32 | 34 | 35 |
  887. // +----+----+----+----+----+----+
  888. // | 40 | 41 | 42 | 43 | 44 | 45 |
  889. // +----+----+----+----+----+----+
  890. setModelData( model, modelTable( [
  891. [
  892. { contents: '00', colspan: 2, rowspan: 2 },
  893. { contents: '02', rowspan: 2 },
  894. { contents: '03', colspan: 2, rowspan: 3 },
  895. '05'
  896. ],
  897. [ '15' ],
  898. [ { contents: '20', rowspan: 2 }, { contents: '21', colspan: 2 }, '25' ],
  899. [ '31', { contents: '32', colspan: 2 }, '34', '35' ],
  900. [ '40', '41', '42', '43', '44', '45' ]
  901. ] ) );
  902. tableSelection.setCellSelection(
  903. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  904. modelRoot.getNodeByPath( [ 0, 3, 2 ] )
  905. );
  906. // +----+----+----+----+----+
  907. // | aa | ac | ad | ae |
  908. // +----+----+----+----+ +
  909. // | ba | bb | |
  910. // +----+ +----+
  911. // | ca | | ce |
  912. // + +----+----+----+----+
  913. // | | db | dc | dd |
  914. // +----+----+----+----+----+
  915. pasteTable( [
  916. [ { contents: 'aa', colspan: 2 }, 'ac', 'ad', { contents: 'ae', rowspan: 2 } ],
  917. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 } ],
  918. [ { contents: 'ca', rowspan: 2 }, 'ce' ],
  919. [ 'db', 'dc', { contents: 'dd', colspan: 2 } ]
  920. ] );
  921. // +----+----+----+----+----+----+
  922. // | aa | ac | ad | ae | 05 |
  923. // +----+----+----+----+ +----+
  924. // | ba | bb | | 15 |
  925. // +----+ +----+----+
  926. // | ca | | ce | 25 |
  927. // + +----+----+----+----+----+
  928. // | | db | dc | dd | 35 |
  929. // +----+----+----+----+----+----+
  930. // | 40 | 41 | 42 | 43 | 44 | 45 |
  931. // +----+----+----+----+----+----+
  932. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  933. [ { contents: 'aa', colspan: 2 }, 'ac', 'ad', { contents: 'ae', rowspan: 2 }, '05' ],
  934. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 }, '15' ],
  935. [ { contents: 'ca', rowspan: 2 }, 'ce', '25' ],
  936. [ 'db', 'dc', { contents: 'dd', colspan: 2 }, '35' ],
  937. [ '40', '41', '42', '43', '44', '45' ]
  938. ] ) );
  939. /* eslint-disable no-multi-spaces */
  940. assertSelectedCells( model, [
  941. [ 1, 1, 1, 1, 0 ],
  942. [ 1, 1, 0 ],
  943. [ 1, 1, 0 ],
  944. [ 1, 1, 1, 0 ],
  945. [ 0, 0, 0, 0, 0, 0 ]
  946. ] );
  947. /* eslint-enable no-multi-spaces */
  948. } );
  949. it( 'handles pasting table that has cell with colspan (last row in selection is spanned)', () => {
  950. // +----+----+----+----+
  951. // | 00 | 01 | 02 | 03 |
  952. // +----+----+----+----+
  953. // | 10 | 11 | 13 |
  954. // + + +----+
  955. // | | | 23 |
  956. // +----+----+----+----+
  957. // | 30 | 31 | 32 | 33 |
  958. // +----+----+----+----+
  959. setModelData( model, [
  960. [ '00', '01', '02', '03' ],
  961. [ { contents: '10', rowspan: 2 }, { contents: '11', colspan: 2, rowspan: 2 }, '13' ],
  962. [ '23' ],
  963. [ '30', '31', '32', '33' ]
  964. ] );
  965. tableSelection.setCellSelection(
  966. modelRoot.getNodeByPath( [ 0, 0, 2 ] ),
  967. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  968. );
  969. // +----+----+----+
  970. // | aa | ab | ac |
  971. // +----+----+----+
  972. // | ba | bc |
  973. // + +----+
  974. // | | cc |
  975. // +----+----+----+
  976. pasteTable( [
  977. [ 'aa', 'ab', 'ac' ],
  978. [ { contents: 'ba', colspan: 2, rowspan: 2 }, 'bc' ],
  979. [ 'cc' ]
  980. ] );
  981. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  982. [ '00', '01', '02', '03' ],
  983. [ '10', { colspan: 2, contents: 'aa' }, '13' ],
  984. [ '20', 'ba', 'bb', '23' ],
  985. [ '30', '31', '32', '33' ]
  986. ] ) );
  987. /* eslint-disable no-multi-spaces */
  988. assertSelectedCells( model, [
  989. [ 0, 0, 0, 0 ],
  990. [ 0, 1, 0 ],
  991. [ 0, 1, 1, 0 ],
  992. [ 0, 0, 0, 0 ]
  993. ] );
  994. /* eslint-enable no-multi-spaces */
  995. } );
  996. } );
  997. } );
  998. } );
  999. } );
  1000. function assertClipboardContentOnMethod( method, expectedViewTable ) {
  1001. const data = {
  1002. dataTransfer: createDataTransfer(),
  1003. preventDefault: sinon.spy()
  1004. };
  1005. viewDocument.fire( method, data );
  1006. expect( data.dataTransfer.getData( 'text/html' ) ).to.equal( expectedViewTable );
  1007. }
  1008. function pasteTable( tableData ) {
  1009. const data = {
  1010. dataTransfer: createDataTransfer(),
  1011. preventDefault: sinon.spy(),
  1012. stopPropagation: sinon.spy()
  1013. };
  1014. data.dataTransfer.setData( 'text/html', viewTable( tableData ) );
  1015. viewDocument.fire( 'paste', data );
  1016. return data;
  1017. }
  1018. function createDataTransfer() {
  1019. const store = new Map();
  1020. return {
  1021. setData( type, data ) {
  1022. store.set( type, data );
  1023. },
  1024. getData( type ) {
  1025. return store.get( type );
  1026. }
  1027. };
  1028. }
  1029. } );