tableutils.js 20 KB

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