tableutils.js 21 KB

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