tableutils.js 23 KB

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