tableutils.js 21 KB

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