tableclipboard-paste.js 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447
  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 console */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import BlockQuoteEditing from '@ckeditor/ckeditor5-block-quote/src/blockquoteediting';
  8. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  9. import HorizontalLineEditing from '@ckeditor/ckeditor5-horizontal-line/src/horizontallineediting';
  10. import ImageCaptionEditing from '@ckeditor/ckeditor5-image/src/imagecaption/imagecaptionediting';
  11. import ImageEditing from '@ckeditor/ckeditor5-image/src/image/imageediting';
  12. import ListEditing from '@ckeditor/ckeditor5-list/src/listediting';
  13. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  14. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  15. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  16. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  17. import { assertSelectedCells, modelTable, viewTable } from './_utils/utils';
  18. import TableEditing from '../src/tableediting';
  19. import TableCellPropertiesEditing from '../src/tablecellproperties/tablecellpropertiesediting';
  20. import TableClipboard from '../src/tableclipboard';
  21. describe( 'table clipboard', () => {
  22. let editor, model, modelRoot, tableSelection, viewDocument, element;
  23. testUtils.createSinonSandbox();
  24. beforeEach( () => {
  25. element = document.createElement( 'div' );
  26. document.body.appendChild( element );
  27. } );
  28. afterEach( async () => {
  29. await editor.destroy();
  30. element.remove();
  31. } );
  32. describe( 'Clipboard integration - paste (selection scenarios)', () => {
  33. beforeEach( async () => {
  34. await createEditor();
  35. setModelData( model, modelTable( [
  36. [ '00[]', '01', '02', '03' ],
  37. [ '10', '11', '12', '13' ],
  38. [ '20', '21', '22', '23' ],
  39. [ '30', '31', '32', '33' ]
  40. ] ) );
  41. } );
  42. it( 'should be disabled in a readonly mode', () => {
  43. editor.isReadOnly = true;
  44. tableSelection.setCellSelection(
  45. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  46. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  47. );
  48. const data = pasteTable( [
  49. [ 'aa', 'ab' ],
  50. [ 'ba', 'bb' ]
  51. ] );
  52. editor.isReadOnly = false;
  53. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  54. [ '00', '01', '02', '03' ],
  55. [ '10', '11', '12', '13' ],
  56. [ '20', '21', '22', '23' ],
  57. [ '30', '31', '32', '33' ]
  58. ] ) );
  59. sinon.assert.calledOnce( data.preventDefault );
  60. } );
  61. it( 'should allow normal paste if no table cells are selected', () => {
  62. const data = {
  63. dataTransfer: createDataTransfer(),
  64. preventDefault: sinon.spy(),
  65. stopPropagation: sinon.spy()
  66. };
  67. data.dataTransfer.setData( 'text/html', '<p>foo</p>' );
  68. viewDocument.fire( 'paste', data );
  69. editor.isReadOnly = false;
  70. assertEqualMarkup( getModelData( model ), modelTable( [
  71. [ '00foo[]', '01', '02', '03' ],
  72. [ '10', '11', '12', '13' ],
  73. [ '20', '21', '22', '23' ],
  74. [ '30', '31', '32', '33' ]
  75. ] ) );
  76. } );
  77. it( 'should block non-rectangular selection', () => {
  78. setModelData( model, modelTable( [
  79. [ { contents: '00', colspan: 3 } ],
  80. [ '10', '11', '12' ],
  81. [ '20', '21', '22' ]
  82. ] ) );
  83. tableSelection.setCellSelection(
  84. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  85. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  86. );
  87. // Catches the temporary console log in the CK_DEBUG mode.
  88. sinon.stub( console, 'log' );
  89. pasteTable( [
  90. [ 'aa', 'ab' ],
  91. [ 'ba', 'bb' ]
  92. ] );
  93. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  94. [ { contents: '00', colspan: 3 } ],
  95. [ '10', '11', '12' ],
  96. [ '20', '21', '22' ]
  97. ] ) );
  98. } );
  99. describe( 'single cell selected', () => {
  100. it( 'blocks this case', () => {
  101. setModelData( model, modelTable( [
  102. [ '00', '01', '02' ],
  103. [ '10', '11', '12' ],
  104. [ '20', '21', '22' ]
  105. ] ) );
  106. tableSelection.setCellSelection(
  107. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  108. modelRoot.getNodeByPath( [ 0, 0, 0 ] )
  109. );
  110. // Catches the temporary console log in the CK_DEBUG mode.
  111. sinon.stub( console, 'log' );
  112. pasteTable( [
  113. [ 'aa', 'ab' ],
  114. [ 'ba', 'bb' ]
  115. ] );
  116. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  117. [ '00', '01', '02' ],
  118. [ '10', '11', '12' ],
  119. [ '20', '21', '22' ]
  120. ] ) );
  121. } );
  122. } );
  123. describe( 'pasted table is equal to the selected area', () => {
  124. describe( 'no spans', () => {
  125. it( 'handles simple table paste to a simple table fragment - at the beginning of a table', () => {
  126. tableSelection.setCellSelection(
  127. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  128. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  129. );
  130. pasteTable( [
  131. [ 'aa', 'ab' ],
  132. [ 'ba', 'bb' ]
  133. ] );
  134. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  135. [ 'aa', 'ab', '02', '03' ],
  136. [ 'ba', 'bb', '12', '13' ],
  137. [ '20', '21', '22', '23' ],
  138. [ '30', '31', '32', '33' ]
  139. ] ) );
  140. assertSelectedCells( model, [
  141. [ 1, 1, 0, 0 ],
  142. [ 1, 1, 0, 0 ],
  143. [ 0, 0, 0, 0 ],
  144. [ 0, 0, 0, 0 ]
  145. ] );
  146. } );
  147. it( 'handles simple table paste to a simple table fragment - at the end of a table', () => {
  148. tableSelection.setCellSelection(
  149. modelRoot.getNodeByPath( [ 0, 2, 2 ] ),
  150. modelRoot.getNodeByPath( [ 0, 3, 3 ] )
  151. );
  152. pasteTable( [
  153. [ 'aa', 'ab' ],
  154. [ 'ba', 'bb' ]
  155. ] );
  156. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  157. [ '00', '01', '02', '03' ],
  158. [ '10', '11', '12', '13' ],
  159. [ '20', '21', 'aa', 'ab' ],
  160. [ '30', '31', 'ba', 'bb' ]
  161. ] ) );
  162. assertSelectedCells( model, [
  163. [ 0, 0, 0, 0 ],
  164. [ 0, 0, 0, 0 ],
  165. [ 0, 0, 1, 1 ],
  166. [ 0, 0, 1, 1 ]
  167. ] );
  168. } );
  169. it( 'handles simple table paste to a simple table fragment - in the middle of a table', () => {
  170. tableSelection.setCellSelection(
  171. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  172. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  173. );
  174. pasteTable( [
  175. [ 'aa', 'ab' ],
  176. [ 'ba', 'bb' ]
  177. ] );
  178. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  179. [ '00', '01', '02', '03' ],
  180. [ '10', 'aa', 'ab', '13' ],
  181. [ '20', 'ba', 'bb', '23' ],
  182. [ '30', '31', '32', '33' ]
  183. ] ) );
  184. assertSelectedCells( model, [
  185. [ 0, 0, 0, 0 ],
  186. [ 0, 1, 1, 0 ],
  187. [ 0, 1, 1, 0 ],
  188. [ 0, 0, 0, 0 ]
  189. ] );
  190. } );
  191. it( 'handles simple row paste to a simple row fragment - in the middle of a table', () => {
  192. tableSelection.setCellSelection(
  193. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  194. modelRoot.getNodeByPath( [ 0, 1, 2 ] )
  195. );
  196. pasteTable( [
  197. [ 'aa', 'ab' ]
  198. ] );
  199. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  200. [ '00', '01', '02', '03' ],
  201. [ '10', 'aa', 'ab', '13' ],
  202. [ '20', '21', '22', '23' ],
  203. [ '30', '31', '32', '33' ]
  204. ] ) );
  205. assertSelectedCells( model, [
  206. [ 0, 0, 0, 0 ],
  207. [ 0, 1, 1, 0 ],
  208. [ 0, 0, 0, 0 ],
  209. [ 0, 0, 0, 0 ]
  210. ] );
  211. } );
  212. it( 'handles simple column paste to a simple column fragment - in the middle of a table', () => {
  213. tableSelection.setCellSelection(
  214. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  215. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  216. );
  217. pasteTable( [
  218. [ 'aa' ],
  219. [ 'ba' ]
  220. ] );
  221. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  222. [ '00', '01', '02', '03' ],
  223. [ '10', 'aa', '12', '13' ],
  224. [ '20', 'ba', '22', '23' ],
  225. [ '30', '31', '32', '33' ]
  226. ] ) );
  227. assertSelectedCells( model, [
  228. [ 0, 0, 0, 0 ],
  229. [ 0, 1, 0, 0 ],
  230. [ 0, 1, 0, 0 ],
  231. [ 0, 0, 0, 0 ]
  232. ] );
  233. } );
  234. it( 'handles simple table paste to a simple table fragment - whole table selected', () => {
  235. tableSelection.setCellSelection(
  236. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  237. modelRoot.getNodeByPath( [ 0, 3, 3 ] )
  238. );
  239. pasteTable( [
  240. [ 'aa', 'ab', 'ac', 'ad' ],
  241. [ 'ba', 'bb', 'bc', 'bd' ],
  242. [ 'ca', 'cb', 'cc', 'cd' ],
  243. [ 'da', 'db', 'dc', 'dd' ]
  244. ] );
  245. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  246. [ 'aa', 'ab', 'ac', 'ad' ],
  247. [ 'ba', 'bb', 'bc', 'bd' ],
  248. [ 'ca', 'cb', 'cc', 'cd' ],
  249. [ 'da', 'db', 'dc', 'dd' ]
  250. ] ) );
  251. assertSelectedCells( model, [
  252. [ 1, 1, 1, 1 ],
  253. [ 1, 1, 1, 1 ],
  254. [ 1, 1, 1, 1 ],
  255. [ 1, 1, 1, 1 ]
  256. ] );
  257. } );
  258. } );
  259. describe( 'pasted table has spans', () => {
  260. it( 'handles pasting table that has cell with colspan', () => {
  261. tableSelection.setCellSelection(
  262. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  263. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  264. );
  265. pasteTable( [
  266. [ { colspan: 2, contents: 'aa' } ],
  267. [ 'ba', 'bb' ]
  268. ] );
  269. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  270. [ '00', '01', '02', '03' ],
  271. [ '10', { colspan: 2, contents: 'aa' }, '13' ],
  272. [ '20', 'ba', 'bb', '23' ],
  273. [ '30', '31', '32', '33' ]
  274. ] ) );
  275. /* eslint-disable no-multi-spaces */
  276. assertSelectedCells( model, [
  277. [ 0, 0, 0, 0 ],
  278. [ 0, 1, 0 ],
  279. [ 0, 1, 1, 0 ],
  280. [ 0, 0, 0, 0 ]
  281. ] );
  282. /* eslint-enable no-multi-spaces */
  283. } );
  284. it( 'handles pasting table that has many cells with various colspan', () => {
  285. tableSelection.setCellSelection(
  286. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  287. modelRoot.getNodeByPath( [ 0, 3, 2 ] )
  288. );
  289. pasteTable( [
  290. [ 'aa', { colspan: 2, contents: 'ab' } ],
  291. [ { colspan: 3, contents: 'ba' } ],
  292. [ 'ca', 'cb', 'cc' ],
  293. [ { colspan: 2, contents: 'da' }, 'dc' ]
  294. ] );
  295. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  296. [ 'aa', { colspan: 2, contents: 'ab' }, '03' ],
  297. [ { colspan: 3, contents: 'ba' }, '13' ],
  298. [ 'ca', 'cb', 'cc', '23' ],
  299. [ { colspan: 2, contents: 'da' }, 'dc', '33' ]
  300. ] ) );
  301. /* eslint-disable no-multi-spaces */
  302. assertSelectedCells( model, [
  303. [ 1, 1, 0 ],
  304. [ 1, 0 ],
  305. [ 1, 1, 1, 0 ],
  306. [ 1, 1, 0 ]
  307. ] );
  308. /* eslint-enable no-multi-spaces */
  309. } );
  310. it( 'handles pasting table that has cell with rowspan', () => {
  311. tableSelection.setCellSelection(
  312. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  313. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  314. );
  315. pasteTable( [
  316. [ { rowspan: 2, contents: 'aa' }, 'ab' ],
  317. [ 'bb' ]
  318. ] );
  319. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  320. [ '00', '01', '02', '03' ],
  321. [ '10', { rowspan: 2, contents: 'aa' }, 'ab', '13' ],
  322. [ '20', 'bb', '23' ],
  323. [ '30', '31', '32', '33' ]
  324. ] ) );
  325. /* eslint-disable no-multi-spaces */
  326. assertSelectedCells( model, [
  327. [ 0, 0, 0, 0 ],
  328. [ 0, 1, 1, 0 ],
  329. [ 0, 1, 0 ],
  330. [ 0, 0, 0, 0 ]
  331. ] );
  332. /* eslint-enable no-multi-spaces */
  333. } );
  334. it( 'handles pasting table that has many cells with various rowspan', () => {
  335. tableSelection.setCellSelection(
  336. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  337. modelRoot.getNodeByPath( [ 0, 2, 3 ] )
  338. );
  339. pasteTable( [
  340. [ 'aa', { rowspan: 3, contents: 'ab' }, { rowspan: 2, contents: 'ac' }, 'ad' ],
  341. [ { rowspan: 2, contents: 'ba' }, 'bd' ],
  342. [ 'cc', 'cd' ]
  343. ] );
  344. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  345. [ 'aa', { rowspan: 3, contents: 'ab' }, { rowspan: 2, contents: 'ac' }, 'ad' ],
  346. [ { rowspan: 2, contents: 'ba' }, 'bd' ],
  347. [ 'cc', 'cd' ],
  348. [ '30', '31', '32', '33' ]
  349. ] ) );
  350. /* eslint-disable no-multi-spaces */
  351. assertSelectedCells( model, [
  352. [ 1, 1, 1, 1 ],
  353. [ 1, 1 ],
  354. [ 1, 1 ],
  355. [ 0, 0, 0 ]
  356. ] );
  357. /* eslint-enable no-multi-spaces */
  358. } );
  359. it( 'handles pasting multi-spanned table', () => {
  360. setModelData( model, modelTable( [
  361. [ '00', '01', '02', '03', '04', '05' ],
  362. [ '10', '11', '12', '13', '14', '15' ],
  363. [ '20', '21', '22', '23', '24', '25' ],
  364. [ '30', '31', '32', '33', '34', '35' ],
  365. [ '40', '41', '42', '43', '44', '45' ]
  366. ] ) );
  367. tableSelection.setCellSelection(
  368. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  369. modelRoot.getNodeByPath( [ 0, 3, 4 ] )
  370. );
  371. // +----+----+----+----+----+
  372. // | aa | ac | ad | ae |
  373. // +----+----+----+----+ +
  374. // | ba | bb | |
  375. // +----+ +----+
  376. // | ca | | ce |
  377. // + +----+----+----+----+
  378. // | | db | dc | dd |
  379. // +----+----+----+----+----+
  380. pasteTable( [
  381. [ { contents: 'aa', colspan: 2 }, 'ac', 'ad', { contents: 'ae', rowspan: 2 } ],
  382. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 } ],
  383. [ { contents: 'ca', rowspan: 2 }, 'ce' ],
  384. [ 'db', 'dc', { contents: 'dd', colspan: 2 } ]
  385. ] );
  386. // +----+----+----+----+----+----+
  387. // | aa | ac | ad | ae | 05 |
  388. // +----+----+----+----+ +----+
  389. // | ba | bb | | 15 |
  390. // +----+ +----+----+
  391. // | ca | | ce | 25 |
  392. // + +----+----+----+----+----+
  393. // | | db | dc | dd | 35 |
  394. // +----+----+----+----+----+----+
  395. // | 40 | 41 | 42 | 43 | 44 | 45 |
  396. // +----+----+----+----+----+----+
  397. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  398. [ { contents: 'aa', colspan: 2 }, 'ac', 'ad', { contents: 'ae', rowspan: 2 }, '05' ],
  399. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 }, '15' ],
  400. [ { contents: 'ca', rowspan: 2 }, 'ce', '25' ],
  401. [ 'db', 'dc', { contents: 'dd', colspan: 2 }, '35' ],
  402. [ '40', '41', '42', '43', '44', '45' ]
  403. ] ) );
  404. /* eslint-disable no-multi-spaces */
  405. assertSelectedCells( model, [
  406. [ 1, 1, 1, 1, 0 ],
  407. [ 1, 1, 0 ],
  408. [ 1, 1, 0 ],
  409. [ 1, 1, 1, 0 ],
  410. [ 0, 0, 0, 0, 0, 0 ]
  411. ] );
  412. /* eslint-enable no-multi-spaces */
  413. } );
  414. } );
  415. describe( 'content table has spans', () => {
  416. it( 'handles pasting simple table over a table with colspans (no colspan exceeds selection)', () => {
  417. setModelData( model, modelTable( [
  418. [ '00[]', '01', '02', '03' ],
  419. [ { colspan: 3, contents: '10' }, '13' ],
  420. [ { colspan: 2, contents: '20' }, '22', '23' ],
  421. [ '30', '31', { colspan: 2, contents: '31' } ]
  422. ] ) );
  423. tableSelection.setCellSelection(
  424. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  425. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  426. );
  427. pasteTable( [
  428. [ 'aa', 'ab', 'ac' ],
  429. [ 'ba', 'bb', 'bc' ],
  430. [ 'ca', 'cb', 'cc' ]
  431. ] );
  432. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  433. [ 'aa', 'ab', 'ac', '03' ],
  434. [ 'ba', 'bb', 'bc', '13' ],
  435. [ 'ca', 'cb', 'cc', '23' ],
  436. [ '30', '31', { colspan: 2, contents: '31' } ]
  437. ] ) );
  438. /* eslint-disable no-multi-spaces */
  439. assertSelectedCells( model, [
  440. [ 1, 1, 1, 0 ],
  441. [ 1, 1, 1, 0 ],
  442. [ 1, 1, 1, 0 ],
  443. [ 0, 0, 0 ]
  444. ] );
  445. /* eslint-enable no-multi-spaces */
  446. } );
  447. it( 'handles pasting simple table over a table with rowspans (no rowspan exceeds selection)', () => {
  448. setModelData( model, modelTable( [
  449. [ '00', { rowspan: 3, contents: '01' }, { rowspan: 2, contents: '02' }, '03' ],
  450. [ { rowspan: 2, contents: '10' }, '13' ],
  451. [ '22', '23' ],
  452. [ '30', '31', '32', '33' ]
  453. ] ) );
  454. tableSelection.setCellSelection(
  455. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  456. modelRoot.getNodeByPath( [ 0, 2, 0 ] )
  457. );
  458. pasteTable( [
  459. [ 'aa', 'ab', 'ac' ],
  460. [ 'ba', 'bb', 'bc' ],
  461. [ 'ca', 'cb', 'cc' ]
  462. ] );
  463. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  464. [ 'aa', 'ab', 'ac', '03' ],
  465. [ 'ba', 'bb', 'bc', '13' ],
  466. [ 'ca', 'cb', 'cc', '23' ],
  467. [ '30', '31', '32', '33' ]
  468. ] ) );
  469. assertSelectedCells( model, [
  470. [ 1, 1, 1, 0 ],
  471. [ 1, 1, 1, 0 ],
  472. [ 1, 1, 1, 0 ],
  473. [ 0, 0, 0, 0 ]
  474. ] );
  475. } );
  476. it( 'handles pasting simple table over table with multi-spans (no span exceeds selection)', () => {
  477. // +----+----+----+----+----+----+
  478. // | 00 | 02 | 03 | 05 |
  479. // + + + +----+
  480. // | | | | 15 |
  481. // +----+----+----+ +----+
  482. // | 20 | 21 | | 25 |
  483. // + +----+----+----+----+----+
  484. // | | 31 | 32 | 34 | 35 |
  485. // +----+----+----+----+----+----+
  486. // | 40 | 41 | 42 | 43 | 44 | 45 |
  487. // +----+----+----+----+----+----+
  488. setModelData( model, modelTable( [
  489. [
  490. { contents: '00', colspan: 2, rowspan: 2 },
  491. { contents: '02', rowspan: 2 },
  492. { contents: '03', colspan: 2, rowspan: 3 },
  493. '05'
  494. ],
  495. [ '15' ],
  496. [ { contents: '20', rowspan: 2 }, { contents: '21', colspan: 2 }, '25' ],
  497. [ '31', { contents: '32', colspan: 2 }, '34', '35' ],
  498. [ '40', '41', '42', '43', '44', '45' ]
  499. ] ) );
  500. tableSelection.setCellSelection(
  501. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  502. modelRoot.getNodeByPath( [ 0, 3, 2 ] )
  503. );
  504. pasteTable( [
  505. [ 'aa', 'ab', 'ac', 'ad', 'ae' ],
  506. [ 'ba', 'bb', 'bc', 'bd', 'be' ],
  507. [ 'ca', 'cb', 'cc', 'cd', 'ce' ],
  508. [ 'da', 'db', 'dc', 'dd', 'de' ]
  509. ] );
  510. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  511. [ 'aa', 'ab', 'ac', 'ad', 'ae', '05' ],
  512. [ 'ba', 'bb', 'bc', 'bd', 'be', '15' ],
  513. [ 'ca', 'cb', 'cc', 'cd', 'ce', '25' ],
  514. [ 'da', 'db', 'dc', 'dd', 'de', '35' ],
  515. [ '40', '41', '42', '43', '44', '45' ]
  516. ] ) );
  517. assertSelectedCells( model, [
  518. [ 1, 1, 1, 1, 1, 0 ],
  519. [ 1, 1, 1, 1, 1, 0 ],
  520. [ 1, 1, 1, 1, 1, 0 ],
  521. [ 1, 1, 1, 1, 1, 0 ],
  522. [ 0, 0, 0, 0, 0, 0 ]
  523. ] );
  524. } );
  525. // TODO: Skipped case - should allow pasting but no tools to compare areas (like in MergeCellsCommand).
  526. it.skip( 'handles pasting table that has cell with colspan (last row in selection is spanned)', () => {
  527. // +----+----+----+----+
  528. // | 00 | 01 | 02 | 03 |
  529. // +----+----+----+----+
  530. // | 10 | 11 | 13 |
  531. // + + +----+
  532. // | | | 23 |
  533. // +----+----+----+----+
  534. // | 30 | 31 | 32 | 33 |
  535. // +----+----+----+----+
  536. setModelData( model, modelTable( [
  537. [ '00', '01', '02', '03' ],
  538. [ { contents: '10', rowspan: 2 }, { contents: '11', colspan: 2, rowspan: 2 }, '13' ],
  539. [ '23' ],
  540. [ '30', '31', '32', '33' ]
  541. ] ) );
  542. tableSelection.setCellSelection(
  543. modelRoot.getNodeByPath( [ 0, 0, 2 ] ),
  544. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  545. );
  546. pasteTable( [
  547. [ 'aa', 'ab', 'ac' ],
  548. [ 'ba', 'bb', 'bc' ],
  549. [ 'ca', 'cb', 'cc' ]
  550. ] );
  551. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  552. [ 'aa', 'ab', 'ac', '03' ],
  553. [ 'ba', 'bb', 'bc', '13' ],
  554. [ 'ca', 'cb', 'cc', '23' ],
  555. [ '30', '31', '32', '33' ]
  556. ] ) );
  557. assertSelectedCells( model, [
  558. [ 1, 1, 1, 0 ],
  559. [ 1, 1, 1, 0 ],
  560. [ 1, 1, 1, 0 ],
  561. [ 0, 0, 0, 0 ]
  562. ] );
  563. } );
  564. } );
  565. describe( 'content and paste tables have spans', () => {
  566. it( 'handles pasting colspanned table over table with colspans (no colspan exceeds selection)', () => {
  567. setModelData( model, modelTable( [
  568. [ '00[]', '01', '02', '03' ],
  569. [ { colspan: 3, contents: '10' }, '13' ],
  570. [ { colspan: 2, contents: '20' }, '22', '23' ],
  571. [ '30', '31', { colspan: 2, contents: '31' } ]
  572. ] ) );
  573. tableSelection.setCellSelection(
  574. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  575. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  576. );
  577. pasteTable( [
  578. [ 'aa', { colspan: 2, contents: 'ab' } ],
  579. [ { colspan: 3, contents: 'ba' } ],
  580. [ 'ca', 'cb', 'cc' ]
  581. ] );
  582. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  583. [ 'aa', { colspan: 2, contents: 'ab' }, '03' ],
  584. [ { colspan: 3, contents: 'ba' }, '13' ],
  585. [ 'ca', 'cb', 'cc', '23' ],
  586. [ '30', '31', { colspan: 2, contents: '31' } ]
  587. ] ) );
  588. /* eslint-disable no-multi-spaces */
  589. assertSelectedCells( model, [
  590. [ 1, 1, 0 ],
  591. [ 1, 0 ],
  592. [ 1, 1, 1, 0 ],
  593. [ 0, 0, 0 ]
  594. ] );
  595. /* eslint-enable no-multi-spaces */
  596. } );
  597. it( 'handles pasting rowspanned table over table with rowspans (no rowspan exceeds selection)', () => {
  598. setModelData( model, modelTable( [
  599. [ { rowspan: 3, contents: '00' }, { rowspan: 2, contents: '01' }, '02', '03' ],
  600. [ { rowspan: 2, contents: '12' }, '13' ],
  601. [ '21', '23' ],
  602. [ '30', '31', '32', '33' ]
  603. ] ) );
  604. tableSelection.setCellSelection(
  605. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  606. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  607. );
  608. pasteTable( [
  609. [ 'aa', { rowspan: 3, contents: 'ab' }, { rowspan: 2, contents: 'ac' }, 'ad' ],
  610. [ { rowspan: 2, contents: 'ba' }, 'bd' ],
  611. [ 'cc', 'cd' ]
  612. ] );
  613. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  614. [ 'aa', { rowspan: 3, contents: 'ab' }, { rowspan: 2, contents: 'ac' }, 'ad' ],
  615. [ { rowspan: 2, contents: 'ba' }, 'bd' ],
  616. [ 'cc', 'cd' ],
  617. [ '30', '31', '32', '33' ]
  618. ] ) );
  619. /* eslint-disable no-multi-spaces */
  620. assertSelectedCells( model, [
  621. [ 1, 1, 1, 1 ],
  622. [ 1, 1 ],
  623. [ 1, 1 ],
  624. [ 0, 0, 0, 0 ]
  625. ] );
  626. /* eslint-enable no-multi-spaces */
  627. } );
  628. it( 'handles pasting multi-spanned table over table with multi-spans (no span exceeds selection)', () => {
  629. // +----+----+----+----+----+----+
  630. // | 00 | 02 | 03 | 05 |
  631. // + + + +----+
  632. // | | | | 15 |
  633. // +----+----+----+ +----+
  634. // | 20 | 21 | | 25 |
  635. // + +----+----+----+----+----+
  636. // | | 31 | 32 | 34 | 35 |
  637. // +----+----+----+----+----+----+
  638. // | 40 | 41 | 42 | 43 | 44 | 45 |
  639. // +----+----+----+----+----+----+
  640. setModelData( model, modelTable( [
  641. [
  642. { contents: '00', colspan: 2, rowspan: 2 },
  643. { contents: '02', rowspan: 2 },
  644. { contents: '03', colspan: 2, rowspan: 3 },
  645. '05'
  646. ],
  647. [ '15' ],
  648. [ { contents: '20', rowspan: 2 }, { contents: '21', colspan: 2 }, '25' ],
  649. [ '31', { contents: '32', colspan: 2 }, '34', '35' ],
  650. [ '40', '41', '42', '43', '44', '45' ]
  651. ] ) );
  652. tableSelection.setCellSelection(
  653. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  654. modelRoot.getNodeByPath( [ 0, 3, 2 ] )
  655. );
  656. // +----+----+----+----+----+
  657. // | aa | ac | ad | ae |
  658. // +----+----+----+----+ +
  659. // | ba | bb | |
  660. // +----+ +----+
  661. // | ca | | ce |
  662. // + +----+----+----+----+
  663. // | | db | dc | dd |
  664. // +----+----+----+----+----+
  665. pasteTable( [
  666. [ { contents: 'aa', colspan: 2 }, 'ac', 'ad', { contents: 'ae', rowspan: 2 } ],
  667. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 } ],
  668. [ { contents: 'ca', rowspan: 2 }, 'ce' ],
  669. [ 'db', 'dc', { contents: 'dd', colspan: 2 } ]
  670. ] );
  671. // +----+----+----+----+----+----+
  672. // | aa | ac | ad | ae | 05 |
  673. // +----+----+----+----+ +----+
  674. // | ba | bb | | 15 |
  675. // +----+ +----+----+
  676. // | ca | | ce | 25 |
  677. // + +----+----+----+----+----+
  678. // | | db | dc | dd | 35 |
  679. // +----+----+----+----+----+----+
  680. // | 40 | 41 | 42 | 43 | 44 | 45 |
  681. // +----+----+----+----+----+----+
  682. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  683. [ { contents: 'aa', colspan: 2 }, 'ac', 'ad', { contents: 'ae', rowspan: 2 }, '05' ],
  684. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 }, '15' ],
  685. [ { contents: 'ca', rowspan: 2 }, 'ce', '25' ],
  686. [ 'db', 'dc', { contents: 'dd', colspan: 2 }, '35' ],
  687. [ '40', '41', '42', '43', '44', '45' ]
  688. ] ) );
  689. /* eslint-disable no-multi-spaces */
  690. assertSelectedCells( model, [
  691. [ 1, 1, 1, 1, 0 ],
  692. [ 1, 1, 0 ],
  693. [ 1, 1, 0 ],
  694. [ 1, 1, 1, 0 ],
  695. [ 0, 0, 0, 0, 0, 0 ]
  696. ] );
  697. /* eslint-enable no-multi-spaces */
  698. } );
  699. // TODO: Skipped case - should allow pasting but no tools to compare areas (like in MergeCellsCommand).
  700. it.skip( 'handles pasting table that has cell with colspan (last row in selection is spanned)', () => {
  701. // +----+----+----+----+
  702. // | 00 | 01 | 02 | 03 |
  703. // +----+----+----+----+
  704. // | 10 | 11 | 13 |
  705. // + + +----+
  706. // | | | 23 |
  707. // +----+----+----+----+
  708. // | 30 | 31 | 32 | 33 |
  709. // +----+----+----+----+
  710. setModelData( model, modelTable( [
  711. [ '00', '01', '02', '03' ],
  712. [ { contents: '10', rowspan: 2 }, { contents: '11', colspan: 2, rowspan: 2 }, '13' ],
  713. [ '23' ],
  714. [ '30', '31', '32', '33' ]
  715. ] ) );
  716. tableSelection.setCellSelection(
  717. modelRoot.getNodeByPath( [ 0, 0, 2 ] ),
  718. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  719. );
  720. // +----+----+----+
  721. // | aa | ab | ac |
  722. // +----+----+----+
  723. // | ba | bc |
  724. // + +----+
  725. // | | cc |
  726. // +----+----+----+
  727. pasteTable( [
  728. [ 'aa', 'ab', 'ac' ],
  729. [ { contents: 'ba', colspan: 2, rowspan: 2 }, 'bc' ],
  730. [ 'cc' ]
  731. ] );
  732. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  733. [ '00', '01', '02', '03' ],
  734. [ '10', { colspan: 2, contents: 'aa' }, '13' ],
  735. [ '20', 'ba', 'bb', '23' ],
  736. [ '30', '31', '32', '33' ]
  737. ] ) );
  738. /* eslint-disable no-multi-spaces */
  739. assertSelectedCells( model, [
  740. [ 0, 0, 0, 0 ],
  741. [ 0, 1, 0 ],
  742. [ 0, 1, 1, 0 ],
  743. [ 0, 0, 0, 0 ]
  744. ] );
  745. /* eslint-enable no-multi-spaces */
  746. } );
  747. } );
  748. } );
  749. describe( 'pasted table is bigger than the selected area', () => {
  750. describe( 'no spans', () => {
  751. it( 'handles simple table paste to a simple table fragment - at the beginning of a table', () => {
  752. tableSelection.setCellSelection(
  753. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  754. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  755. );
  756. pasteTable( [
  757. [ 'aa', 'ab', 'ac' ],
  758. [ 'ba', 'bb', 'bc' ],
  759. [ 'ca', 'cb', 'cc' ]
  760. ] );
  761. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  762. [ 'aa', 'ab', '02', '03' ],
  763. [ 'ba', 'bb', '12', '13' ],
  764. [ '20', '21', '22', '23' ],
  765. [ '30', '31', '32', '33' ]
  766. ] ) );
  767. assertSelectedCells( model, [
  768. [ 1, 1, 0, 0 ],
  769. [ 1, 1, 0, 0 ],
  770. [ 0, 0, 0, 0 ],
  771. [ 0, 0, 0, 0 ]
  772. ] );
  773. } );
  774. it( 'handles simple table paste to a simple table fragment - at the end of a table', () => {
  775. tableSelection.setCellSelection(
  776. modelRoot.getNodeByPath( [ 0, 2, 2 ] ),
  777. modelRoot.getNodeByPath( [ 0, 3, 3 ] )
  778. );
  779. pasteTable( [
  780. [ 'aa', 'ab', 'ac' ],
  781. [ 'ba', 'bb', 'bc' ],
  782. [ 'ca', 'cb', 'cc' ]
  783. ] );
  784. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  785. [ '00', '01', '02', '03' ],
  786. [ '10', '11', '12', '13' ],
  787. [ '20', '21', 'aa', 'ab' ],
  788. [ '30', '31', 'ba', 'bb' ]
  789. ] ) );
  790. assertSelectedCells( model, [
  791. [ 0, 0, 0, 0 ],
  792. [ 0, 0, 0, 0 ],
  793. [ 0, 0, 1, 1 ],
  794. [ 0, 0, 1, 1 ]
  795. ] );
  796. } );
  797. it( 'handles simple table paste to a simple table fragment - in the middle of a table', () => {
  798. tableSelection.setCellSelection(
  799. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  800. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  801. );
  802. pasteTable( [
  803. [ 'aa', 'ab', 'ac' ],
  804. [ 'ba', 'bb', 'bc' ],
  805. [ 'ca', 'cb', 'cc' ]
  806. ] );
  807. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  808. [ '00', '01', '02', '03' ],
  809. [ '10', 'aa', 'ab', '13' ],
  810. [ '20', 'ba', 'bb', '23' ],
  811. [ '30', '31', '32', '33' ]
  812. ] ) );
  813. assertSelectedCells( model, [
  814. [ 0, 0, 0, 0 ],
  815. [ 0, 1, 1, 0 ],
  816. [ 0, 1, 1, 0 ],
  817. [ 0, 0, 0, 0 ]
  818. ] );
  819. } );
  820. it( 'handles paste to a simple row fragment - in the middle of a table', () => {
  821. tableSelection.setCellSelection(
  822. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  823. modelRoot.getNodeByPath( [ 0, 1, 2 ] )
  824. );
  825. pasteTable( [
  826. [ 'aa', 'ab', 'ac' ],
  827. [ 'ba', 'bb', 'bc' ],
  828. [ 'ca', 'cb', 'cc' ]
  829. ] );
  830. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  831. [ '00', '01', '02', '03' ],
  832. [ '10', 'aa', 'ab', '13' ],
  833. [ '20', '21', '22', '23' ],
  834. [ '30', '31', '32', '33' ]
  835. ] ) );
  836. assertSelectedCells( model, [
  837. [ 0, 0, 0, 0 ],
  838. [ 0, 1, 1, 0 ],
  839. [ 0, 0, 0, 0 ],
  840. [ 0, 0, 0, 0 ]
  841. ] );
  842. } );
  843. it( 'handles paste to a simple column fragment - in the middle of a table', () => {
  844. tableSelection.setCellSelection(
  845. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  846. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  847. );
  848. pasteTable( [
  849. [ 'aa', 'ab', 'ac' ],
  850. [ 'ba', 'bb', 'bc' ],
  851. [ 'ca', 'cb', 'cc' ]
  852. ] );
  853. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  854. [ '00', '01', '02', '03' ],
  855. [ '10', 'aa', '12', '13' ],
  856. [ '20', 'ba', '22', '23' ],
  857. [ '30', '31', '32', '33' ]
  858. ] ) );
  859. assertSelectedCells( model, [
  860. [ 0, 0, 0, 0 ],
  861. [ 0, 1, 0, 0 ],
  862. [ 0, 1, 0, 0 ],
  863. [ 0, 0, 0, 0 ]
  864. ] );
  865. } );
  866. it( 'handles simple table paste to a simple table fragment - whole table selected', () => {
  867. tableSelection.setCellSelection(
  868. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  869. modelRoot.getNodeByPath( [ 0, 3, 3 ] )
  870. );
  871. pasteTable( [
  872. [ 'aa', 'ab', 'ac', 'ad', 'ae' ],
  873. [ 'ba', 'bb', 'bc', 'bd', 'be' ],
  874. [ 'ca', 'cb', 'cc', 'cd', 'ce' ],
  875. [ 'da', 'db', 'dc', 'dd', 'de' ],
  876. [ 'ea', 'eb', 'ec', 'ed', 'ee' ]
  877. ] );
  878. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  879. [ 'aa', 'ab', 'ac', 'ad' ],
  880. [ 'ba', 'bb', 'bc', 'bd' ],
  881. [ 'ca', 'cb', 'cc', 'cd' ],
  882. [ 'da', 'db', 'dc', 'dd' ]
  883. ] ) );
  884. assertSelectedCells( model, [
  885. [ 1, 1, 1, 1 ],
  886. [ 1, 1, 1, 1 ],
  887. [ 1, 1, 1, 1 ],
  888. [ 1, 1, 1, 1 ]
  889. ] );
  890. } );
  891. } );
  892. describe( 'pasted table has spans', () => {
  893. it( 'handles pasting table that has cell with colspan', () => {
  894. tableSelection.setCellSelection(
  895. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  896. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  897. );
  898. pasteTable( [
  899. [ { colspan: 3, contents: 'aa' } ],
  900. [ 'ba', 'bb', 'bc' ]
  901. ] );
  902. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  903. [ '00', '01', '02', '03' ],
  904. [ '10', { colspan: 2, contents: 'aa' }, '13' ],
  905. [ '20', 'ba', 'bb', '23' ],
  906. [ '30', '31', '32', '33' ]
  907. ] ) );
  908. /* eslint-disable no-multi-spaces */
  909. assertSelectedCells( model, [
  910. [ 0, 0, 0, 0 ],
  911. [ 0, 1, 0 ],
  912. [ 0, 1, 1, 0 ],
  913. [ 0, 0, 0, 0 ]
  914. ] );
  915. /* eslint-enable no-multi-spaces */
  916. } );
  917. it( 'handles pasting table that has many cells with various colspan', () => {
  918. tableSelection.setCellSelection(
  919. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  920. modelRoot.getNodeByPath( [ 0, 3, 2 ] )
  921. );
  922. pasteTable( [
  923. [ 'aa', { colspan: 3, contents: 'ab' } ],
  924. [ { colspan: 4, contents: 'ba' } ],
  925. [ 'ca', 'cb', 'cc', 'cd' ],
  926. [ { colspan: 2, contents: 'da' }, 'dc', 'dd' ]
  927. ] );
  928. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  929. [ 'aa', { colspan: 2, contents: 'ab' }, '03' ],
  930. [ { colspan: 3, contents: 'ba' }, '13' ],
  931. [ 'ca', 'cb', 'cc', '23' ],
  932. [ { colspan: 2, contents: 'da' }, 'dc', '33' ]
  933. ] ) );
  934. /* eslint-disable no-multi-spaces */
  935. assertSelectedCells( model, [
  936. [ 1, 1, 0 ],
  937. [ 1, 0 ],
  938. [ 1, 1, 1, 0 ],
  939. [ 1, 1, 0 ]
  940. ] );
  941. /* eslint-enable no-multi-spaces */
  942. } );
  943. it( 'handles pasting table that has cell with rowspan', () => {
  944. tableSelection.setCellSelection(
  945. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  946. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  947. );
  948. pasteTable( [
  949. [ { rowspan: 3, contents: 'aa' }, 'ab' ],
  950. [ 'bb' ],
  951. [ 'cb' ]
  952. ] );
  953. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  954. [ '00', '01', '02', '03' ],
  955. [ '10', { rowspan: 2, contents: 'aa' }, 'ab', '13' ],
  956. [ '20', 'bb', '23' ],
  957. [ '30', '31', '32', '33' ]
  958. ] ) );
  959. /* eslint-disable no-multi-spaces */
  960. assertSelectedCells( model, [
  961. [ 0, 0, 0, 0 ],
  962. [ 0, 1, 1, 0 ],
  963. [ 0, 1, 0 ],
  964. [ 0, 0, 0, 0 ]
  965. ] );
  966. /* eslint-enable no-multi-spaces */
  967. } );
  968. it( 'handles pasting table that has many cells with various rowspan', () => {
  969. tableSelection.setCellSelection(
  970. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  971. modelRoot.getNodeByPath( [ 0, 2, 3 ] )
  972. );
  973. pasteTable( [
  974. [ 'aa', { rowspan: 3, contents: 'ab' }, { rowspan: 2, contents: 'ac' }, 'ad', 'ae' ],
  975. [ { rowspan: 3, contents: 'ba' }, 'bd', 'be' ],
  976. [ 'cc', 'cd', 'ce' ],
  977. [ 'da', 'db', 'dc', 'dd' ]
  978. ] );
  979. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  980. [ 'aa', { rowspan: 3, contents: 'ab' }, { rowspan: 2, contents: 'ac' }, 'ad' ],
  981. [ { rowspan: 2, contents: 'ba' }, 'bd' ],
  982. [ 'cc', 'cd' ],
  983. [ '30', '31', '32', '33' ]
  984. ] ) );
  985. /* eslint-disable no-multi-spaces */
  986. assertSelectedCells( model, [
  987. [ 1, 1, 1, 1 ],
  988. [ 1, 1 ],
  989. [ 1, 1 ],
  990. [ 0, 0, 0 ]
  991. ] );
  992. /* eslint-enable no-multi-spaces */
  993. } );
  994. it( 'handles pasting multi-spanned table', () => {
  995. setModelData( model, modelTable( [
  996. [ '00', '01', '02', '03', '04', '05' ],
  997. [ '10', '11', '12', '13', '14', '15' ],
  998. [ '20', '21', '22', '23', '24', '25' ],
  999. [ '30', '31', '32', '33', '34', '35' ],
  1000. [ '40', '41', '42', '43', '44', '45' ]
  1001. ] ) );
  1002. tableSelection.setCellSelection(
  1003. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  1004. modelRoot.getNodeByPath( [ 0, 3, 3 ] )
  1005. );
  1006. // +----+----+----+----+----+
  1007. // | aa | ac | ad | ae |
  1008. // +----+----+----+----+ +
  1009. // | ba | bb | |
  1010. // +----+ +----+
  1011. // | ca | | ce |
  1012. // + +----+----+----+----+
  1013. // | | db | dc | dd |
  1014. // +----+----+----+----+----+
  1015. pasteTable( [
  1016. [ { contents: 'aa', colspan: 2 }, 'ac', 'ad', { contents: 'ae', rowspan: 2 } ],
  1017. [ 'ba', { contents: 'bb', colspan: 3, rowspan: 2 } ],
  1018. [ { contents: 'ca', rowspan: 2 }, 'ce' ],
  1019. [ 'db', 'dc', { contents: 'dd', colspan: 2 } ]
  1020. ] );
  1021. // +----+----+----+----+----+----+
  1022. // | 00 | 01 | 02 | 03 | 04 | 05 |
  1023. // +----+----+----+----+----+----+
  1024. // | 10 | aa | ac | 14 | 15 |
  1025. // +----+----+----+----+----+----+
  1026. // | 20 | ba | bb | 24 | 25 |
  1027. // +----+----+ +----+----+
  1028. // | 30 | ca | | 34 | 35 |
  1029. // +----+----+----+----+----+----+
  1030. // | 40 | 41 | 42 | 43 | 44 | 45 |
  1031. // +----+----+----+----+----+----+
  1032. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1033. [ '00', '01', '02', '03', '04', '05' ],
  1034. [ '10', { contents: 'aa', colspan: 2 }, 'ac', '14', '15' ],
  1035. [ '20', 'ba', { contents: 'bb', colspan: 2, rowspan: 2 }, '24', '25' ],
  1036. [ '30', 'ca', '34', '35' ],
  1037. [ '40', '41', '42', '43', '44', '45' ]
  1038. ] ) );
  1039. /* eslint-disable no-multi-spaces */
  1040. assertSelectedCells( model, [
  1041. [ 0, 0, 0, 0, 0, 0 ],
  1042. [ 0, 1, 1, 0 ],
  1043. [ 0, 1, 1, 0 ],
  1044. [ 0, 1, 0 ],
  1045. [ 0, 0, 0, 0, 0, 0 ]
  1046. ] );
  1047. /* eslint-enable no-multi-spaces */
  1048. } );
  1049. } );
  1050. } );
  1051. describe( 'pasted table is smaller than the selected area', () => {
  1052. it( 'blocks this case', () => {
  1053. tableSelection.setCellSelection(
  1054. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1055. modelRoot.getNodeByPath( [ 0, 3, 3 ] )
  1056. );
  1057. // Catches the temporary console log in the CK_DEBUG mode.
  1058. sinon.stub( console, 'log' );
  1059. pasteTable( [
  1060. [ 'aa', 'ab' ],
  1061. [ 'ba', 'bb' ]
  1062. ] );
  1063. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1064. [ '00', '01', '02', '03' ],
  1065. [ '10', '11', '12', '13' ],
  1066. [ '20', '21', '22', '23' ],
  1067. [ '30', '31', '32', '33' ]
  1068. ] ) );
  1069. assertSelectedCells( model, [
  1070. [ 1, 1, 1, 1 ],
  1071. [ 1, 1, 1, 1 ],
  1072. [ 1, 1, 1, 1 ],
  1073. [ 1, 1, 1, 1 ]
  1074. ] );
  1075. } );
  1076. } );
  1077. } );
  1078. describe( 'Clipboard integration - paste (content scenarios)', () => {
  1079. it( 'handles multiple paragraphs', async () => {
  1080. await createEditor();
  1081. setModelData( model, modelTable( [
  1082. [ '00', '01', '02' ],
  1083. [ '01', '11', '12' ],
  1084. [ '02', '21', '22' ]
  1085. ] ) );
  1086. tableSelection.setCellSelection(
  1087. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1088. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  1089. );
  1090. pasteTable( [
  1091. [ '<p>a</p><p>a</p><p>a</p>', 'ab' ],
  1092. [ 'ba', 'bb' ]
  1093. ] );
  1094. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1095. [ '<paragraph>a</paragraph><paragraph>a</paragraph><paragraph>a</paragraph>', 'ab', '02' ],
  1096. [ 'ba', 'bb', '12' ],
  1097. [ '02', '21', '22' ]
  1098. ] ) );
  1099. } );
  1100. it( 'handles image in table cell', async () => {
  1101. await createEditor( [ ImageEditing, ImageCaptionEditing ] );
  1102. setModelData( model, modelTable( [
  1103. [ '00', '01', '02' ],
  1104. [ '01', '11', '12' ],
  1105. [ '02', '21', '22' ]
  1106. ] ) );
  1107. tableSelection.setCellSelection(
  1108. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1109. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  1110. );
  1111. pasteTable( [
  1112. [ '<img src="/assets/sample.jpg">', 'ab' ],
  1113. [ 'ba', 'bb' ]
  1114. ] );
  1115. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1116. [ '<image src="/assets/sample.jpg"><caption></caption></image>', 'ab', '02' ],
  1117. [ 'ba', 'bb', '12' ],
  1118. [ '02', '21', '22' ]
  1119. ] ) );
  1120. } );
  1121. it( 'handles mixed nested content in table cell', async () => {
  1122. await createEditor( [ ImageEditing, ImageCaptionEditing, BlockQuoteEditing, HorizontalLineEditing, ListEditing ] );
  1123. setModelData( model, modelTable( [
  1124. [ '00', '01', '02' ],
  1125. [ '01', '11', '12' ],
  1126. [ '02', '21', '22' ]
  1127. ] ) );
  1128. tableSelection.setCellSelection(
  1129. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1130. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  1131. );
  1132. const img = '<img src="/assets/sample.jpg">';
  1133. const list = '<ul><li>foo</li><li>bar</li></ul>';
  1134. const blockquote = `<blockquote><p>baz</p>${ list }</blockquote>`;
  1135. pasteTable( [
  1136. [ `${ img }${ list }${ blockquote }`, 'ab' ],
  1137. [ 'ba', 'bb' ]
  1138. ] );
  1139. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1140. [
  1141. '<image src="/assets/sample.jpg"><caption></caption></image>' +
  1142. '<listItem listIndent="0" listType="bulleted">foo</listItem>' +
  1143. '<listItem listIndent="0" listType="bulleted">bar</listItem>' +
  1144. '<blockQuote>' +
  1145. '<paragraph>baz</paragraph>' +
  1146. '<listItem listIndent="0" listType="bulleted">foo</listItem>' +
  1147. '<listItem listIndent="0" listType="bulleted">bar</listItem>' +
  1148. '</blockQuote>',
  1149. 'ab',
  1150. '02' ],
  1151. [ 'ba', 'bb', '12' ],
  1152. [ '02', '21', '22' ]
  1153. ] ) );
  1154. } );
  1155. it( 'handles table cell properties', async () => {
  1156. await createEditor( [ TableCellPropertiesEditing ] );
  1157. setModelData( model, modelTable( [
  1158. [ '00', '01', '02' ],
  1159. [ '01', '11', '12' ],
  1160. [ '02', '21', '22' ]
  1161. ] ) );
  1162. tableSelection.setCellSelection(
  1163. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1164. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  1165. );
  1166. pasteTable( [
  1167. [ { contents: 'aa', style: 'border:1px solid #f00;background:#ba7;width:1337px' }, 'ab' ],
  1168. [ 'ba', 'bb' ]
  1169. ] );
  1170. const tableCell = model.document.getRoot().getNodeByPath( [ 0, 0, 0 ] );
  1171. expect( tableCell.getAttribute( 'borderColor' ) ).to.deep.equal( {
  1172. top: '#f00',
  1173. right: '#f00',
  1174. bottom: '#f00',
  1175. left: '#f00'
  1176. } );
  1177. expect( tableCell.getAttribute( 'borderStyle' ) ).to.deep.equal( {
  1178. top: 'solid',
  1179. right: 'solid',
  1180. bottom: 'solid',
  1181. left: 'solid'
  1182. } );
  1183. expect( tableCell.getAttribute( 'borderWidth' ) ).to.deep.equal( {
  1184. top: '1px',
  1185. right: '1px',
  1186. bottom: '1px',
  1187. left: '1px'
  1188. } );
  1189. expect( tableCell.getAttribute( 'backgroundColor' ) ).to.equal( '#ba7' );
  1190. expect( tableCell.getAttribute( 'width' ) ).to.equal( '1337px' );
  1191. } );
  1192. it( 'discards table properties', async () => {
  1193. await createEditor( [ TableCellPropertiesEditing ] );
  1194. setModelData( model, modelTable( [
  1195. [ '00', '01', '02' ],
  1196. [ '01', '11', '12' ],
  1197. [ '02', '21', '22' ]
  1198. ] ) );
  1199. tableSelection.setCellSelection(
  1200. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  1201. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  1202. );
  1203. const tableStyle = 'border:1px solid #f00;background:#ba7;width:1337px';
  1204. const pastedTable = `<table style="${ tableStyle }"><tr><td>aa</td><td>ab</td></tr><tr><td>ba</td><td>bb</td></tr></table>`;
  1205. const data = {
  1206. dataTransfer: createDataTransfer(),
  1207. preventDefault: sinon.spy(),
  1208. stopPropagation: sinon.spy()
  1209. };
  1210. data.dataTransfer.setData( 'text/html', pastedTable );
  1211. viewDocument.fire( 'paste', data );
  1212. assertEqualMarkup( getModelData( model, { withoutSelection: true } ), modelTable( [
  1213. [ 'aa', 'ab', '02' ],
  1214. [ 'ba', 'bb', '12' ],
  1215. [ '02', '21', '22' ]
  1216. ] ) );
  1217. } );
  1218. } );
  1219. async function createEditor( extraPlugins = [] ) {
  1220. editor = await ClassicTestEditor.create( element, {
  1221. plugins: [ TableEditing, TableClipboard, Paragraph, Clipboard, ...extraPlugins ]
  1222. } );
  1223. model = editor.model;
  1224. modelRoot = model.document.getRoot();
  1225. viewDocument = editor.editing.view.document;
  1226. tableSelection = editor.plugins.get( 'TableSelection' );
  1227. }
  1228. function pasteTable( tableData ) {
  1229. const data = {
  1230. dataTransfer: createDataTransfer(),
  1231. preventDefault: sinon.spy(),
  1232. stopPropagation: sinon.spy()
  1233. };
  1234. data.dataTransfer.setData( 'text/html', viewTable( tableData ) );
  1235. viewDocument.fire( 'paste', data );
  1236. return data;
  1237. }
  1238. function createDataTransfer() {
  1239. const store = new Map();
  1240. return {
  1241. setData( type, data ) {
  1242. store.set( type, data );
  1243. },
  1244. getData( type ) {
  1245. return store.get( type );
  1246. }
  1247. };
  1248. }
  1249. } );