8
0

tableutils.js 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  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. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  7. import { defaultConversion, defaultSchema, modelTable } from './_utils/utils';
  8. import TableEditing from '../src/tableediting';
  9. import TableUtils from '../src/tableutils';
  10. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  11. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  12. describe( 'TableUtils', () => {
  13. let editor, model, root, tableUtils;
  14. afterEach( () => {
  15. return editor.destroy();
  16. } );
  17. describe( '#pluginName', () => {
  18. beforeEach( () => {
  19. return ModelTestEditor.create( {
  20. plugins: [ TableUtils ]
  21. } ).then( newEditor => {
  22. editor = newEditor;
  23. model = editor.model;
  24. root = model.document.getRoot( 'main' );
  25. tableUtils = editor.plugins.get( TableUtils );
  26. defaultSchema( model.schema );
  27. defaultConversion( editor.conversion );
  28. } );
  29. } );
  30. it( 'should provide plugin name', () => {
  31. expect( TableUtils.pluginName ).to.equal( 'TableUtils' );
  32. } );
  33. } );
  34. describe( 'getCellLocation()', () => {
  35. beforeEach( () => {
  36. return ModelTestEditor.create( {
  37. plugins: [ TableUtils ]
  38. } ).then( newEditor => {
  39. editor = newEditor;
  40. model = editor.model;
  41. root = model.document.getRoot( 'main' );
  42. tableUtils = editor.plugins.get( TableUtils );
  43. defaultSchema( model.schema );
  44. defaultConversion( editor.conversion );
  45. } );
  46. } );
  47. it( 'should return proper table cell location', () => {
  48. setData( model, modelTable( [
  49. [ { rowspan: 2, colspan: 2, contents: '00[]' }, '02' ],
  50. [ '12' ]
  51. ] ) );
  52. expect( tableUtils.getCellLocation( root.getNodeByPath( [ 0, 0, 0 ] ) ) ).to.deep.equal( { row: 0, column: 0 } );
  53. expect( tableUtils.getCellLocation( root.getNodeByPath( [ 0, 0, 1 ] ) ) ).to.deep.equal( { row: 0, column: 2 } );
  54. expect( tableUtils.getCellLocation( root.getNodeByPath( [ 0, 1, 0 ] ) ) ).to.deep.equal( { row: 1, column: 2 } );
  55. } );
  56. } );
  57. describe( 'insertRows()', () => {
  58. beforeEach( () => {
  59. return ModelTestEditor.create( {
  60. plugins: [ TableUtils ]
  61. } ).then( newEditor => {
  62. editor = newEditor;
  63. model = editor.model;
  64. root = model.document.getRoot( 'main' );
  65. tableUtils = editor.plugins.get( TableUtils );
  66. defaultSchema( model.schema );
  67. defaultConversion( editor.conversion );
  68. } );
  69. } );
  70. it( 'should insert row in given table at given index', () => {
  71. setData( model, modelTable( [
  72. [ '11[]', '12' ],
  73. [ '21', '22' ]
  74. ] ) );
  75. tableUtils.insertRows( root.getNodeByPath( [ 0 ] ), { at: 1 } );
  76. assertEqualMarkup( getData( model ), modelTable( [
  77. [ '11[]', '12' ],
  78. [ '', '' ],
  79. [ '21', '22' ]
  80. ] ) );
  81. } );
  82. it( 'should insert row in given table at default index', () => {
  83. setData( model, modelTable( [
  84. [ '11[]', '12' ],
  85. [ '21', '22' ]
  86. ] ) );
  87. tableUtils.insertRows( root.getNodeByPath( [ 0 ] ) );
  88. assertEqualMarkup( getData( model ), modelTable( [
  89. [ '', '' ],
  90. [ '11[]', '12' ],
  91. [ '21', '22' ]
  92. ] ) );
  93. } );
  94. it( 'should update table heading rows attribute when inserting row in headings section', () => {
  95. setData( model, modelTable( [
  96. [ '11[]', '12' ],
  97. [ '21', '22' ],
  98. [ '31', '32' ]
  99. ], { headingRows: 2 } ) );
  100. tableUtils.insertRows( root.getNodeByPath( [ 0 ] ), { at: 1 } );
  101. assertEqualMarkup( getData( model ), modelTable( [
  102. [ '11[]', '12' ],
  103. [ '', '' ],
  104. [ '21', '22' ],
  105. [ '31', '32' ]
  106. ], { headingRows: 3 } ) );
  107. } );
  108. it( 'should not update table heading rows attribute when inserting row after headings section', () => {
  109. setData( model, modelTable( [
  110. [ '11[]', '12' ],
  111. [ '21', '22' ],
  112. [ '31', '32' ]
  113. ], { headingRows: 2 } ) );
  114. tableUtils.insertRows( root.getNodeByPath( [ 0 ] ), { at: 2 } );
  115. assertEqualMarkup( getData( model ), modelTable( [
  116. [ '11[]', '12' ],
  117. [ '21', '22' ],
  118. [ '', '' ],
  119. [ '31', '32' ]
  120. ], { headingRows: 2 } ) );
  121. } );
  122. it( 'should expand rowspan of a cell that overlaps inserted rows', () => {
  123. setData( model, modelTable( [
  124. [ { colspan: 2, contents: '11[]' }, '13', '14' ],
  125. [ { colspan: 2, rowspan: 4, contents: '21' }, '23', '24' ],
  126. [ '33', '34' ]
  127. ], { headingColumns: 3, headingRows: 1 } ) );
  128. tableUtils.insertRows( root.getNodeByPath( [ 0 ] ), { at: 2, rows: 3 } );
  129. assertEqualMarkup( getData( model ), modelTable( [
  130. [ { colspan: 2, contents: '11[]' }, '13', '14' ],
  131. [ { colspan: 2, rowspan: 7, contents: '21' }, '23', '24' ],
  132. [ '', '' ],
  133. [ '', '' ],
  134. [ '', '' ],
  135. [ '33', '34' ]
  136. ], { headingColumns: 3, headingRows: 1 } ) );
  137. } );
  138. it( 'should not expand rowspan of a cell that does not overlaps inserted rows', () => {
  139. setData( model, modelTable( [
  140. [ { rowspan: 2, contents: '11[]' }, '12', '13' ],
  141. [ '22', '23' ],
  142. [ '31', '32', '33' ]
  143. ], { headingColumns: 3, headingRows: 1 } ) );
  144. tableUtils.insertRows( root.getNodeByPath( [ 0 ] ), { at: 2, rows: 3 } );
  145. assertEqualMarkup( getData( model ), modelTable( [
  146. [ { rowspan: 2, contents: '11[]' }, '12', '13' ],
  147. [ '22', '23' ],
  148. [ '', '', '' ],
  149. [ '', '', '' ],
  150. [ '', '', '' ],
  151. [ '31', '32', '33' ]
  152. ], { headingColumns: 3, headingRows: 1 } ) );
  153. } );
  154. it( 'should properly calculate columns if next row has colspans', () => {
  155. setData( model, modelTable( [
  156. [ { rowspan: 2, contents: '11[]' }, '12', '13' ],
  157. [ '22', '23' ],
  158. [ { colspan: 3, contents: '31' } ]
  159. ], { headingColumns: 3, headingRows: 1 } ) );
  160. tableUtils.insertRows( root.getNodeByPath( [ 0 ] ), { at: 2, rows: 3 } );
  161. assertEqualMarkup( getData( model ), modelTable( [
  162. [ { rowspan: 2, contents: '11[]' }, '12', '13' ],
  163. [ '22', '23' ],
  164. [ '', '', '' ],
  165. [ '', '', '' ],
  166. [ '', '', '' ],
  167. [ { colspan: 3, contents: '31' } ]
  168. ], { headingColumns: 3, headingRows: 1 } ) );
  169. } );
  170. it( 'should insert rows at the end of a table', () => {
  171. setData( model, modelTable( [
  172. [ '11[]', '12' ],
  173. [ '21', '22' ]
  174. ] ) );
  175. tableUtils.insertRows( root.getNodeByPath( [ 0 ] ), { at: 2, rows: 3 } );
  176. assertEqualMarkup( getData( model ), modelTable( [
  177. [ '11[]', '12' ],
  178. [ '21', '22' ],
  179. [ '', '' ],
  180. [ '', '' ],
  181. [ '', '' ]
  182. ] ) );
  183. } );
  184. } );
  185. describe( 'insertColumns()', () => {
  186. beforeEach( () => {
  187. return ModelTestEditor.create( {
  188. plugins: [ TableUtils ]
  189. } ).then( newEditor => {
  190. editor = newEditor;
  191. model = editor.model;
  192. root = model.document.getRoot( 'main' );
  193. tableUtils = editor.plugins.get( TableUtils );
  194. defaultSchema( model.schema );
  195. defaultConversion( editor.conversion );
  196. } );
  197. } );
  198. it( 'should insert column in given table at given index', () => {
  199. setData( model, modelTable( [
  200. [ '11[]', '12' ],
  201. [ '21', '22' ]
  202. ] ) );
  203. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 1 } );
  204. assertEqualMarkup( getData( model ), modelTable( [
  205. [ '11[]', '', '12' ],
  206. [ '21', '', '22' ]
  207. ] ) );
  208. } );
  209. it( 'should insert column in given table with default values', () => {
  210. setData( model, modelTable( [
  211. [ '11[]', '12' ],
  212. [ '21', '22' ]
  213. ] ) );
  214. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ) );
  215. assertEqualMarkup( getData( model ), modelTable( [
  216. [ '', '11[]', '12' ],
  217. [ '', '21', '22' ]
  218. ] ) );
  219. } );
  220. it( 'should insert column in given table at default index', () => {
  221. setData( model, modelTable( [
  222. [ '11[]', '12' ],
  223. [ '21', '22' ]
  224. ] ) );
  225. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ) );
  226. assertEqualMarkup( getData( model ), modelTable( [
  227. [ '', '11[]', '12' ],
  228. [ '', '21', '22' ]
  229. ] ) );
  230. } );
  231. it( 'should insert columns at the end of a row', () => {
  232. setData( model, modelTable( [
  233. [ '00[]', '01' ],
  234. [ { colspan: 2, contents: '10' } ],
  235. [ '20', { rowspan: 2, contents: '21' } ],
  236. [ '30' ]
  237. ] ) );
  238. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 2, columns: 2 } );
  239. assertEqualMarkup( getData( model ), modelTable( [
  240. [ '00[]', '01', '', '' ],
  241. [ { colspan: 2, contents: '10' }, '', '' ],
  242. [ '20', { rowspan: 2, contents: '21' }, '', '' ],
  243. [ '30', '', '' ]
  244. ] ) );
  245. } );
  246. it( 'should insert columns at the beginning of a row', () => {
  247. setData( model, modelTable( [
  248. [ '00[]', '01' ],
  249. [ { colspan: 2, contents: '10' } ],
  250. [ '20', { rowspan: 2, contents: '21' } ],
  251. [ { rowspan: 2, contents: '30' } ],
  252. [ '41' ]
  253. ] ) );
  254. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 0, columns: 2 } );
  255. assertEqualMarkup( getData( model ), modelTable( [
  256. [ '', '', '00[]', '01' ],
  257. [ '', '', { colspan: 2, contents: '10' } ],
  258. [ '', '', '20', { rowspan: 2, contents: '21' } ],
  259. [ '', '', { rowspan: 2, contents: '30' } ],
  260. [ '', '', '41' ]
  261. ] ) );
  262. } );
  263. it( 'should properly insert column at beginning of row-col-spanned cell', () => {
  264. setData( model, modelTable( [
  265. [ '11', '12', '13' ],
  266. [ '21', { colspan: 2, rowspan: 2, contents: '22[]' } ],
  267. [ '31' ],
  268. [ '41', '42', '43' ]
  269. ] ) );
  270. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 1, columns: 1 } );
  271. assertEqualMarkup( getData( model ), modelTable( [
  272. [ '11', '', '12', '13' ],
  273. [ '21', '', { colspan: 2, rowspan: 2, contents: '22[]' } ],
  274. [ '31', '' ],
  275. [ '41', '', '42', '43' ]
  276. ] ) );
  277. } );
  278. it( 'should update table heading columns attribute when inserting column in headings section', () => {
  279. setData( model, modelTable( [
  280. [ '11[]', '12' ],
  281. [ '21', '22' ],
  282. [ '31', '32' ]
  283. ], { headingColumns: 2 } ) );
  284. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 1 } );
  285. assertEqualMarkup( getData( model ), modelTable( [
  286. [ '11[]', '', '12' ],
  287. [ '21', '', '22' ],
  288. [ '31', '', '32' ]
  289. ], { headingColumns: 3 } ) );
  290. } );
  291. it( 'should not update table heading columns attribute when inserting column after headings section', () => {
  292. setData( model, modelTable( [
  293. [ '11[]', '12', '13' ],
  294. [ '21', '22', '23' ],
  295. [ '31', '32', '33' ]
  296. ], { headingColumns: 2 } ) );
  297. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 2 } );
  298. assertEqualMarkup( getData( model ), modelTable( [
  299. [ '11[]', '12', '', '13' ],
  300. [ '21', '22', '', '23' ],
  301. [ '31', '32', '', '33' ]
  302. ], { headingColumns: 2 } ) );
  303. } );
  304. it( 'should expand spanned columns', () => {
  305. setData( model, modelTable( [
  306. [ '00[]', '01' ],
  307. [ { colspan: 2, contents: '10' } ],
  308. [ '20', '21' ]
  309. ], { headingColumns: 2 } ) );
  310. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 1 } );
  311. assertEqualMarkup( getData( model ), modelTable( [
  312. [ '00[]', '', '01' ],
  313. [ { colspan: 3, contents: '10' } ],
  314. [ '20', '', '21' ]
  315. ], { headingColumns: 3 } ) );
  316. } );
  317. it( 'should skip wide spanned columns', () => {
  318. setData( model, modelTable( [
  319. [ '11[]', '12', '13', '14', '15' ],
  320. [ '21', '22', { colspan: 2, contents: '23' }, '25' ],
  321. [ { colspan: 4, contents: '31' }, { colspan: 2, contents: '34' } ]
  322. ], { headingColumns: 4 } ) );
  323. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 2, columns: 2 } );
  324. assertEqualMarkup( getData( model ), modelTable( [
  325. [ '11[]', '12', '', '', '13', '14', '15' ],
  326. [ '21', '22', '', '', { colspan: 2, contents: '23' }, '25' ],
  327. [ { colspan: 6, contents: '31' }, { colspan: 2, contents: '34' } ]
  328. ], { headingColumns: 6 } ) );
  329. } );
  330. it( 'should skip row & column spanned cells', () => {
  331. setData( model, modelTable( [
  332. [ { colspan: 2, rowspan: 2, contents: '00[]' }, '02' ],
  333. [ '12' ],
  334. [ '20', '21', '22' ]
  335. ], { headingColumns: 2 } ) );
  336. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 1, columns: 2 } );
  337. assertEqualMarkup( getData( model ), modelTable( [
  338. [ { colspan: 4, rowspan: 2, contents: '00[]' }, '02' ],
  339. [ '12' ],
  340. [ '20', '', '', '21', '22' ]
  341. ], { headingColumns: 4 } ) );
  342. } );
  343. it( 'should properly insert column while table has row-spanned cells', () => {
  344. setData( model, modelTable( [
  345. [ { rowspan: 4, contents: '00[]' }, { rowspan: 2, contents: '01' }, '02' ],
  346. [ '12' ],
  347. [ { rowspan: 2, contents: '21' }, '22' ],
  348. [ '32' ]
  349. ], { headingColumns: 2 } ) );
  350. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 1, columns: 1 } );
  351. assertEqualMarkup( getData( model ), modelTable( [
  352. [ { rowspan: 4, contents: '00[]' }, '', { rowspan: 2, contents: '01' }, '02' ],
  353. [ '', '12' ],
  354. [ '', { rowspan: 2, contents: '21' }, '22' ],
  355. [ '', '32' ]
  356. ], { headingColumns: 3 } ) );
  357. } );
  358. } );
  359. describe( 'splitCellVertically()', () => {
  360. beforeEach( () => {
  361. return ModelTestEditor.create( {
  362. plugins: [ TableUtils ]
  363. } ).then( newEditor => {
  364. editor = newEditor;
  365. model = editor.model;
  366. root = model.document.getRoot( 'main' );
  367. tableUtils = editor.plugins.get( TableUtils );
  368. defaultSchema( model.schema );
  369. defaultConversion( editor.conversion );
  370. } );
  371. } );
  372. it( 'should split table cell to given table cells number', () => {
  373. setData( model, modelTable( [
  374. [ '00', '01', '02' ],
  375. [ '10', '[]11', '12' ],
  376. [ '20', { colspan: 2, contents: '21' } ],
  377. [ { colspan: 2, contents: '30' }, '32' ]
  378. ] ) );
  379. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 1, 1 ] ), 3 );
  380. assertEqualMarkup( getData( model ), modelTable( [
  381. [ '00', { colspan: 3, contents: '01' }, '02' ],
  382. [ '10', '[]11', '', '', '12' ],
  383. [ '20', { colspan: 4, contents: '21' } ],
  384. [ { colspan: 4, contents: '30' }, '32' ]
  385. ] ) );
  386. } );
  387. it( 'should split table cell for two table cells as a default', () => {
  388. setData( model, modelTable( [
  389. [ '00', '01', '02' ],
  390. [ '10', '[]11', '12' ],
  391. [ '20', { colspan: 2, contents: '21' } ],
  392. [ { colspan: 2, contents: '30' }, '32' ]
  393. ] ) );
  394. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 1, 1 ] ) );
  395. assertEqualMarkup( getData( model ), modelTable( [
  396. [ '00', { colspan: 2, contents: '01' }, '02' ],
  397. [ '10', '[]11', '', '12' ],
  398. [ '20', { colspan: 3, contents: '21' } ],
  399. [ { colspan: 3, contents: '30' }, '32' ]
  400. ] ) );
  401. } );
  402. it( 'should split table cell if split is equal to colspan', () => {
  403. setData( model, modelTable( [
  404. [ '00', '01', '02' ],
  405. [ '10', '11', '12' ],
  406. [ '20', { colspan: 2, contents: '21[]' } ],
  407. [ { colspan: 2, contents: '30' }, '32' ]
  408. ] ) );
  409. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 2, 1 ] ), 2 );
  410. assertEqualMarkup( getData( model ), modelTable( [
  411. [ '00', '01', '02' ],
  412. [ '10', '11', '12' ],
  413. [ '20', '21[]', '' ],
  414. [ { colspan: 2, contents: '30' }, '32' ]
  415. ] ) );
  416. } );
  417. it( 'should properly split table cell if split is uneven', () => {
  418. setData( model, modelTable( [
  419. [ '00', '01', '02' ],
  420. [ { colspan: 3, contents: '10[]' } ]
  421. ] ) );
  422. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 1, 0 ] ), 2 );
  423. assertEqualMarkup( getData( model ), modelTable( [
  424. [ '00', '01', '02' ],
  425. [ { colspan: 2, contents: '10[]' }, '' ]
  426. ] ) );
  427. } );
  428. it( 'should properly set colspan of inserted cells', () => {
  429. setData( model, modelTable( [
  430. [ '00', '01', '02', '03' ],
  431. [ { colspan: 4, contents: '10[]' } ]
  432. ] ) );
  433. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 1, 0 ] ), 2 );
  434. assertEqualMarkup( getData( model ), modelTable( [
  435. [ '00', '01', '02', '03' ],
  436. [ { colspan: 2, contents: '10[]' }, { colspan: 2, contents: '' } ]
  437. ] ) );
  438. } );
  439. it( 'should keep rowspan attribute for newly inserted cells', () => {
  440. setData( model, modelTable( [
  441. [ '00', '01', '02', '03', '04', '05' ],
  442. [ { colspan: 5, rowspan: 2, contents: '10[]' }, '15' ],
  443. [ '25' ]
  444. ] ) );
  445. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 1, 0 ] ), 2 );
  446. assertEqualMarkup( getData( model ), modelTable( [
  447. [ '00', '01', '02', '03', '04', '05' ],
  448. [ { colspan: 3, rowspan: 2, contents: '10[]' }, { colspan: 2, rowspan: 2, contents: '' }, '15' ],
  449. [ '25' ]
  450. ] ) );
  451. } );
  452. it( 'should keep rowspan attribute of for newly inserted cells if number of cells is bigger then curren colspan', () => {
  453. setData( model, modelTable( [
  454. [ '00', '01', '02' ],
  455. [ { colspan: 2, rowspan: 2, contents: '10[]' }, '12' ],
  456. [ '22' ]
  457. ] ) );
  458. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 1, 0 ] ), 3 );
  459. assertEqualMarkup( getData( model ), modelTable( [
  460. [ { colspan: 2, contents: '00' }, '01', '02' ],
  461. [ { rowspan: 2, contents: '10[]' }, { rowspan: 2, contents: '' }, { rowspan: 2, contents: '' }, '12' ],
  462. [ '22' ]
  463. ] ) );
  464. } );
  465. it( 'should properly break a cell if it has colspan and number of created cells is bigger then colspan', () => {
  466. setData( model, modelTable( [
  467. [ '00', '01', '02', '03' ],
  468. [ { colspan: 4, contents: '10[]' } ]
  469. ] ) );
  470. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 1, 0 ] ), 6 );
  471. assertEqualMarkup( getData( model ), modelTable( [
  472. [ { colspan: 3, contents: '00' }, '01', '02', '03' ],
  473. [ '10[]', '', '', '', '', '' ]
  474. ] ) );
  475. } );
  476. it( 'should update heading columns is split cell is in heading section', () => {
  477. setData( model, modelTable( [
  478. [ '00', '01' ],
  479. [ '10[]', '11' ]
  480. ], { headingColumns: 1 } ) );
  481. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 1, 0 ] ), 3 );
  482. assertEqualMarkup( getData( model ), modelTable( [
  483. [ { colspan: 3, contents: '00' }, '01' ],
  484. [ '10[]', '', '', '11' ]
  485. ], { headingColumns: 3 } ) );
  486. } );
  487. } );
  488. describe( 'splitCellHorizontally()', () => {
  489. beforeEach( () => {
  490. return ModelTestEditor.create( {
  491. plugins: [ TableUtils ]
  492. } ).then( newEditor => {
  493. editor = newEditor;
  494. model = editor.model;
  495. root = model.document.getRoot( 'main' );
  496. tableUtils = editor.plugins.get( TableUtils );
  497. defaultSchema( model.schema );
  498. defaultConversion( editor.conversion );
  499. } );
  500. } );
  501. it( 'should split table cell to default table cells number', () => {
  502. setData( model, modelTable( [
  503. [ '00', '01', '02' ],
  504. [ '10', '[]11', '12' ],
  505. [ '20', '21', '22' ]
  506. ] ) );
  507. tableUtils.splitCellHorizontally( root.getNodeByPath( [ 0, 1, 1 ] ) );
  508. assertEqualMarkup( getData( model ), modelTable( [
  509. [ '00', '01', '02' ],
  510. [ { rowspan: 2, contents: '10' }, '[]11', { rowspan: 2, contents: '12' } ],
  511. [ '' ],
  512. [ '20', '21', '22' ]
  513. ] ) );
  514. } );
  515. it( 'should split table cell to given table cells number', () => {
  516. setData( model, modelTable( [
  517. [ '00', '01', '02' ],
  518. [ '10', '[]11', '12' ],
  519. [ '20', '21', '22' ]
  520. ] ) );
  521. tableUtils.splitCellHorizontally( root.getNodeByPath( [ 0, 1, 1 ] ), 4 );
  522. assertEqualMarkup( getData( model ), modelTable( [
  523. [ '00', '01', '02' ],
  524. [ { rowspan: 4, contents: '10' }, '[]11', { rowspan: 4, contents: '12' } ],
  525. [ '' ],
  526. [ '' ],
  527. [ '' ],
  528. [ '20', '21', '22' ]
  529. ] ) );
  530. } );
  531. it( 'should properly update row-spanned cells overlapping selected cell', () => {
  532. setData( model, modelTable( [
  533. [ { rowspan: 2, contents: '00' }, '01', { rowspan: 3, contents: '02' } ],
  534. [ '[]11' ],
  535. [ '20', '21' ]
  536. ] ) );
  537. tableUtils.splitCellHorizontally( root.getNodeByPath( [ 0, 1, 0 ] ), 3 );
  538. assertEqualMarkup( getData( model ), modelTable( [
  539. [ { rowspan: 4, contents: '00' }, '01', { rowspan: 5, contents: '02' } ],
  540. [ '[]11' ],
  541. [ '' ],
  542. [ '' ],
  543. [ '20', '21' ]
  544. ] ) );
  545. } );
  546. it( 'should split row-spanned cell', () => {
  547. setData( model, modelTable( [
  548. [ '00', { rowspan: 2, contents: '01[]' } ],
  549. [ '10' ],
  550. [ '20', '21' ]
  551. ] ) );
  552. const tableCell = root.getNodeByPath( [ 0, 0, 1 ] );
  553. tableUtils.splitCellHorizontally( tableCell, 2 );
  554. assertEqualMarkup( getData( model ), modelTable( [
  555. [ '00', '01[]' ],
  556. [ '10', '' ],
  557. [ '20', '21' ]
  558. ] ) );
  559. } );
  560. it( 'should copy colspan while splitting row-spanned cell', () => {
  561. setData( model, modelTable( [
  562. [ '00', { rowspan: 2, colspan: 2, contents: '01[]' } ],
  563. [ '10' ],
  564. [ '20', '21', '22' ]
  565. ] ) );
  566. const tableCell = root.getNodeByPath( [ 0, 0, 1 ] );
  567. tableUtils.splitCellHorizontally( tableCell, 2 );
  568. assertEqualMarkup( getData( model ), modelTable( [
  569. [ '00', { colspan: 2, contents: '01[]' } ],
  570. [ '10', { colspan: 2, contents: '' } ],
  571. [ '20', '21', '22' ]
  572. ] ) );
  573. } );
  574. it( 'should evenly distribute rowspan attribute', () => {
  575. setData( model, modelTable( [
  576. [ '00', { rowspan: 7, contents: '01[]' } ],
  577. [ '10' ],
  578. [ '20' ],
  579. [ '30' ],
  580. [ '40' ],
  581. [ '50' ],
  582. [ '60' ],
  583. [ '70', '71' ]
  584. ] ) );
  585. const tableCell = root.getNodeByPath( [ 0, 0, 1 ] );
  586. tableUtils.splitCellHorizontally( tableCell, 3 );
  587. assertEqualMarkup( getData( model ), modelTable( [
  588. [ '00', { rowspan: 3, contents: '01[]' } ],
  589. [ '10' ],
  590. [ '20' ],
  591. [ '30', { rowspan: 2, contents: '' } ],
  592. [ '40' ],
  593. [ '50', { rowspan: 2, contents: '' } ],
  594. [ '60' ],
  595. [ '70', '71' ]
  596. ] ) );
  597. } );
  598. it( 'should split row-spanned cell and updated other cells rowspan when splitting to bigger number of cells', () => {
  599. setData( model, modelTable( [
  600. [ '00', { rowspan: 2, contents: '01[]' } ],
  601. [ '10' ],
  602. [ '20', '21' ]
  603. ] ) );
  604. const tableCell = root.getNodeByPath( [ 0, 0, 1 ] );
  605. tableUtils.splitCellHorizontally( tableCell, 3 );
  606. assertEqualMarkup( getData( model ), modelTable( [
  607. [ { rowspan: 2, contents: '00' }, '01[]' ],
  608. [ '' ],
  609. [ '10', '' ],
  610. [ '20', '21' ]
  611. ] ) );
  612. } );
  613. it( 'should split row-spanned & col-spanned cell', () => {
  614. setData( model, modelTable( [
  615. [ '00', { colspan: 2, contents: '01[]' } ],
  616. [ '10', '11' ]
  617. ] ) );
  618. const tableCell = root.getNodeByPath( [ 0, 0, 1 ] );
  619. tableUtils.splitCellHorizontally( tableCell, 3 );
  620. assertEqualMarkup( getData( model ), modelTable( [
  621. [ { rowspan: 3, contents: '00' }, { colspan: 2, contents: '01[]' } ],
  622. [ { colspan: 2, contents: '' } ],
  623. [ { colspan: 2, contents: '' } ],
  624. [ '10', '11' ]
  625. ] ) );
  626. } );
  627. it( 'should split table cell from a heading section', () => {
  628. setData( model, modelTable( [
  629. [ '00[]', '01', '02' ],
  630. [ '10', '11', '12' ],
  631. [ '20', '21', '22' ]
  632. ], { headingRows: 1 } ) );
  633. tableUtils.splitCellHorizontally( root.getNodeByPath( [ 0, 0, 0 ] ), 3 );
  634. assertEqualMarkup( getData( model ), modelTable( [
  635. [ '00[]', { rowspan: 3, contents: '01' }, { rowspan: 3, contents: '02' } ],
  636. [ '' ],
  637. [ '' ],
  638. [ '10', '11', '12' ],
  639. [ '20', '21', '22' ]
  640. ], { headingRows: 3 } ) );
  641. } );
  642. } );
  643. describe( 'getColumns()', () => {
  644. beforeEach( () => {
  645. return ModelTestEditor.create( {
  646. plugins: [ TableUtils ]
  647. } ).then( newEditor => {
  648. editor = newEditor;
  649. model = editor.model;
  650. root = model.document.getRoot( 'main' );
  651. tableUtils = editor.plugins.get( TableUtils );
  652. defaultSchema( model.schema );
  653. defaultConversion( editor.conversion );
  654. } );
  655. } );
  656. it( 'should return proper number of columns', () => {
  657. setData( model, modelTable( [
  658. [ '00', { colspan: 3, contents: '01' }, '04' ]
  659. ] ) );
  660. expect( tableUtils.getColumns( root.getNodeByPath( [ 0 ] ) ) ).to.equal( 5 );
  661. } );
  662. } );
  663. describe( 'getRows()', () => {
  664. beforeEach( () => {
  665. return ModelTestEditor.create( {
  666. plugins: [ TableUtils ]
  667. } ).then( newEditor => {
  668. editor = newEditor;
  669. model = editor.model;
  670. root = model.document.getRoot( 'main' );
  671. tableUtils = editor.plugins.get( TableUtils );
  672. defaultSchema( model.schema );
  673. defaultConversion( editor.conversion );
  674. } );
  675. } );
  676. it( 'should return proper number of columns for simple table', () => {
  677. setData( model, modelTable( [
  678. [ '00', '01' ],
  679. [ '10', '11' ]
  680. ] ) );
  681. expect( tableUtils.getRows( root.getNodeByPath( [ 0 ] ) ) ).to.equal( 2 );
  682. } );
  683. it( 'should return proper number of columns for a table with header', () => {
  684. setData( model, modelTable( [
  685. [ '00', '01' ],
  686. [ '10', '11' ]
  687. ], { headingRows: 1 } ) );
  688. expect( tableUtils.getRows( root.getNodeByPath( [ 0 ] ) ) ).to.equal( 2 );
  689. } );
  690. it( 'should return proper number of columns for rowspan table', () => {
  691. setData( model, modelTable( [
  692. [ '00', '01' ],
  693. [ { rowspan: 2, contents: '10' }, '11' ],
  694. [ '21' ]
  695. ] ) );
  696. expect( tableUtils.getRows( root.getNodeByPath( [ 0 ] ) ) ).to.equal( 3 );
  697. } );
  698. } );
  699. describe( 'removeRows()', () => {
  700. beforeEach( () => {
  701. return ModelTestEditor.create( {
  702. plugins: [ Paragraph, TableEditing, TableUtils ]
  703. } ).then( newEditor => {
  704. editor = newEditor;
  705. model = editor.model;
  706. root = model.document.getRoot( 'main' );
  707. tableUtils = editor.plugins.get( TableUtils );
  708. } );
  709. } );
  710. describe( 'single row', () => {
  711. it( 'should remove a given row from a table start', () => {
  712. setData( model, modelTable( [
  713. [ '00', '01' ],
  714. [ '10', '11' ],
  715. [ '20', '21' ]
  716. ] ) );
  717. tableUtils.removeRows( root.getChild( 0 ), { at: 0 } );
  718. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  719. [ '10', '11' ],
  720. [ '20', '21' ]
  721. ] ) );
  722. } );
  723. it( 'should remove last row', () => {
  724. setData( model, modelTable( [
  725. [ '00', '01' ],
  726. [ '10', '11' ]
  727. ] ) );
  728. tableUtils.removeRows( root.getChild( 0 ), { at: 1 } );
  729. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  730. [ '00', '01' ]
  731. ] ) );
  732. } );
  733. it( 'should change heading rows if removing a heading row', () => {
  734. setData( model, modelTable( [
  735. [ '00', '01' ],
  736. [ '10', '11' ],
  737. [ '20', '21' ]
  738. ], { headingRows: 2 } ) );
  739. tableUtils.removeRows( root.getChild( 0 ), { at: 1 } );
  740. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  741. [ '00', '01' ],
  742. [ '20', '21' ]
  743. ], { headingRows: 1 } ) );
  744. } );
  745. it( 'should decrease rowspan of table cells from previous rows', () => {
  746. // +----+----+----+----+----+
  747. // | 00 | 01 | 02 | 03 | 04 |
  748. // +----+ + + + +
  749. // | 10 | | | | |
  750. // +----+----+ + + +
  751. // | 20 | 21 | | | |
  752. // +----+----+----+ + +
  753. // | 30 | 31 | 32 | | |
  754. // +----+----+----+----+ +
  755. // | 40 | 41 | 42 | 43 | |
  756. // +----+----+----+----+----+
  757. // | 50 | 51 | 52 | 53 | 54 |
  758. // +----+----+----+----+----+
  759. setData( model, modelTable( [
  760. [ '00', { contents: '01', rowspan: 2 }, { contents: '02', rowspan: 3 }, { contents: '03', rowspan: 4 },
  761. { contents: '04', rowspan: 5 } ],
  762. [ '10' ],
  763. [ '20', '21' ],
  764. [ '30', '31', '32' ],
  765. [ '40', '41', '42', '43' ],
  766. [ '50', '51', '52', '53', '54' ]
  767. ] ) );
  768. tableUtils.removeRows( root.getChild( 0 ), { at: 1, rows: 1 } );
  769. // +----+----+----+----+----+
  770. // | 00 | 01 | 02 | 03 | 04 |
  771. // +----+----+ + + +
  772. // | 20 | 21 | | | |
  773. // +----+----+----+ + +
  774. // | 30 | 31 | 32 | | |
  775. // +----+----+----+----+ +
  776. // | 40 | 41 | 42 | 43 | |
  777. // +----+----+----+----+----+
  778. // | 50 | 51 | 52 | 53 | 54 |
  779. // +----+----+----+----+----+
  780. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  781. [ '00', '01', { contents: '02', rowspan: 2 }, { contents: '03', rowspan: 3 }, { contents: '04', rowspan: 4 } ],
  782. [ '20', '21' ],
  783. [ '30', '31', '32' ],
  784. [ '40', '41', '42', '43' ],
  785. [ '50', '51', '52', '53', '54' ]
  786. ] ) );
  787. } );
  788. it( 'should decrease rowspan of table cells from previous rows (row-spanned cells on different rows)', () => {
  789. setData( model, modelTable( [
  790. [ { rowspan: 4, contents: '00' }, { rowspan: 3, contents: '01' }, { rowspan: 2, contents: '02' }, '03', '04' ],
  791. [ { rowspan: 2, contents: '13' }, '14' ],
  792. [ '22', '24' ],
  793. [ '31', '32', '33', '34' ]
  794. ] ) );
  795. tableUtils.removeRows( root.getChild( 0 ), { at: 2 } );
  796. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  797. [ { rowspan: 3, contents: '00' }, { rowspan: 2, contents: '01' }, { rowspan: 2, contents: '02' }, '03', '04' ],
  798. [ '13', '14' ],
  799. [ '31', '32', '33', '34' ]
  800. ] ) );
  801. } );
  802. it( 'should move row-spanned cells to a row below removing it\'s row', () => {
  803. setData( model, modelTable( [
  804. [ { rowspan: 3, contents: '00' }, { rowspan: 2, contents: '01' }, '02' ],
  805. [ '12' ],
  806. [ '21', '22' ],
  807. [ '30', '31', '32' ]
  808. ] ) );
  809. tableUtils.removeRows( root.getChild( 0 ), { at: 0 } );
  810. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  811. [ { rowspan: 2, contents: '00' }, '01', '12' ],
  812. [ '21', '22' ],
  813. [ '30', '31', '32' ]
  814. ] ) );
  815. } );
  816. it( 'should move row-spanned cells to a row below removing it\'s row (other cell is overlapping removed row)', () => {
  817. setData( model, modelTable( [
  818. [ '00', { rowspan: 3, contents: '01' }, '02', '03', '04' ],
  819. [ '10', { rowspan: 2, contents: '12' }, '13', '14' ],
  820. [ '20', '23', '24' ]
  821. ] ) );
  822. tableUtils.removeRows( root.getChild( 0 ), { at: 1 } );
  823. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  824. [ '00', { rowspan: 2, contents: '01' }, '02', '03', '04' ],
  825. [ '20', '12', '23', '24' ]
  826. ] ) );
  827. } );
  828. } );
  829. describe( 'many rows', () => {
  830. it( 'should properly remove middle rows', () => {
  831. setData( model, modelTable( [
  832. [ '00', '01' ],
  833. [ '10', '11' ],
  834. [ '20', '21' ],
  835. [ '30', '31' ]
  836. ] ) );
  837. tableUtils.removeRows( root.getChild( 0 ), { at: 1, rows: 2 } );
  838. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  839. [ '00', '01' ],
  840. [ '30', '31' ]
  841. ] ) );
  842. } );
  843. it( 'should properly remove tailing rows', () => {
  844. setData( model, modelTable( [
  845. [ '00', '01' ],
  846. [ '10', '11' ],
  847. [ '20', '21' ],
  848. [ '30', '31' ]
  849. ] ) );
  850. tableUtils.removeRows( root.getChild( 0 ), { at: 2, rows: 2 } );
  851. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  852. [ '00', '01' ],
  853. [ '10', '11' ]
  854. ] ) );
  855. } );
  856. it( 'should properly remove beginning rows', () => {
  857. setData( model, modelTable( [
  858. [ '00', '01' ],
  859. [ '10', '11' ],
  860. [ '20', '21' ],
  861. [ '30', '31' ]
  862. ] ) );
  863. tableUtils.removeRows( root.getChild( 0 ), { at: 0, rows: 2 } );
  864. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  865. [ '20', '21' ],
  866. [ '30', '31' ]
  867. ] ) );
  868. } );
  869. it( 'should support removing multiple headings (removed rows in heading section)', () => {
  870. setData( model, modelTable( [
  871. [ '00', '01' ],
  872. [ '10', '11' ],
  873. [ '20', '21' ],
  874. [ '30', '31' ]
  875. ], { headingRows: 3 } ) );
  876. tableUtils.removeRows( root.getChild( 0 ), { at: 0, rows: 2 } );
  877. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  878. [ '20', '21' ],
  879. [ '30', '31' ]
  880. ], { headingRows: 1 } ) );
  881. } );
  882. it( 'should support removing multiple headings (removed rows in heading and body section)', () => {
  883. setData( model, modelTable( [
  884. [ '00', '01' ],
  885. [ '10', '11' ],
  886. [ '20', '21' ],
  887. [ '30', '31' ],
  888. [ '40', '41' ]
  889. ], { headingRows: 3 } ) );
  890. tableUtils.removeRows( root.getChild( 0 ), { at: 1, rows: 3 } );
  891. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  892. [ '00', '01' ],
  893. [ '40', '41' ]
  894. ], { headingRows: 1 } ) );
  895. } );
  896. it( 'should support removing mixed heading and cell rows', () => {
  897. setData( model, modelTable( [
  898. [ '00', '01' ],
  899. [ '10', '11' ],
  900. [ '20', '21' ]
  901. ], { headingRows: 1 } ) );
  902. tableUtils.removeRows( root.getChild( 0 ), { at: 0, rows: 2 } );
  903. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  904. [ '20', '21' ]
  905. ] ) );
  906. } );
  907. it( 'should move row-spanned cells to a row after removed rows section', () => {
  908. setData( model, modelTable( [
  909. [ '00', '01', '02', '03' ],
  910. [ { rowspan: 4, contents: '10' }, { rowspan: 3, contents: '11' }, { rowspan: 2, contents: '12' }, '13' ],
  911. [ { rowspan: 3, contents: '23' } ],
  912. [ '32' ],
  913. [ '41', '42' ]
  914. ] ) );
  915. tableUtils.removeRows( root.getChild( 0 ), { at: 1, rows: 2 } );
  916. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  917. [ '00', '01', '02', '03' ],
  918. [ { rowspan: 2, contents: '10' }, '11', '32', { rowspan: 2, contents: '23' } ],
  919. [ '41', '42' ]
  920. ] ) );
  921. } );
  922. it( 'should decrease rowspan of table cells from rows before removed rows section', () => {
  923. setData( model, modelTable( [
  924. [ { rowspan: 4, contents: '00' }, { rowspan: 3, contents: '01' }, { rowspan: 2, contents: '02' }, '03' ],
  925. [ '13' ],
  926. [ '22', '23' ],
  927. [ '31', '32', '33' ]
  928. ] ) );
  929. tableUtils.removeRows( root.getChild( 0 ), { at: 1, rows: 2 } );
  930. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  931. [ { rowspan: 2, contents: '00' }, '01', '02', '03' ],
  932. [ '31', '32', '33' ]
  933. ] ) );
  934. } );
  935. it( 'should decrease rowspan of table cells from previous rows', () => {
  936. // +----+----+----+----+----+
  937. // | 00 | 01 | 02 | 03 | 04 |
  938. // +----+ + + + +
  939. // | 10 | | | | |
  940. // +----+----+ + + +
  941. // | 20 | 21 | | | |
  942. // +----+----+----+ + +
  943. // | 30 | 31 | 32 | | |
  944. // +----+----+----+----+ +
  945. // | 40 | 41 | 42 | 43 | |
  946. // +----+----+----+----+----+
  947. // | 50 | 51 | 52 | 53 | 54 |
  948. // +----+----+----+----+----+
  949. setData( model, modelTable( [
  950. [ '00', { contents: '01', rowspan: 2 }, { contents: '02', rowspan: 3 }, { contents: '03', rowspan: 4 },
  951. { contents: '04', rowspan: 5 } ],
  952. [ '10' ],
  953. [ '20', '21' ],
  954. [ '30', '31', '32' ],
  955. [ '40', '41', '42', '43' ],
  956. [ '50', '51', '52', '53', '54' ]
  957. ] ) );
  958. tableUtils.removeRows( root.getChild( 0 ), { at: 2, rows: 2 } );
  959. // +----+----+----+----+----+
  960. // | 00 | 01 | 02 | 03 | 04 |
  961. // +----+ + + + +
  962. // | 10 | | | | |
  963. // +----+----+----+----+ +
  964. // | 40 | 41 | 42 | 43 | |
  965. // +----+----+----+----+----+
  966. // | 50 | 51 | 52 | 53 | 54 |
  967. // +----+----+----+----+----+
  968. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  969. [ '00', { contents: '01', rowspan: 2 }, { contents: '02', rowspan: 2 }, { contents: '03', rowspan: 2 },
  970. { contents: '04', rowspan: 3 } ],
  971. [ '10' ],
  972. [ '40', '41', '42', '43' ],
  973. [ '50', '51', '52', '53', '54' ]
  974. ] ) );
  975. } );
  976. it( 'should re-use batch to create one undo step', () => {
  977. setData( model, modelTable( [
  978. [ '00', '01' ],
  979. [ '10', '11' ],
  980. [ '20', '21' ],
  981. [ '30', '31' ]
  982. ], { headingRows: 3 } ) );
  983. const createdBatches = new Set();
  984. model.on( 'applyOperation', ( evt, args ) => {
  985. const operation = args[ 0 ];
  986. createdBatches.add( operation.batch );
  987. } );
  988. const batch = model.createBatch();
  989. tableUtils.removeRows( root.getChild( 0 ), { at: 0, rows: 2, batch } );
  990. expect( createdBatches.size ).to.equal( 1 );
  991. } );
  992. } );
  993. } );
  994. describe( 'removeColumns()', () => {
  995. beforeEach( () => {
  996. return ModelTestEditor.create( {
  997. plugins: [ TableUtils ]
  998. } ).then( newEditor => {
  999. editor = newEditor;
  1000. model = editor.model;
  1001. root = model.document.getRoot( 'main' );
  1002. tableUtils = editor.plugins.get( TableUtils );
  1003. defaultSchema( model.schema );
  1004. defaultConversion( editor.conversion );
  1005. } );
  1006. } );
  1007. describe( 'single row', () => {
  1008. it( 'should remove a given column', () => {
  1009. setData( model, modelTable( [
  1010. [ '00', '01', '02' ],
  1011. [ '10', '11', '12' ],
  1012. [ '20', '21', '22' ]
  1013. ] ) );
  1014. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 1 } );
  1015. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1016. [ '00', '02' ],
  1017. [ '10', '12' ],
  1018. [ '20', '22' ]
  1019. ] ) );
  1020. } );
  1021. it( 'should remove a given column from a table start', () => {
  1022. setData( model, modelTable( [
  1023. [ '00', '01' ],
  1024. [ '10', '11' ],
  1025. [ '20', '21' ]
  1026. ] ) );
  1027. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 0 } );
  1028. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1029. [ '01' ],
  1030. [ '11' ],
  1031. [ '21' ]
  1032. ] ) );
  1033. } );
  1034. it( 'should change heading columns if removing a heading column', () => {
  1035. setData( model, modelTable( [
  1036. [ '00', '01' ],
  1037. [ '10', '11' ],
  1038. [ '20', '21' ]
  1039. ], { headingColumns: 2 } ) );
  1040. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 0 } );
  1041. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1042. [ '01' ],
  1043. [ '11' ],
  1044. [ '21' ]
  1045. ], { headingColumns: 1 } ) );
  1046. } );
  1047. it( 'should decrease colspan of table cells from previous column', () => {
  1048. setData( model, modelTable( [
  1049. [ { colspan: 4, contents: '00' }, '04' ],
  1050. [ { colspan: 3, contents: '10' }, '13', '14' ],
  1051. [ { colspan: 2, contents: '20' }, '22', '23', '24' ],
  1052. [ '30', { colspan: 2, contents: '31' }, '33', '34' ],
  1053. [ '40', '41', '42', '43', '44' ]
  1054. ] ) );
  1055. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 2 } );
  1056. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1057. [ { colspan: 3, contents: '00' }, '04' ],
  1058. [ { colspan: 2, contents: '10' }, '13', '14' ],
  1059. [ { colspan: 2, contents: '20' }, '23', '24' ],
  1060. [ '30', '31', '33', '34' ],
  1061. [ '40', '41', '43', '44' ]
  1062. ] ) );
  1063. } );
  1064. it( 'should decrease colspan of cells that are on removed column', () => {
  1065. setData( model, modelTable( [
  1066. [ { colspan: 3, contents: '00' }, '03' ],
  1067. [ { colspan: 2, contents: '10' }, '12', '13' ],
  1068. [ '20', '21', '22', '23' ]
  1069. ] ) );
  1070. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 0 } );
  1071. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1072. [ { colspan: 2, contents: '00' }, '03' ],
  1073. [ '10', '12', '13' ],
  1074. [ '21', '22', '23' ]
  1075. ] ) );
  1076. } );
  1077. it( 'should remove column with rowspan (first column)', () => {
  1078. setData( model, modelTable( [
  1079. [ { rowspan: 2, contents: '00' }, '01' ],
  1080. [ '11' ]
  1081. ] ) );
  1082. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 0 } );
  1083. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1084. [ '01' ],
  1085. [ '11' ]
  1086. ] ) );
  1087. } );
  1088. it( 'should remove column with rowspan (last column)', () => {
  1089. setData( model, modelTable( [
  1090. [ '00', { rowspan: 2, contents: '01' } ],
  1091. [ '10' ]
  1092. ] ) );
  1093. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 1 } );
  1094. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1095. [ '00' ],
  1096. [ '10' ]
  1097. ] ) );
  1098. } );
  1099. it( 'should remove column if other column is row-spanned (last column)', () => {
  1100. setData( model, modelTable( [
  1101. [ '00', { rowspan: 2, contents: '01' } ],
  1102. [ '10' ]
  1103. ] ) );
  1104. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 0 } );
  1105. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1106. [ '01' ]
  1107. ] ) );
  1108. } );
  1109. it( 'should remove column if other column is row-spanned (first column)', () => {
  1110. setData( model, modelTable( [
  1111. [ { rowspan: 2, contents: '00' }, '01' ],
  1112. [ '11' ]
  1113. ] ) );
  1114. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 1 } );
  1115. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1116. [ '00' ]
  1117. ] ) );
  1118. } );
  1119. it( 'should remove column if removing row with one column - other columns are spanned', () => {
  1120. setData( model, modelTable( [
  1121. [ '00', { rowspan: 2, contents: '01' }, { rowspan: 2, contents: '02' } ],
  1122. [ '10' ],
  1123. [ '20', '21', '22' ]
  1124. ] ) );
  1125. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 0 } );
  1126. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1127. [ '01', '02' ],
  1128. [ '21', '22' ]
  1129. ] ) );
  1130. } );
  1131. } );
  1132. describe( 'multiple columns', () => {
  1133. it( 'should properly remove two first columns', () => {
  1134. setData( model, modelTable( [
  1135. [ '00', '01', '02' ],
  1136. [ '10', '11', '12' ],
  1137. [ '20', '21', '22' ],
  1138. [ '30', '31', '32' ]
  1139. ] ) );
  1140. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 0, columns: 2 } );
  1141. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1142. [ '02' ],
  1143. [ '12' ],
  1144. [ '22' ],
  1145. [ '32' ]
  1146. ] ) );
  1147. } );
  1148. it( 'should properly remove two middle columns', () => {
  1149. setData( model, modelTable( [
  1150. [ '00', '01', '02', '03' ],
  1151. [ '10', '11', '12', '13' ],
  1152. [ '20', '21', '22', '23' ],
  1153. [ '30', '31', '32', '33' ]
  1154. ] ) );
  1155. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 1, columns: 2 } );
  1156. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1157. [ '00', '03' ],
  1158. [ '10', '13' ],
  1159. [ '20', '23' ],
  1160. [ '30', '33' ]
  1161. ] ) );
  1162. } );
  1163. it( 'should properly remove two last columns', () => {
  1164. setData( model, modelTable( [
  1165. [ '00', '01', '02' ],
  1166. [ '10', '11', '12' ],
  1167. [ '20', '21', '22' ],
  1168. [ '30', '31', '32' ]
  1169. ] ) );
  1170. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 1, columns: 2 } );
  1171. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1172. [ '00' ],
  1173. [ '10' ],
  1174. [ '20' ],
  1175. [ '30' ]
  1176. ] ) );
  1177. } );
  1178. it( 'should properly remove multiple heading columns', () => {
  1179. setData( model, modelTable( [
  1180. [ '00', '01', '02', '03', '04' ],
  1181. [ '10', '11', '12', '13', '14' ]
  1182. ], { headingColumns: 3 } ) );
  1183. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 1, columns: 3 } );
  1184. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1185. [ '00', '04' ],
  1186. [ '10', '14' ]
  1187. ], { headingColumns: 1 } ) );
  1188. } );
  1189. it( 'should properly calculate truncated colspans', () => {
  1190. setData( model, modelTable( [
  1191. [ { contents: '00', colspan: 3 } ],
  1192. [ '10', '11', '12' ],
  1193. [ '20', '21', '22' ]
  1194. ] ) );
  1195. tableUtils.removeColumns( root.getNodeByPath( [ 0 ] ), { at: 0, columns: 2 } );
  1196. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  1197. [ '00' ],
  1198. [ '12' ],
  1199. [ '22' ]
  1200. ] ) );
  1201. } );
  1202. } );
  1203. } );
  1204. } );