tableclipboard.js 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915
  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. it( 'should be disabled in a readonly mode', () => {
  340. editor.isReadOnly = true;
  341. tableSelection.setCellSelection(
  342. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  343. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  344. );
  345. const data = pasteTable( [
  346. [ 'aa', 'ab' ],
  347. [ 'ba', 'bb' ]
  348. ] );
  349. editor.isReadOnly = false;
  350. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  351. [ '00', '01', '02', '03' ],
  352. [ '10', '11', '12', '13' ],
  353. [ '20', '21', '22', '23' ],
  354. [ '30', '31', '32', '33' ]
  355. ] ) );
  356. sinon.assert.calledOnce( data.preventDefault );
  357. } );
  358. it( 'should allow normal paste if no table cells are selected', () => {
  359. const data = {
  360. dataTransfer: createDataTransfer(),
  361. preventDefault: sinon.spy(),
  362. stopPropagation: sinon.spy()
  363. };
  364. data.dataTransfer.setData( 'text/html', '<p>foo</p>' );
  365. viewDocument.fire( 'paste', data );
  366. editor.isReadOnly = false;
  367. assertEqualMarkup( getModelData( model ), modelTable( [
  368. [ '00foo[]', '01', '02', '03' ],
  369. [ '10', '11', '12', '13' ],
  370. [ '20', '21', '22', '23' ],
  371. [ '30', '31', '32', '33' ]
  372. ] ) );
  373. } );
  374. describe( 'pasted table is equal to the selected area', () => {
  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. // TODO: Skipped case - should allow pasting but no tools to compare areas (like in MergeCellsCommand).
  777. it.skip( 'handles pasting table that has cell with colspan (last row in selection is spanned)', () => {
  778. // +----+----+----+----+
  779. // | 00 | 01 | 02 | 03 |
  780. // +----+----+----+----+
  781. // | 10 | 11 | 13 |
  782. // + + +----+
  783. // | | | 23 |
  784. // +----+----+----+----+
  785. // | 30 | 31 | 32 | 33 |
  786. // +----+----+----+----+
  787. setModelData( model, modelTable( [
  788. [ '00', '01', '02', '03' ],
  789. [ { contents: '10', rowspan: 2 }, { contents: '11', colspan: 2, rowspan: 2 }, '13' ],
  790. [ '23' ],
  791. [ '30', '31', '32', '33' ]
  792. ] ) );
  793. tableSelection.setCellSelection(
  794. modelRoot.getNodeByPath( [ 0, 0, 2 ] ),
  795. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  796. );
  797. pasteTable( [
  798. [ 'aa', 'ab', 'ac' ],
  799. [ 'ba', 'bb', 'bc' ],
  800. [ 'ca', 'cb', 'cc' ]
  801. ] );
  802. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  803. [ 'aa', 'ab', 'ac', '03' ],
  804. [ 'ba', 'bb', 'bc', '13' ],
  805. [ 'ca', 'cb', 'cc', '23' ],
  806. [ '30', '31', '32', '33' ]
  807. ] ) );
  808. assertSelectedCells( model, [
  809. [ 1, 1, 1, 0 ],
  810. [ 1, 1, 1, 0 ],
  811. [ 1, 1, 1, 0 ],
  812. [ 0, 0, 0, 0 ]
  813. ] );
  814. } );
  815. } );
  816. describe( 'content and paste tables have spans', () => {
  817. it( 'handles pasting colspanned table over table with colspans (no colspan exceeds selection)', () => {
  818. setModelData( model, modelTable( [
  819. [ '00[]', '01', '02', '03' ],
  820. [ { colspan: 3, contents: '10' }, '13' ],
  821. [ { colspan: 2, contents: '20' }, '22', '23' ],
  822. [ '30', '31', { colspan: 2, contents: '31' } ]
  823. ] ) );
  824. tableSelection.setCellSelection(
  825. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  826. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  827. );
  828. pasteTable( [
  829. [ 'aa', { colspan: 2, contents: 'ab' } ],
  830. [ { colspan: 3, contents: 'ba' } ],
  831. [ 'ca', 'cb', 'cc' ]
  832. ] );
  833. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  834. [ 'aa', { colspan: 2, contents: 'ab' }, '03' ],
  835. [ { colspan: 3, contents: 'ba' }, '13' ],
  836. [ 'ca', 'cb', 'cc', '23' ],
  837. [ '30', '31', { colspan: 2, contents: '31' } ]
  838. ] ) );
  839. /* eslint-disable no-multi-spaces */
  840. assertSelectedCells( model, [
  841. [ 1, 1, 0 ],
  842. [ 1, 0 ],
  843. [ 1, 1, 1, 0 ],
  844. [ 0, 0, 0 ]
  845. ] );
  846. /* eslint-enable no-multi-spaces */
  847. } );
  848. it( 'handles pasting rowspanned table over table with rowspans (no rowspan exceeds selection)', () => {
  849. setModelData( model, modelTable( [
  850. [ { rowspan: 3, contents: '00' }, { rowspan: 2, contents: '01' }, '02', '03' ],
  851. [ { rowspan: 2, contents: '12' }, '13' ],
  852. [ '21', '23' ],
  853. [ '30', '31', '32', '33' ]
  854. ] ) );
  855. tableSelection.setCellSelection(
  856. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  857. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  858. );
  859. pasteTable( [
  860. [ 'aa', { rowspan: 3, contents: 'ab' }, { rowspan: 2, contents: 'ac' }, 'ad' ],
  861. [ { rowspan: 2, contents: 'ba' }, 'bd' ],
  862. [ 'cc', 'cd' ]
  863. ] );
  864. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  865. [ 'aa', { rowspan: 3, contents: 'ab' }, { rowspan: 2, contents: 'ac' }, 'ad' ],
  866. [ { rowspan: 2, contents: 'ba' }, 'bd' ],
  867. [ 'cc', 'cd' ],
  868. [ '30', '31', '32', '33' ]
  869. ] ) );
  870. /* eslint-disable no-multi-spaces */
  871. assertSelectedCells( model, [
  872. [ 1, 1, 1, 1 ],
  873. [ 1, 1 ],
  874. [ 1, 1 ],
  875. [ 0, 0, 0, 0 ]
  876. ] );
  877. /* eslint-enable no-multi-spaces */
  878. } );
  879. it( 'handles pasting multi-spanned table over table with multi-spans (no span exceeds selection)', () => {
  880. // +----+----+----+----+----+----+
  881. // | 00 | 02 | 03 | 05 |
  882. // + + + +----+
  883. // | | | | 15 |
  884. // +----+----+----+ +----+
  885. // | 20 | 21 | | 25 |
  886. // + +----+----+----+----+----+
  887. // | | 31 | 32 | 34 | 35 |
  888. // +----+----+----+----+----+----+
  889. // | 40 | 41 | 42 | 43 | 44 | 45 |
  890. // +----+----+----+----+----+----+
  891. setModelData( model, modelTable( [
  892. [
  893. { contents: '00', colspan: 2, rowspan: 2 },
  894. { contents: '02', rowspan: 2 },
  895. { contents: '03', colspan: 2, rowspan: 3 },
  896. '05'
  897. ],
  898. [ '15' ],
  899. [ { contents: '20', rowspan: 2 }, { contents: '21', colspan: 2 }, '25' ],
  900. [ '31', { contents: '32', colspan: 2 }, '34', '35' ],
  901. [ '40', '41', '42', '43', '44', '45' ]
  902. ] ) );
  903. tableSelection.setCellSelection(
  904. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  905. modelRoot.getNodeByPath( [ 0, 3, 2 ] )
  906. );
  907. // +----+----+----+----+----+
  908. // | aa | ac | ad | ae |
  909. // +----+----+----+----+ +
  910. // | ba | bb | |
  911. // +----+ +----+
  912. // | ca | | ce |
  913. // + +----+----+----+----+
  914. // | | db | dc | dd |
  915. // +----+----+----+----+----+
  916. pasteTable( [
  917. [ { contents: 'aa', colspan: 2 }, 'ac', 'ad', { contents: 'ae', rowspan: 2 } ],
  918. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 } ],
  919. [ { contents: 'ca', rowspan: 2 }, 'ce' ],
  920. [ 'db', 'dc', { contents: 'dd', colspan: 2 } ]
  921. ] );
  922. // +----+----+----+----+----+----+
  923. // | aa | ac | ad | ae | 05 |
  924. // +----+----+----+----+ +----+
  925. // | ba | bb | | 15 |
  926. // +----+ +----+----+
  927. // | ca | | ce | 25 |
  928. // + +----+----+----+----+----+
  929. // | | db | dc | dd | 35 |
  930. // +----+----+----+----+----+----+
  931. // | 40 | 41 | 42 | 43 | 44 | 45 |
  932. // +----+----+----+----+----+----+
  933. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  934. [ { contents: 'aa', colspan: 2 }, 'ac', 'ad', { contents: 'ae', rowspan: 2 }, '05' ],
  935. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 }, '15' ],
  936. [ { contents: 'ca', rowspan: 2 }, 'ce', '25' ],
  937. [ 'db', 'dc', { contents: 'dd', colspan: 2 }, '35' ],
  938. [ '40', '41', '42', '43', '44', '45' ]
  939. ] ) );
  940. /* eslint-disable no-multi-spaces */
  941. assertSelectedCells( model, [
  942. [ 1, 1, 1, 1, 0 ],
  943. [ 1, 1, 0 ],
  944. [ 1, 1, 0 ],
  945. [ 1, 1, 1, 0 ],
  946. [ 0, 0, 0, 0, 0, 0 ]
  947. ] );
  948. /* eslint-enable no-multi-spaces */
  949. } );
  950. // TODO: Skipped case - should allow pasting but no tools to compare areas (like in MergeCellsCommand).
  951. it.skip( 'handles pasting table that has cell with colspan (last row in selection is spanned)', () => {
  952. // +----+----+----+----+
  953. // | 00 | 01 | 02 | 03 |
  954. // +----+----+----+----+
  955. // | 10 | 11 | 13 |
  956. // + + +----+
  957. // | | | 23 |
  958. // +----+----+----+----+
  959. // | 30 | 31 | 32 | 33 |
  960. // +----+----+----+----+
  961. setModelData( model, modelTable( [
  962. [ '00', '01', '02', '03' ],
  963. [ { contents: '10', rowspan: 2 }, { contents: '11', colspan: 2, rowspan: 2 }, '13' ],
  964. [ '23' ],
  965. [ '30', '31', '32', '33' ]
  966. ] ) );
  967. tableSelection.setCellSelection(
  968. modelRoot.getNodeByPath( [ 0, 0, 2 ] ),
  969. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  970. );
  971. // +----+----+----+
  972. // | aa | ab | ac |
  973. // +----+----+----+
  974. // | ba | bc |
  975. // + +----+
  976. // | | cc |
  977. // +----+----+----+
  978. pasteTable( [
  979. [ 'aa', 'ab', 'ac' ],
  980. [ { contents: 'ba', colspan: 2, rowspan: 2 }, 'bc' ],
  981. [ 'cc' ]
  982. ] );
  983. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  984. [ '00', '01', '02', '03' ],
  985. [ '10', { colspan: 2, contents: 'aa' }, '13' ],
  986. [ '20', 'ba', 'bb', '23' ],
  987. [ '30', '31', '32', '33' ]
  988. ] ) );
  989. /* eslint-disable no-multi-spaces */
  990. assertSelectedCells( model, [
  991. [ 0, 0, 0, 0 ],
  992. [ 0, 1, 0 ],
  993. [ 0, 1, 1, 0 ],
  994. [ 0, 0, 0, 0 ]
  995. ] );
  996. /* eslint-enable no-multi-spaces */
  997. } );
  998. } );
  999. } );
  1000. describe( 'pasted table is smaller than the selected area', () => {
  1001. describe( 'no spans', () => {
  1002. it( 'handles simple table paste to a simple table fragment - at the beginning of a table', () => {
  1003. tableSelection.setCellSelection(
  1004. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1005. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  1006. );
  1007. pasteTable( [
  1008. [ 'aa', 'ab', 'ac' ],
  1009. [ 'ba', 'bb', 'bc' ],
  1010. [ 'ca', 'cb', 'cc' ]
  1011. ] );
  1012. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1013. [ 'aa', 'ab', '02', '03' ],
  1014. [ 'ba', 'bb', '12', '13' ],
  1015. [ '20', '21', '22', '23' ],
  1016. [ '30', '31', '32', '33' ]
  1017. ] ) );
  1018. assertSelectedCells( model, [
  1019. [ 1, 1, 0, 0 ],
  1020. [ 1, 1, 0, 0 ],
  1021. [ 0, 0, 0, 0 ],
  1022. [ 0, 0, 0, 0 ]
  1023. ] );
  1024. } );
  1025. it( 'handles simple table paste to a simple table fragment - at the end of a table', () => {
  1026. tableSelection.setCellSelection(
  1027. modelRoot.getNodeByPath( [ 0, 2, 2 ] ),
  1028. modelRoot.getNodeByPath( [ 0, 3, 3 ] )
  1029. );
  1030. pasteTable( [
  1031. [ 'aa', 'ab' ],
  1032. [ 'ba', 'bb' ]
  1033. ] );
  1034. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1035. [ '00', '01', '02', '03' ],
  1036. [ '10', '11', '12', '13' ],
  1037. [ '20', '21', 'aa', 'ab' ],
  1038. [ '30', '31', 'ba', 'bb' ]
  1039. ] ) );
  1040. assertSelectedCells( model, [
  1041. [ 0, 0, 0, 0 ],
  1042. [ 0, 0, 0, 0 ],
  1043. [ 0, 0, 1, 1 ],
  1044. [ 0, 0, 1, 1 ]
  1045. ] );
  1046. } );
  1047. it( 'handles simple table paste to a simple table fragment - in the middle of a table', () => {
  1048. tableSelection.setCellSelection(
  1049. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  1050. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  1051. );
  1052. pasteTable( [
  1053. [ 'aa', 'ab' ],
  1054. [ 'ba', 'bb' ]
  1055. ] );
  1056. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1057. [ '00', '01', '02', '03' ],
  1058. [ '10', 'aa', 'ab', '13' ],
  1059. [ '20', 'ba', 'bb', '23' ],
  1060. [ '30', '31', '32', '33' ]
  1061. ] ) );
  1062. assertSelectedCells( model, [
  1063. [ 0, 0, 0, 0 ],
  1064. [ 0, 1, 1, 0 ],
  1065. [ 0, 1, 1, 0 ],
  1066. [ 0, 0, 0, 0 ]
  1067. ] );
  1068. } );
  1069. it( 'handles simple row paste to a simple row fragment - in the middle of a table', () => {
  1070. tableSelection.setCellSelection(
  1071. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  1072. modelRoot.getNodeByPath( [ 0, 1, 2 ] )
  1073. );
  1074. pasteTable( [
  1075. [ 'aa', 'ab' ]
  1076. ] );
  1077. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1078. [ '00', '01', '02', '03' ],
  1079. [ '10', 'aa', 'ab', '13' ],
  1080. [ '20', '21', '22', '23' ],
  1081. [ '30', '31', '32', '33' ]
  1082. ] ) );
  1083. assertSelectedCells( model, [
  1084. [ 0, 0, 0, 0 ],
  1085. [ 0, 1, 1, 0 ],
  1086. [ 0, 0, 0, 0 ],
  1087. [ 0, 0, 0, 0 ]
  1088. ] );
  1089. } );
  1090. it( 'handles simple column paste to a simple column fragment - in the middle of a table', () => {
  1091. tableSelection.setCellSelection(
  1092. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  1093. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  1094. );
  1095. pasteTable( [
  1096. [ 'aa' ],
  1097. [ 'ba' ]
  1098. ] );
  1099. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1100. [ '00', '01', '02', '03' ],
  1101. [ '10', 'aa', '12', '13' ],
  1102. [ '20', 'ba', '22', '23' ],
  1103. [ '30', '31', '32', '33' ]
  1104. ] ) );
  1105. assertSelectedCells( model, [
  1106. [ 0, 0, 0, 0 ],
  1107. [ 0, 1, 0, 0 ],
  1108. [ 0, 1, 0, 0 ],
  1109. [ 0, 0, 0, 0 ]
  1110. ] );
  1111. } );
  1112. it( 'handles simple table paste to a simple table fragment - whole table selected', () => {
  1113. tableSelection.setCellSelection(
  1114. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1115. modelRoot.getNodeByPath( [ 0, 3, 3 ] )
  1116. );
  1117. pasteTable( [
  1118. [ 'aa', 'ab', 'ac', 'ad' ],
  1119. [ 'ba', 'bb', 'bc', 'bd' ],
  1120. [ 'ca', 'cb', 'cc', 'cd' ],
  1121. [ 'da', 'db', 'dc', 'dd' ]
  1122. ] );
  1123. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1124. [ 'aa', 'ab', 'ac', 'ad' ],
  1125. [ 'ba', 'bb', 'bc', 'bd' ],
  1126. [ 'ca', 'cb', 'cc', 'cd' ],
  1127. [ 'da', 'db', 'dc', 'dd' ]
  1128. ] ) );
  1129. assertSelectedCells( model, [
  1130. [ 1, 1, 1, 1 ],
  1131. [ 1, 1, 1, 1 ],
  1132. [ 1, 1, 1, 1 ],
  1133. [ 1, 1, 1, 1 ]
  1134. ] );
  1135. } );
  1136. } );
  1137. describe.skip( 'pasted table has spans', () => {
  1138. it( 'handles pasting table that has cell with colspan', () => {
  1139. tableSelection.setCellSelection(
  1140. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  1141. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  1142. );
  1143. pasteTable( [
  1144. [ { colspan: 2, contents: 'aa' } ],
  1145. [ 'ba', 'bb' ]
  1146. ] );
  1147. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1148. [ '00', '01', '02', '03' ],
  1149. [ '10', { colspan: 2, contents: 'aa' }, '13' ],
  1150. [ '20', 'ba', 'bb', '23' ],
  1151. [ '30', '31', '32', '33' ]
  1152. ] ) );
  1153. /* eslint-disable no-multi-spaces */
  1154. assertSelectedCells( model, [
  1155. [ 0, 0, 0, 0 ],
  1156. [ 0, 1, 0 ],
  1157. [ 0, 1, 1, 0 ],
  1158. [ 0, 0, 0, 0 ]
  1159. ] );
  1160. /* eslint-enable no-multi-spaces */
  1161. } );
  1162. it( 'handles pasting table that has many cells with various colspan', () => {
  1163. tableSelection.setCellSelection(
  1164. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1165. modelRoot.getNodeByPath( [ 0, 3, 2 ] )
  1166. );
  1167. pasteTable( [
  1168. [ 'aa', { colspan: 2, contents: 'ab' } ],
  1169. [ { colspan: 3, contents: 'ba' } ],
  1170. [ 'ca', 'cb', 'cc' ],
  1171. [ { colspan: 2, contents: 'da' }, 'dc' ]
  1172. ] );
  1173. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1174. [ 'aa', { colspan: 2, contents: 'ab' }, '03' ],
  1175. [ { colspan: 3, contents: 'ba' }, '13' ],
  1176. [ 'ca', 'cb', 'cc', '23' ],
  1177. [ { colspan: 2, contents: 'da' }, 'dc', '33' ]
  1178. ] ) );
  1179. /* eslint-disable no-multi-spaces */
  1180. assertSelectedCells( model, [
  1181. [ 1, 1, 0 ],
  1182. [ 1, 0 ],
  1183. [ 1, 1, 1, 0 ],
  1184. [ 1, 1, 0 ]
  1185. ] );
  1186. /* eslint-enable no-multi-spaces */
  1187. } );
  1188. it( 'handles pasting table that has cell with rowspan', () => {
  1189. tableSelection.setCellSelection(
  1190. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  1191. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  1192. );
  1193. pasteTable( [
  1194. [ { rowspan: 2, contents: 'aa' }, 'ab' ],
  1195. [ 'bb' ]
  1196. ] );
  1197. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1198. [ '00', '01', '02', '03' ],
  1199. [ '10', { rowspan: 2, contents: 'aa' }, 'ab', '13' ],
  1200. [ '20', 'bb', '23' ],
  1201. [ '30', '31', '32', '33' ]
  1202. ] ) );
  1203. /* eslint-disable no-multi-spaces */
  1204. assertSelectedCells( model, [
  1205. [ 0, 0, 0, 0 ],
  1206. [ 0, 1, 1, 0 ],
  1207. [ 0, 1, 0 ],
  1208. [ 0, 0, 0, 0 ]
  1209. ] );
  1210. /* eslint-enable no-multi-spaces */
  1211. } );
  1212. it( 'handles pasting table that has many cells with various rowspan', () => {
  1213. tableSelection.setCellSelection(
  1214. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1215. modelRoot.getNodeByPath( [ 0, 2, 3 ] )
  1216. );
  1217. pasteTable( [
  1218. [ 'aa', { rowspan: 3, contents: 'ab' }, { rowspan: 2, contents: 'ac' }, 'ad' ],
  1219. [ { rowspan: 2, contents: 'ba' }, 'bd' ],
  1220. [ 'cc', 'cd' ]
  1221. ] );
  1222. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1223. [ 'aa', { rowspan: 3, contents: 'ab' }, { rowspan: 2, contents: 'ac' }, 'ad' ],
  1224. [ { rowspan: 2, contents: 'ba' }, 'bd' ],
  1225. [ 'cc', 'cd' ],
  1226. [ '30', '31', '32', '33' ]
  1227. ] ) );
  1228. /* eslint-disable no-multi-spaces */
  1229. assertSelectedCells( model, [
  1230. [ 1, 1, 1, 1 ],
  1231. [ 1, 1 ],
  1232. [ 1, 1 ],
  1233. [ 0, 0, 0 ]
  1234. ] );
  1235. /* eslint-enable no-multi-spaces */
  1236. } );
  1237. it( 'handles pasting multi-spanned table', () => {
  1238. setModelData( model, modelTable( [
  1239. [ '00', '01', '02', '03', '04', '05' ],
  1240. [ '10', '11', '12', '13', '14', '15' ],
  1241. [ '20', '21', '22', '23', '24', '25' ],
  1242. [ '30', '31', '32', '33', '34', '35' ],
  1243. [ '40', '41', '42', '43', '44', '45' ]
  1244. ] ) );
  1245. tableSelection.setCellSelection(
  1246. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1247. modelRoot.getNodeByPath( [ 0, 3, 4 ] )
  1248. );
  1249. // +----+----+----+----+----+
  1250. // | aa | ac | ad | ae |
  1251. // +----+----+----+----+ +
  1252. // | ba | bb | |
  1253. // +----+ +----+
  1254. // | ca | | ce |
  1255. // + +----+----+----+----+
  1256. // | | db | dc | dd |
  1257. // +----+----+----+----+----+
  1258. pasteTable( [
  1259. [ { contents: 'aa', colspan: 2 }, 'ac', 'ad', { contents: 'ae', rowspan: 2 } ],
  1260. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 } ],
  1261. [ { contents: 'ca', rowspan: 2 }, 'ce' ],
  1262. [ 'db', 'dc', { contents: 'dd', colspan: 2 } ]
  1263. ] );
  1264. // +----+----+----+----+----+----+
  1265. // | aa | ac | ad | ae | 05 |
  1266. // +----+----+----+----+ +----+
  1267. // | ba | bb | | 15 |
  1268. // +----+ +----+----+
  1269. // | ca | | ce | 25 |
  1270. // + +----+----+----+----+----+
  1271. // | | db | dc | dd | 35 |
  1272. // +----+----+----+----+----+----+
  1273. // | 40 | 41 | 42 | 43 | 44 | 45 |
  1274. // +----+----+----+----+----+----+
  1275. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1276. [ { contents: 'aa', colspan: 2 }, 'ac', 'ad', { contents: 'ae', rowspan: 2 }, '05' ],
  1277. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 }, '15' ],
  1278. [ { contents: 'ca', rowspan: 2 }, 'ce', '25' ],
  1279. [ 'db', 'dc', { contents: 'dd', colspan: 2 }, '35' ],
  1280. [ '40', '41', '42', '43', '44', '45' ]
  1281. ] ) );
  1282. /* eslint-disable no-multi-spaces */
  1283. assertSelectedCells( model, [
  1284. [ 1, 1, 1, 1, 0 ],
  1285. [ 1, 1, 0 ],
  1286. [ 1, 1, 0 ],
  1287. [ 1, 1, 1, 0 ],
  1288. [ 0, 0, 0, 0, 0, 0 ]
  1289. ] );
  1290. /* eslint-enable no-multi-spaces */
  1291. } );
  1292. } );
  1293. describe.skip( 'content table has spans', () => {
  1294. it( 'handles pasting simple table over a table with colspans (no colspan exceeds selection)', () => {
  1295. setModelData( model, modelTable( [
  1296. [ '00[]', '01', '02', '03' ],
  1297. [ { colspan: 3, contents: '10' }, '13' ],
  1298. [ { colspan: 2, contents: '20' }, '22', '23' ],
  1299. [ '30', '31', { colspan: 2, contents: '31' } ]
  1300. ] ) );
  1301. tableSelection.setCellSelection(
  1302. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1303. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  1304. );
  1305. pasteTable( [
  1306. [ 'aa', 'ab', 'ac' ],
  1307. [ 'ba', 'bb', 'bc' ],
  1308. [ 'ca', 'cb', 'cc' ]
  1309. ] );
  1310. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1311. [ 'aa', 'ab', 'ac', '03' ],
  1312. [ 'ba', 'bb', 'bc', '13' ],
  1313. [ 'ca', 'cb', 'cc', '23' ],
  1314. [ '30', '31', { colspan: 2, contents: '31' } ]
  1315. ] ) );
  1316. /* eslint-disable no-multi-spaces */
  1317. assertSelectedCells( model, [
  1318. [ 1, 1, 1, 0 ],
  1319. [ 1, 1, 1, 0 ],
  1320. [ 1, 1, 1, 0 ],
  1321. [ 0, 0, 0 ]
  1322. ] );
  1323. /* eslint-enable no-multi-spaces */
  1324. } );
  1325. it( 'handles pasting simple table over a table with rowspans (no rowspan exceeds selection)', () => {
  1326. setModelData( model, modelTable( [
  1327. [ '00', { rowspan: 3, contents: '01' }, { rowspan: 2, contents: '02' }, '03' ],
  1328. [ { rowspan: 2, contents: '10' }, '13' ],
  1329. [ '22', '23' ],
  1330. [ '30', '31', '32', '33' ]
  1331. ] ) );
  1332. tableSelection.setCellSelection(
  1333. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1334. modelRoot.getNodeByPath( [ 0, 2, 0 ] )
  1335. );
  1336. pasteTable( [
  1337. [ 'aa', 'ab', 'ac' ],
  1338. [ 'ba', 'bb', 'bc' ],
  1339. [ 'ca', 'cb', 'cc' ]
  1340. ] );
  1341. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1342. [ 'aa', 'ab', 'ac', '03' ],
  1343. [ 'ba', 'bb', 'bc', '13' ],
  1344. [ 'ca', 'cb', 'cc', '23' ],
  1345. [ '30', '31', '32', '33' ]
  1346. ] ) );
  1347. assertSelectedCells( model, [
  1348. [ 1, 1, 1, 0 ],
  1349. [ 1, 1, 1, 0 ],
  1350. [ 1, 1, 1, 0 ],
  1351. [ 0, 0, 0, 0 ]
  1352. ] );
  1353. } );
  1354. it( 'handles pasting simple table over table with multi-spans (no span exceeds selection)', () => {
  1355. // +----+----+----+----+----+----+
  1356. // | 00 | 02 | 03 | 05 |
  1357. // + + + +----+
  1358. // | | | | 15 |
  1359. // +----+----+----+ +----+
  1360. // | 20 | 21 | | 25 |
  1361. // + +----+----+----+----+----+
  1362. // | | 31 | 32 | 34 | 35 |
  1363. // +----+----+----+----+----+----+
  1364. // | 40 | 41 | 42 | 43 | 44 | 45 |
  1365. // +----+----+----+----+----+----+
  1366. setModelData( model, modelTable( [
  1367. [
  1368. { contents: '00', colspan: 2, rowspan: 2 },
  1369. { contents: '02', rowspan: 2 },
  1370. { contents: '03', colspan: 2, rowspan: 3 },
  1371. '05'
  1372. ],
  1373. [ '15' ],
  1374. [ { contents: '20', rowspan: 2 }, { contents: '21', colspan: 2 }, '25' ],
  1375. [ '31', { contents: '32', colspan: 2 }, '34', '35' ],
  1376. [ '40', '41', '42', '43', '44', '45' ]
  1377. ] ) );
  1378. tableSelection.setCellSelection(
  1379. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1380. modelRoot.getNodeByPath( [ 0, 3, 2 ] )
  1381. );
  1382. pasteTable( [
  1383. [ 'aa', 'ab', 'ac', 'ad', 'ae' ],
  1384. [ 'ba', 'bb', 'bc', 'bd', 'be' ],
  1385. [ 'ca', 'cb', 'cc', 'cd', 'ce' ],
  1386. [ 'da', 'db', 'dc', 'dd', 'de' ]
  1387. ] );
  1388. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1389. [ 'aa', 'ab', 'ac', 'ad', 'ae', '05' ],
  1390. [ 'ba', 'bb', 'bc', 'bd', 'be', '15' ],
  1391. [ 'ca', 'cb', 'cc', 'cd', 'ce', '25' ],
  1392. [ 'da', 'db', 'dc', 'dd', 'de', '35' ],
  1393. [ '40', '41', '42', '43', '44', '45' ]
  1394. ] ) );
  1395. assertSelectedCells( model, [
  1396. [ 1, 1, 1, 1, 1, 0 ],
  1397. [ 1, 1, 1, 1, 1, 0 ],
  1398. [ 1, 1, 1, 1, 1, 0 ],
  1399. [ 1, 1, 1, 1, 1, 0 ],
  1400. [ 0, 0, 0, 0, 0, 0 ]
  1401. ] );
  1402. } );
  1403. // TODO: Skipped case - should allow pasting but no tools to compare areas (like in MergeCellsCommand).
  1404. it.skip( 'handles pasting table that has cell with colspan (last row in selection is spanned)', () => {
  1405. // +----+----+----+----+
  1406. // | 00 | 01 | 02 | 03 |
  1407. // +----+----+----+----+
  1408. // | 10 | 11 | 13 |
  1409. // + + +----+
  1410. // | | | 23 |
  1411. // +----+----+----+----+
  1412. // | 30 | 31 | 32 | 33 |
  1413. // +----+----+----+----+
  1414. setModelData( model, modelTable( [
  1415. [ '00', '01', '02', '03' ],
  1416. [ { contents: '10', rowspan: 2 }, { contents: '11', colspan: 2, rowspan: 2 }, '13' ],
  1417. [ '23' ],
  1418. [ '30', '31', '32', '33' ]
  1419. ] ) );
  1420. tableSelection.setCellSelection(
  1421. modelRoot.getNodeByPath( [ 0, 0, 2 ] ),
  1422. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  1423. );
  1424. pasteTable( [
  1425. [ 'aa', 'ab', 'ac' ],
  1426. [ 'ba', 'bb', 'bc' ],
  1427. [ 'ca', 'cb', 'cc' ]
  1428. ] );
  1429. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1430. [ 'aa', 'ab', 'ac', '03' ],
  1431. [ 'ba', 'bb', 'bc', '13' ],
  1432. [ 'ca', 'cb', 'cc', '23' ],
  1433. [ '30', '31', '32', '33' ]
  1434. ] ) );
  1435. assertSelectedCells( model, [
  1436. [ 1, 1, 1, 0 ],
  1437. [ 1, 1, 1, 0 ],
  1438. [ 1, 1, 1, 0 ],
  1439. [ 0, 0, 0, 0 ]
  1440. ] );
  1441. } );
  1442. } );
  1443. describe.skip( 'content and paste tables have spans', () => {
  1444. it( 'handles pasting colspanned table over table with colspans (no colspan exceeds selection)', () => {
  1445. setModelData( model, modelTable( [
  1446. [ '00[]', '01', '02', '03' ],
  1447. [ { colspan: 3, contents: '10' }, '13' ],
  1448. [ { colspan: 2, contents: '20' }, '22', '23' ],
  1449. [ '30', '31', { colspan: 2, contents: '31' } ]
  1450. ] ) );
  1451. tableSelection.setCellSelection(
  1452. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1453. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  1454. );
  1455. pasteTable( [
  1456. [ 'aa', { colspan: 2, contents: 'ab' } ],
  1457. [ { colspan: 3, contents: 'ba' } ],
  1458. [ 'ca', 'cb', 'cc' ]
  1459. ] );
  1460. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1461. [ 'aa', { colspan: 2, contents: 'ab' }, '03' ],
  1462. [ { colspan: 3, contents: 'ba' }, '13' ],
  1463. [ 'ca', 'cb', 'cc', '23' ],
  1464. [ '30', '31', { colspan: 2, contents: '31' } ]
  1465. ] ) );
  1466. /* eslint-disable no-multi-spaces */
  1467. assertSelectedCells( model, [
  1468. [ 1, 1, 0 ],
  1469. [ 1, 0 ],
  1470. [ 1, 1, 1, 0 ],
  1471. [ 0, 0, 0 ]
  1472. ] );
  1473. /* eslint-enable no-multi-spaces */
  1474. } );
  1475. it( 'handles pasting rowspanned table over table with rowspans (no rowspan exceeds selection)', () => {
  1476. setModelData( model, modelTable( [
  1477. [ { rowspan: 3, contents: '00' }, { rowspan: 2, contents: '01' }, '02', '03' ],
  1478. [ { rowspan: 2, contents: '12' }, '13' ],
  1479. [ '21', '23' ],
  1480. [ '30', '31', '32', '33' ]
  1481. ] ) );
  1482. tableSelection.setCellSelection(
  1483. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1484. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  1485. );
  1486. pasteTable( [
  1487. [ 'aa', { rowspan: 3, contents: 'ab' }, { rowspan: 2, contents: 'ac' }, 'ad' ],
  1488. [ { rowspan: 2, contents: 'ba' }, 'bd' ],
  1489. [ 'cc', 'cd' ]
  1490. ] );
  1491. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1492. [ 'aa', { rowspan: 3, contents: 'ab' }, { rowspan: 2, contents: 'ac' }, 'ad' ],
  1493. [ { rowspan: 2, contents: 'ba' }, 'bd' ],
  1494. [ 'cc', 'cd' ],
  1495. [ '30', '31', '32', '33' ]
  1496. ] ) );
  1497. /* eslint-disable no-multi-spaces */
  1498. assertSelectedCells( model, [
  1499. [ 1, 1, 1, 1 ],
  1500. [ 1, 1 ],
  1501. [ 1, 1 ],
  1502. [ 0, 0, 0, 0 ]
  1503. ] );
  1504. /* eslint-enable no-multi-spaces */
  1505. } );
  1506. it( 'handles pasting multi-spanned table over table with multi-spans (no span exceeds selection)', () => {
  1507. // +----+----+----+----+----+----+
  1508. // | 00 | 02 | 03 | 05 |
  1509. // + + + +----+
  1510. // | | | | 15 |
  1511. // +----+----+----+ +----+
  1512. // | 20 | 21 | | 25 |
  1513. // + +----+----+----+----+----+
  1514. // | | 31 | 32 | 34 | 35 |
  1515. // +----+----+----+----+----+----+
  1516. // | 40 | 41 | 42 | 43 | 44 | 45 |
  1517. // +----+----+----+----+----+----+
  1518. setModelData( model, modelTable( [
  1519. [
  1520. { contents: '00', colspan: 2, rowspan: 2 },
  1521. { contents: '02', rowspan: 2 },
  1522. { contents: '03', colspan: 2, rowspan: 3 },
  1523. '05'
  1524. ],
  1525. [ '15' ],
  1526. [ { contents: '20', rowspan: 2 }, { contents: '21', colspan: 2 }, '25' ],
  1527. [ '31', { contents: '32', colspan: 2 }, '34', '35' ],
  1528. [ '40', '41', '42', '43', '44', '45' ]
  1529. ] ) );
  1530. tableSelection.setCellSelection(
  1531. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1532. modelRoot.getNodeByPath( [ 0, 3, 2 ] )
  1533. );
  1534. // +----+----+----+----+----+
  1535. // | aa | ac | ad | ae |
  1536. // +----+----+----+----+ +
  1537. // | ba | bb | |
  1538. // +----+ +----+
  1539. // | ca | | ce |
  1540. // + +----+----+----+----+
  1541. // | | db | dc | dd |
  1542. // +----+----+----+----+----+
  1543. pasteTable( [
  1544. [ { contents: 'aa', colspan: 2 }, 'ac', 'ad', { contents: 'ae', rowspan: 2 } ],
  1545. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 } ],
  1546. [ { contents: 'ca', rowspan: 2 }, 'ce' ],
  1547. [ 'db', 'dc', { contents: 'dd', colspan: 2 } ]
  1548. ] );
  1549. // +----+----+----+----+----+----+
  1550. // | aa | ac | ad | ae | 05 |
  1551. // +----+----+----+----+ +----+
  1552. // | ba | bb | | 15 |
  1553. // +----+ +----+----+
  1554. // | ca | | ce | 25 |
  1555. // + +----+----+----+----+----+
  1556. // | | db | dc | dd | 35 |
  1557. // +----+----+----+----+----+----+
  1558. // | 40 | 41 | 42 | 43 | 44 | 45 |
  1559. // +----+----+----+----+----+----+
  1560. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1561. [ { contents: 'aa', colspan: 2 }, 'ac', 'ad', { contents: 'ae', rowspan: 2 }, '05' ],
  1562. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 }, '15' ],
  1563. [ { contents: 'ca', rowspan: 2 }, 'ce', '25' ],
  1564. [ 'db', 'dc', { contents: 'dd', colspan: 2 }, '35' ],
  1565. [ '40', '41', '42', '43', '44', '45' ]
  1566. ] ) );
  1567. /* eslint-disable no-multi-spaces */
  1568. assertSelectedCells( model, [
  1569. [ 1, 1, 1, 1, 0 ],
  1570. [ 1, 1, 0 ],
  1571. [ 1, 1, 0 ],
  1572. [ 1, 1, 1, 0 ],
  1573. [ 0, 0, 0, 0, 0, 0 ]
  1574. ] );
  1575. /* eslint-enable no-multi-spaces */
  1576. } );
  1577. // TODO: Skipped case - should allow pasting but no tools to compare areas (like in MergeCellsCommand).
  1578. it.skip( 'handles pasting table that has cell with colspan (last row in selection is spanned)', () => {
  1579. // +----+----+----+----+
  1580. // | 00 | 01 | 02 | 03 |
  1581. // +----+----+----+----+
  1582. // | 10 | 11 | 13 |
  1583. // + + +----+
  1584. // | | | 23 |
  1585. // +----+----+----+----+
  1586. // | 30 | 31 | 32 | 33 |
  1587. // +----+----+----+----+
  1588. setModelData( model, modelTable( [
  1589. [ '00', '01', '02', '03' ],
  1590. [ { contents: '10', rowspan: 2 }, { contents: '11', colspan: 2, rowspan: 2 }, '13' ],
  1591. [ '23' ],
  1592. [ '30', '31', '32', '33' ]
  1593. ] ) );
  1594. tableSelection.setCellSelection(
  1595. modelRoot.getNodeByPath( [ 0, 0, 2 ] ),
  1596. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  1597. );
  1598. // +----+----+----+
  1599. // | aa | ab | ac |
  1600. // +----+----+----+
  1601. // | ba | bc |
  1602. // + +----+
  1603. // | | cc |
  1604. // +----+----+----+
  1605. pasteTable( [
  1606. [ 'aa', 'ab', 'ac' ],
  1607. [ { contents: 'ba', colspan: 2, rowspan: 2 }, 'bc' ],
  1608. [ 'cc' ]
  1609. ] );
  1610. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1611. [ '00', '01', '02', '03' ],
  1612. [ '10', { colspan: 2, contents: 'aa' }, '13' ],
  1613. [ '20', 'ba', 'bb', '23' ],
  1614. [ '30', '31', '32', '33' ]
  1615. ] ) );
  1616. /* eslint-disable no-multi-spaces */
  1617. assertSelectedCells( model, [
  1618. [ 0, 0, 0, 0 ],
  1619. [ 0, 1, 0 ],
  1620. [ 0, 1, 1, 0 ],
  1621. [ 0, 0, 0, 0 ]
  1622. ] );
  1623. /* eslint-enable no-multi-spaces */
  1624. } );
  1625. } );
  1626. } );
  1627. } );
  1628. } );
  1629. function assertClipboardContentOnMethod( method, expectedViewTable ) {
  1630. const data = {
  1631. dataTransfer: createDataTransfer(),
  1632. preventDefault: sinon.spy()
  1633. };
  1634. viewDocument.fire( method, data );
  1635. expect( data.dataTransfer.getData( 'text/html' ) ).to.equal( expectedViewTable );
  1636. }
  1637. function pasteTable( tableData ) {
  1638. const data = {
  1639. dataTransfer: createDataTransfer(),
  1640. preventDefault: sinon.spy(),
  1641. stopPropagation: sinon.spy()
  1642. };
  1643. data.dataTransfer.setData( 'text/html', viewTable( tableData ) );
  1644. viewDocument.fire( 'paste', data );
  1645. return data;
  1646. }
  1647. function createDataTransfer() {
  1648. const store = new Map();
  1649. return {
  1650. setData( type, data ) {
  1651. store.set( type, data );
  1652. },
  1653. getData( type ) {
  1654. return store.get( type );
  1655. }
  1656. };
  1657. }
  1658. } );