tableutils.js 46 KB

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