tableutils.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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, formatTable, formattedModelTable, modelTable } from './_utils/utils';
  8. import TableUtils from '../src/tableutils';
  9. describe( 'TableUtils', () => {
  10. let editor, model, root, tableUtils;
  11. beforeEach( () => {
  12. return ModelTestEditor.create( {
  13. plugins: [ TableUtils ]
  14. } ).then( newEditor => {
  15. editor = newEditor;
  16. model = editor.model;
  17. root = model.document.getRoot( 'main' );
  18. tableUtils = editor.plugins.get( TableUtils );
  19. defaultSchema( model.schema );
  20. defaultConversion( editor.conversion );
  21. } );
  22. } );
  23. afterEach( () => {
  24. return editor.destroy();
  25. } );
  26. describe( '#pluginName', () => {
  27. it( 'should provide plugin name', () => {
  28. expect( TableUtils.pluginName ).to.equal( 'TableUtils' );
  29. } );
  30. } );
  31. describe( 'getCellLocation()', () => {
  32. it( 'should return proper table cell location', () => {
  33. setData( model, modelTable( [
  34. [ { rowspan: 2, colspan: 2, contents: '00[]' }, '02' ],
  35. [ '12' ]
  36. ] ) );
  37. expect( tableUtils.getCellLocation( root.getNodeByPath( [ 0, 0, 0 ] ) ) ).to.deep.equal( { row: 0, column: 0 } );
  38. expect( tableUtils.getCellLocation( root.getNodeByPath( [ 0, 0, 1 ] ) ) ).to.deep.equal( { row: 0, column: 2 } );
  39. expect( tableUtils.getCellLocation( root.getNodeByPath( [ 0, 1, 0 ] ) ) ).to.deep.equal( { row: 1, column: 2 } );
  40. } );
  41. } );
  42. describe( 'insertRows()', () => {
  43. it( 'should insert row in given table at given index', () => {
  44. setData( model, modelTable( [
  45. [ '11[]', '12' ],
  46. [ '21', '22' ]
  47. ] ) );
  48. tableUtils.insertRows( root.getNodeByPath( [ 0 ] ), { at: 1 } );
  49. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  50. [ '11[]', '12' ],
  51. [ '', '' ],
  52. [ '21', '22' ]
  53. ] ) );
  54. } );
  55. it( 'should insert row in given table at default index', () => {
  56. setData( model, modelTable( [
  57. [ '11[]', '12' ],
  58. [ '21', '22' ]
  59. ] ) );
  60. tableUtils.insertRows( root.getNodeByPath( [ 0 ] ) );
  61. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  62. [ '', '' ],
  63. [ '11[]', '12' ],
  64. [ '21', '22' ]
  65. ] ) );
  66. } );
  67. it( 'should update table heading rows attribute when inserting row in headings section', () => {
  68. setData( model, modelTable( [
  69. [ '11[]', '12' ],
  70. [ '21', '22' ],
  71. [ '31', '32' ]
  72. ], { headingRows: 2 } ) );
  73. tableUtils.insertRows( root.getNodeByPath( [ 0 ] ), { at: 1 } );
  74. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  75. [ '11[]', '12' ],
  76. [ '', '' ],
  77. [ '21', '22' ],
  78. [ '31', '32' ]
  79. ], { headingRows: 3 } ) );
  80. } );
  81. it( 'should not update table heading rows attribute when inserting row after headings section', () => {
  82. setData( model, modelTable( [
  83. [ '11[]', '12' ],
  84. [ '21', '22' ],
  85. [ '31', '32' ]
  86. ], { headingRows: 2 } ) );
  87. tableUtils.insertRows( root.getNodeByPath( [ 0 ] ), { at: 2 } );
  88. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  89. [ '11[]', '12' ],
  90. [ '21', '22' ],
  91. [ '', '' ],
  92. [ '31', '32' ]
  93. ], { headingRows: 2 } ) );
  94. } );
  95. it( 'should expand rowspan of a cell that overlaps inserted rows', () => {
  96. setData( model, modelTable( [
  97. [ { colspan: 2, contents: '11[]' }, '13', '14' ],
  98. [ { colspan: 2, rowspan: 4, contents: '21' }, '23', '24' ],
  99. [ '33', '34' ]
  100. ], { headingColumns: 3, headingRows: 1 } ) );
  101. tableUtils.insertRows( root.getNodeByPath( [ 0 ] ), { at: 2, rows: 3 } );
  102. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  103. [ { colspan: 2, contents: '11[]' }, '13', '14' ],
  104. [ { colspan: 2, rowspan: 7, contents: '21' }, '23', '24' ],
  105. [ '', '' ],
  106. [ '', '' ],
  107. [ '', '' ],
  108. [ '33', '34' ]
  109. ], { headingColumns: 3, headingRows: 1 } ) );
  110. } );
  111. it( 'should not expand rowspan of a cell that does not overlaps inserted rows', () => {
  112. setData( model, modelTable( [
  113. [ { rowspan: 2, contents: '11[]' }, '12', '13' ],
  114. [ '22', '23' ],
  115. [ '31', '32', '33' ]
  116. ], { headingColumns: 3, headingRows: 1 } ) );
  117. tableUtils.insertRows( root.getNodeByPath( [ 0 ] ), { at: 2, rows: 3 } );
  118. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  119. [ { rowspan: 2, contents: '11[]' }, '12', '13' ],
  120. [ '22', '23' ],
  121. [ '', '', '' ],
  122. [ '', '', '' ],
  123. [ '', '', '' ],
  124. [ '31', '32', '33' ]
  125. ], { headingColumns: 3, headingRows: 1 } ) );
  126. } );
  127. it( 'should properly calculate columns if next row has colspans', () => {
  128. setData( model, modelTable( [
  129. [ { rowspan: 2, contents: '11[]' }, '12', '13' ],
  130. [ '22', '23' ],
  131. [ { colspan: 3, contents: '31' } ]
  132. ], { headingColumns: 3, headingRows: 1 } ) );
  133. tableUtils.insertRows( root.getNodeByPath( [ 0 ] ), { at: 2, rows: 3 } );
  134. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  135. [ { rowspan: 2, contents: '11[]' }, '12', '13' ],
  136. [ '22', '23' ],
  137. [ '', '', '' ],
  138. [ '', '', '' ],
  139. [ '', '', '' ],
  140. [ { colspan: 3, contents: '31' } ]
  141. ], { headingColumns: 3, headingRows: 1 } ) );
  142. } );
  143. it( 'should insert rows at the end of a table', () => {
  144. setData( model, modelTable( [
  145. [ '11[]', '12' ],
  146. [ '21', '22' ]
  147. ] ) );
  148. tableUtils.insertRows( root.getNodeByPath( [ 0 ] ), { at: 2, rows: 3 } );
  149. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  150. [ '11[]', '12' ],
  151. [ '21', '22' ],
  152. [ '', '' ],
  153. [ '', '' ],
  154. [ '', '' ]
  155. ] ) );
  156. } );
  157. } );
  158. describe( 'insertColumns()', () => {
  159. it( 'should insert column in given table at given index', () => {
  160. setData( model, modelTable( [
  161. [ '11[]', '12' ],
  162. [ '21', '22' ]
  163. ] ) );
  164. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 1 } );
  165. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  166. [ '11[]', '', '12' ],
  167. [ '21', '', '22' ]
  168. ] ) );
  169. } );
  170. it( 'should insert column in given table with default values', () => {
  171. setData( model, modelTable( [
  172. [ '11[]', '12' ],
  173. [ '21', '22' ]
  174. ] ) );
  175. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ) );
  176. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  177. [ '', '11[]', '12' ],
  178. [ '', '21', '22' ]
  179. ] ) );
  180. } );
  181. it( 'should insert column in given table at default index', () => {
  182. setData( model, modelTable( [
  183. [ '11[]', '12' ],
  184. [ '21', '22' ]
  185. ] ) );
  186. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ) );
  187. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  188. [ '', '11[]', '12' ],
  189. [ '', '21', '22' ]
  190. ] ) );
  191. } );
  192. it( 'should insert columns at the end of a row', () => {
  193. setData( model, modelTable( [
  194. [ '00[]', '01' ],
  195. [ { colspan: 2, contents: '10' } ],
  196. [ '20', { rowspan: 2, contents: '21' } ],
  197. [ '30' ]
  198. ] ) );
  199. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 2, columns: 2 } );
  200. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  201. [ '00[]', '01', '', '' ],
  202. [ { colspan: 2, contents: '10' }, '', '' ],
  203. [ '20', { rowspan: 2, contents: '21' }, '', '' ],
  204. [ '30', '', '' ]
  205. ] ) );
  206. } );
  207. it( 'should insert columns at the beginning of a row', () => {
  208. setData( model, modelTable( [
  209. [ '00[]', '01' ],
  210. [ { colspan: 2, contents: '10' } ],
  211. [ '20', { rowspan: 2, contents: '21' } ],
  212. [ { rowspan: 2, contents: '30' } ],
  213. [ '41' ]
  214. ] ) );
  215. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 0, columns: 2 } );
  216. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  217. [ '', '', '00[]', '01' ],
  218. [ '', '', { colspan: 2, contents: '10' } ],
  219. [ '', '', '20', { rowspan: 2, contents: '21' } ],
  220. [ '', '', { rowspan: 2, contents: '30' } ],
  221. [ '', '', '41' ]
  222. ] ) );
  223. } );
  224. it( 'should update table heading columns attribute when inserting column in headings section', () => {
  225. setData( model, modelTable( [
  226. [ '11[]', '12' ],
  227. [ '21', '22' ],
  228. [ '31', '32' ]
  229. ], { headingColumns: 2 } ) );
  230. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 1 } );
  231. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  232. [ '11[]', '', '12' ],
  233. [ '21', '', '22' ],
  234. [ '31', '', '32' ]
  235. ], { headingColumns: 3 } ) );
  236. } );
  237. it( 'should not update table heading columns attribute when inserting column after headings section', () => {
  238. setData( model, modelTable( [
  239. [ '11[]', '12', '13' ],
  240. [ '21', '22', '23' ],
  241. [ '31', '32', '33' ]
  242. ], { headingColumns: 2 } ) );
  243. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 2 } );
  244. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  245. [ '11[]', '12', '', '13' ],
  246. [ '21', '22', '', '23' ],
  247. [ '31', '32', '', '33' ]
  248. ], { headingColumns: 2 } ) );
  249. } );
  250. it( 'should expand spanned columns', () => {
  251. setData( model, modelTable( [
  252. [ '00[]', '01' ],
  253. [ { colspan: 2, contents: '10' } ],
  254. [ '20', '21' ]
  255. ], { headingColumns: 2 } ) );
  256. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 1 } );
  257. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  258. [ '00[]', '', '01' ],
  259. [ { colspan: 3, contents: '10' } ],
  260. [ '20', '', '21' ]
  261. ], { headingColumns: 3 } ) );
  262. } );
  263. it( 'should skip wide spanned columns', () => {
  264. setData( model, modelTable( [
  265. [ '11[]', '12', '13', '14', '15' ],
  266. [ '21', '22', { colspan: 2, contents: '23' }, '25' ],
  267. [ { colspan: 4, contents: '31' }, { colspan: 2, contents: '34' } ]
  268. ], { headingColumns: 4 } ) );
  269. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 2, columns: 2 } );
  270. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  271. [ '11[]', '12', '', '', '13', '14', '15' ],
  272. [ '21', '22', '', '', { colspan: 2, contents: '23' }, '25' ],
  273. [ { colspan: 6, contents: '31' }, { colspan: 2, contents: '34' } ]
  274. ], { headingColumns: 6 } ) );
  275. } );
  276. it( 'should skip row & column spanned cells', () => {
  277. setData( model, modelTable( [
  278. [ { colspan: 2, rowspan: 2, contents: '00[]' }, '02' ],
  279. [ '12' ],
  280. [ '20', '21', '22' ]
  281. ], { headingColumns: 2 } ) );
  282. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 1, columns: 2 } );
  283. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  284. [ { colspan: 4, rowspan: 2, contents: '00[]' }, '02' ],
  285. [ '12' ],
  286. [ '20', '', '', '21', '22' ]
  287. ], { headingColumns: 4 } ) );
  288. } );
  289. it( 'should properly insert column while table has rowspanned cells', () => {
  290. setData( model, modelTable( [
  291. [ { rowspan: 4, contents: '00[]' }, { rowspan: 2, contents: '01' }, '02' ],
  292. [ '12' ],
  293. [ { rowspan: 2, contents: '21' }, '22' ],
  294. [ '32' ]
  295. ], { headingColumns: 2 } ) );
  296. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 1, columns: 1 } );
  297. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  298. [ { rowspan: 4, contents: '00[]' }, '', { rowspan: 2, contents: '01' }, '02' ],
  299. [ '', '12' ],
  300. [ '', { rowspan: 2, contents: '21' }, '22' ],
  301. [ '', '32' ]
  302. ], { headingColumns: 3 } ) );
  303. } );
  304. } );
  305. describe( 'splitCellVertically()', () => {
  306. it( 'should split table cell to given table cells number', () => {
  307. setData( model, modelTable( [
  308. [ '00', '01', '02' ],
  309. [ '10', '[]11', '12' ],
  310. [ '20', { colspan: 2, contents: '21' } ],
  311. [ { colspan: 2, contents: '30' }, '32' ]
  312. ] ) );
  313. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 1, 1 ] ), 3 );
  314. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  315. [ '00', { colspan: 3, contents: '01' }, '02' ],
  316. [ '10', '[]11', '', '', '12' ],
  317. [ '20', { colspan: 4, contents: '21' } ],
  318. [ { colspan: 4, contents: '30' }, '32' ]
  319. ] ) );
  320. } );
  321. it( 'should split table cell for two table cells as a default', () => {
  322. setData( model, modelTable( [
  323. [ '00', '01', '02' ],
  324. [ '10', '[]11', '12' ],
  325. [ '20', { colspan: 2, contents: '21' } ],
  326. [ { colspan: 2, contents: '30' }, '32' ]
  327. ] ) );
  328. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 1, 1 ] ) );
  329. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  330. [ '00', { colspan: 2, contents: '01' }, '02' ],
  331. [ '10', '[]11', '', '12' ],
  332. [ '20', { colspan: 3, contents: '21' } ],
  333. [ { colspan: 3, contents: '30' }, '32' ]
  334. ] ) );
  335. } );
  336. it( 'should split table cell if split is equal to colspan', () => {
  337. setData( model, modelTable( [
  338. [ '00', '01', '02' ],
  339. [ '10', '11', '12' ],
  340. [ '20', { colspan: 2, contents: '21[]' } ],
  341. [ { colspan: 2, contents: '30' }, '32' ]
  342. ] ) );
  343. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 2, 1 ] ), 2 );
  344. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  345. [ '00', '01', '02' ],
  346. [ '10', '11', '12' ],
  347. [ '20', '21[]', '' ],
  348. [ { colspan: 2, contents: '30' }, '32' ]
  349. ] ) );
  350. } );
  351. it( 'should properly split table cell if split is uneven', () => {
  352. setData( model, modelTable( [
  353. [ '00', '01', '02' ],
  354. [ { colspan: 3, contents: '10[]' } ]
  355. ] ) );
  356. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 1, 0 ] ), 2 );
  357. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  358. [ '00', '01', '02' ],
  359. [ { colspan: 2, contents: '10[]' }, '' ]
  360. ] ) );
  361. } );
  362. it( 'should properly set colspan of inserted cells', () => {
  363. setData( model, modelTable( [
  364. [ '00', '01', '02', '03' ],
  365. [ { colspan: 4, contents: '10[]' } ]
  366. ] ) );
  367. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 1, 0 ] ), 2 );
  368. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  369. [ '00', '01', '02', '03' ],
  370. [ { colspan: 2, contents: '10[]' }, { colspan: 2, contents: '' } ]
  371. ] ) );
  372. } );
  373. it( 'should keep rowspan attribute for newly inserted cells', () => {
  374. setData( model, modelTable( [
  375. [ '00', '01', '02', '03', '04', '05' ],
  376. [ { colspan: 5, rowspan: 2, contents: '10[]' }, '15' ],
  377. [ '25' ]
  378. ] ) );
  379. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 1, 0 ] ), 2 );
  380. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  381. [ '00', '01', '02', '03', '04', '05' ],
  382. [ { colspan: 3, rowspan: 2, contents: '10[]' }, { colspan: 2, rowspan: 2, contents: '' }, '15' ],
  383. [ '25' ]
  384. ] ) );
  385. } );
  386. it( 'should keep rowspan attribute of for newly inserted cells if number of cells is bigger then curren colspan', () => {
  387. setData( model, modelTable( [
  388. [ '00', '01', '02' ],
  389. [ { colspan: 2, rowspan: 2, contents: '10[]' }, '12' ],
  390. [ '22' ]
  391. ] ) );
  392. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 1, 0 ] ), 3 );
  393. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  394. [ { colspan: 2, contents: '00' }, '01', '02' ],
  395. [ { rowspan: 2, contents: '10[]' }, { rowspan: 2, contents: '' }, { rowspan: 2, contents: '' }, '12' ],
  396. [ '22' ]
  397. ] ) );
  398. } );
  399. it( 'should properly break a cell if it has colspan and number of created cells is bigger then colspan', () => {
  400. setData( model, modelTable( [
  401. [ '00', '01', '02', '03' ],
  402. [ { colspan: 4, contents: '10[]' } ]
  403. ] ) );
  404. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 1, 0 ] ), 6 );
  405. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  406. [ { colspan: 3, contents: '00' }, '01', '02', '03' ],
  407. [ '10[]', '', '', '', '', '' ]
  408. ] ) );
  409. } );
  410. it( 'should update heading columns is split cell is in heading section', () => {
  411. setData( model, modelTable( [
  412. [ '00', '01' ],
  413. [ '10[]', '11' ]
  414. ], { headingColumns: 1 } ) );
  415. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 1, 0 ] ), 3 );
  416. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  417. [ { colspan: 3, contents: '00' }, '01' ],
  418. [ '10[]', '', '', '11' ]
  419. ], { headingColumns: 3 } ) );
  420. } );
  421. } );
  422. describe( 'splitCellHorizontally()', () => {
  423. it( 'should split table cell to default table cells number', () => {
  424. setData( model, modelTable( [
  425. [ '00', '01', '02' ],
  426. [ '10', '[]11', '12' ],
  427. [ '20', '21', '22' ]
  428. ] ) );
  429. tableUtils.splitCellHorizontally( root.getNodeByPath( [ 0, 1, 1 ] ) );
  430. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  431. [ '00', '01', '02' ],
  432. [ { rowspan: 2, contents: '10' }, '[]11', { rowspan: 2, contents: '12' } ],
  433. [ '' ],
  434. [ '20', '21', '22' ]
  435. ] ) );
  436. } );
  437. it( 'should split table cell to given table cells number', () => {
  438. setData( model, modelTable( [
  439. [ '00', '01', '02' ],
  440. [ '10', '[]11', '12' ],
  441. [ '20', '21', '22' ]
  442. ] ) );
  443. tableUtils.splitCellHorizontally( root.getNodeByPath( [ 0, 1, 1 ] ), 4 );
  444. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  445. [ '00', '01', '02' ],
  446. [ { rowspan: 4, contents: '10' }, '[]11', { rowspan: 4, contents: '12' } ],
  447. [ '' ],
  448. [ '' ],
  449. [ '' ],
  450. [ '20', '21', '22' ]
  451. ] ) );
  452. } );
  453. it( 'should properly update rowspanned cells overlapping selected cell', () => {
  454. setData( model, modelTable( [
  455. [ { rowspan: 2, contents: '00' }, '01', { rowspan: 3, contents: '02' } ],
  456. [ '[]11' ],
  457. [ '20', '21' ]
  458. ] ) );
  459. tableUtils.splitCellHorizontally( root.getNodeByPath( [ 0, 1, 0 ] ), 3 );
  460. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  461. [ { rowspan: 4, contents: '00' }, '01', { rowspan: 5, contents: '02' } ],
  462. [ '[]11' ],
  463. [ '' ],
  464. [ '' ],
  465. [ '20', '21' ]
  466. ] ) );
  467. } );
  468. it( 'should split rowspanned cell', () => {
  469. setData( model, modelTable( [
  470. [ '00', { rowspan: 2, contents: '01[]' } ],
  471. [ '10' ],
  472. [ '20', '21' ]
  473. ] ) );
  474. const tableCell = root.getNodeByPath( [ 0, 0, 1 ] );
  475. tableUtils.splitCellHorizontally( tableCell, 2 );
  476. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  477. [ '00', '01[]' ],
  478. [ '10', '' ],
  479. [ '20', '21' ]
  480. ] ) );
  481. } );
  482. it( 'should copy colspan while splitting rowspanned cell', () => {
  483. setData( model, modelTable( [
  484. [ '00', { rowspan: 2, colspan: 2, contents: '01[]' } ],
  485. [ '10' ],
  486. [ '20', '21', '22' ]
  487. ] ) );
  488. const tableCell = root.getNodeByPath( [ 0, 0, 1 ] );
  489. tableUtils.splitCellHorizontally( tableCell, 2 );
  490. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  491. [ '00', { colspan: 2, contents: '01[]' } ],
  492. [ '10', { colspan: 2, contents: '' } ],
  493. [ '20', '21', '22' ]
  494. ] ) );
  495. } );
  496. it( 'should evenly distribute rowspan attribute', () => {
  497. setData( model, modelTable( [
  498. [ '00', { rowspan: 7, contents: '01[]' } ],
  499. [ '10' ],
  500. [ '20' ],
  501. [ '30' ],
  502. [ '40' ],
  503. [ '50' ],
  504. [ '60' ],
  505. [ '70', '71' ]
  506. ] ) );
  507. const tableCell = root.getNodeByPath( [ 0, 0, 1 ] );
  508. tableUtils.splitCellHorizontally( tableCell, 3 );
  509. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  510. [ '00', { rowspan: 3, contents: '01[]' } ],
  511. [ '10' ],
  512. [ '20' ],
  513. [ '30', { rowspan: 2, contents: '' } ],
  514. [ '40' ],
  515. [ '50', { rowspan: 2, contents: '' } ],
  516. [ '60' ],
  517. [ '70', '71' ]
  518. ] ) );
  519. } );
  520. it( 'should split rowspanned cell and updated other cells rowspan when splitting to bigger number of cells', () => {
  521. setData( model, modelTable( [
  522. [ '00', { rowspan: 2, contents: '01[]' } ],
  523. [ '10' ],
  524. [ '20', '21' ]
  525. ] ) );
  526. const tableCell = root.getNodeByPath( [ 0, 0, 1 ] );
  527. tableUtils.splitCellHorizontally( tableCell, 3 );
  528. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  529. [ { rowspan: 2, contents: '00' }, '01[]' ],
  530. [ '' ],
  531. [ '10', '' ],
  532. [ '20', '21' ]
  533. ] ) );
  534. } );
  535. it( 'should split rowspanned & colspaned cell', () => {
  536. setData( model, modelTable( [
  537. [ '00', { colspan: 2, contents: '01[]' } ],
  538. [ '10', '11' ]
  539. ] ) );
  540. const tableCell = root.getNodeByPath( [ 0, 0, 1 ] );
  541. tableUtils.splitCellHorizontally( tableCell, 3 );
  542. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  543. [ { rowspan: 3, contents: '00' }, { colspan: 2, contents: '01[]' } ],
  544. [ { colspan: 2, contents: '' } ],
  545. [ { colspan: 2, contents: '' } ],
  546. [ '10', '11' ]
  547. ] ) );
  548. } );
  549. it( 'should split table cell from a heading section', () => {
  550. setData( model, modelTable( [
  551. [ '00[]', '01', '02' ],
  552. [ '10', '11', '12' ],
  553. [ '20', '21', '22' ]
  554. ], { headingRows: 1 } ) );
  555. tableUtils.splitCellHorizontally( root.getNodeByPath( [ 0, 0, 0 ] ), 3 );
  556. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  557. [ '00[]', { rowspan: 3, contents: '01' }, { rowspan: 3, contents: '02' } ],
  558. [ '' ],
  559. [ '' ],
  560. [ '10', '11', '12' ],
  561. [ '20', '21', '22' ]
  562. ], { headingRows: 3 } ) );
  563. } );
  564. } );
  565. describe( 'getColumns()', () => {
  566. it( 'should return proper number of columns', () => {
  567. setData( model, modelTable( [
  568. [ '00', { colspan: 3, contents: '01' }, '04' ]
  569. ] ) );
  570. expect( tableUtils.getColumns( root.getNodeByPath( [ 0 ] ) ) ).to.equal( 5 );
  571. } );
  572. } );
  573. } );