tableutils.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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. [ '00[]', '01' ],
  299. [ { colspan: 2, contents: '10' } ],
  300. [ '20', '21' ]
  301. ], { headingColumns: 2 } ) );
  302. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 1 } );
  303. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  304. [ '00[]', '', '01' ],
  305. [ { colspan: 3, contents: '10' } ],
  306. [ '20', '', '21' ]
  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. it( 'should keep rowspan attribute of for newly inserted cells if number of cells is bigger then curren colspan', () => {
  433. setData( model, modelTable( [
  434. [ '00', '01', '02' ],
  435. [ { colspan: 2, rowspan: 2, contents: '10[]' }, '12' ],
  436. [ '22' ]
  437. ] ) );
  438. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 1, 0 ] ), 3 );
  439. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  440. [ { colspan: 2, contents: '00' }, '01', '02' ],
  441. [ { rowspan: 2, contents: '10[]' }, { rowspan: 2, contents: '' }, { rowspan: 2, contents: '' }, '12' ],
  442. [ '22' ]
  443. ] ) );
  444. } );
  445. it( 'should properly break a cell if it has colspan and number of created cells is bigger then colspan', () => {
  446. setData( model, modelTable( [
  447. [ '00', '01', '02', '03' ],
  448. [ { colspan: 4, contents: '10[]' } ]
  449. ] ) );
  450. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 1, 0 ] ), 6 );
  451. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  452. [ { colspan: 3, contents: '00' }, '01', '02', '03' ],
  453. [ '10[]', '', '', '', '', '' ]
  454. ] ) );
  455. } );
  456. it( 'should update heading columns is split cell is in heading section', () => {
  457. setData( model, modelTable( [
  458. [ '00', '01' ],
  459. [ '10[]', '11' ]
  460. ], { headingColumns: 1 } ) );
  461. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 1, 0 ] ), 3 );
  462. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  463. [ { colspan: 3, contents: '00' }, '01' ],
  464. [ '10[]', '', '', '11' ]
  465. ], { headingColumns: 3 } ) );
  466. } );
  467. } );
  468. describe( 'splitCellHorizontally()', () => {
  469. it( 'should split table cell to default table cells number', () => {
  470. setData( model, modelTable( [
  471. [ '00', '01', '02' ],
  472. [ '10', '[]11', '12' ],
  473. [ '20', '21', '22' ]
  474. ] ) );
  475. tableUtils.splitCellHorizontally( root.getNodeByPath( [ 0, 1, 1 ] ) );
  476. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  477. [ '00', '01', '02' ],
  478. [ { rowspan: 2, contents: '10' }, '[]11', { rowspan: 2, contents: '12' } ],
  479. [ '' ],
  480. [ '20', '21', '22' ]
  481. ] ) );
  482. } );
  483. it( 'should split table cell to given table cells number', () => {
  484. setData( model, modelTable( [
  485. [ '00', '01', '02' ],
  486. [ '10', '[]11', '12' ],
  487. [ '20', '21', '22' ]
  488. ] ) );
  489. tableUtils.splitCellHorizontally( root.getNodeByPath( [ 0, 1, 1 ] ), 4 );
  490. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  491. [ '00', '01', '02' ],
  492. [ { rowspan: 4, contents: '10' }, '[]11', { rowspan: 4, contents: '12' } ],
  493. [ '' ],
  494. [ '' ],
  495. [ '' ],
  496. [ '20', '21', '22' ]
  497. ] ) );
  498. } );
  499. it( 'should properly update rowspanned cells overlapping selected cell', () => {
  500. setData( model, modelTable( [
  501. [ { rowspan: 2, contents: '00' }, '01', { rowspan: 3, contents: '02' } ],
  502. [ '[]11' ],
  503. [ '20', '21' ]
  504. ] ) );
  505. tableUtils.splitCellHorizontally( root.getNodeByPath( [ 0, 1, 0 ] ), 3 );
  506. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  507. [ { rowspan: 4, contents: '00' }, '01', { rowspan: 5, contents: '02' } ],
  508. [ '[]11' ],
  509. [ '' ],
  510. [ '' ],
  511. [ '20', '21' ]
  512. ] ) );
  513. } );
  514. it( 'should split rowspanned cell', () => {
  515. setData( model, modelTable( [
  516. [ '00', { rowspan: 2, contents: '01[]' } ],
  517. [ '10' ],
  518. [ '20', '21' ]
  519. ] ) );
  520. const tableCell = root.getNodeByPath( [ 0, 0, 1 ] );
  521. tableUtils.splitCellHorizontally( tableCell, 2 );
  522. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  523. [ '00', '01[]' ],
  524. [ '10', '' ],
  525. [ '20', '21' ]
  526. ] ) );
  527. } );
  528. it( 'should copy colspan while splitting rowspanned cell', () => {
  529. setData( model, modelTable( [
  530. [ '00', { rowspan: 2, colspan: 2, contents: '01[]' } ],
  531. [ '10' ],
  532. [ '20', '21', '22' ]
  533. ] ) );
  534. const tableCell = root.getNodeByPath( [ 0, 0, 1 ] );
  535. tableUtils.splitCellHorizontally( tableCell, 2 );
  536. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  537. [ '00', { colspan: 2, contents: '01[]' } ],
  538. [ '10', { colspan: 2, contents: '' } ],
  539. [ '20', '21', '22' ]
  540. ] ) );
  541. } );
  542. it( 'should evenly distribute rowspan attribute', () => {
  543. setData( model, modelTable( [
  544. [ '00', { rowspan: 7, contents: '01[]' } ],
  545. [ '10' ],
  546. [ '20' ],
  547. [ '30' ],
  548. [ '40' ],
  549. [ '50' ],
  550. [ '60' ],
  551. [ '70', '71' ]
  552. ] ) );
  553. const tableCell = root.getNodeByPath( [ 0, 0, 1 ] );
  554. tableUtils.splitCellHorizontally( tableCell, 3 );
  555. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  556. [ '00', { rowspan: 3, contents: '01[]' } ],
  557. [ '10' ],
  558. [ '20' ],
  559. [ '30', { rowspan: 2, contents: '' } ],
  560. [ '40' ],
  561. [ '50', { rowspan: 2, contents: '' } ],
  562. [ '60' ],
  563. [ '70', '71' ]
  564. ] ) );
  565. } );
  566. it( 'should split rowspanned cell and updated other cells rowspan when splitting to bigger number of cells', () => {
  567. setData( model, modelTable( [
  568. [ '00', { rowspan: 2, contents: '01[]' } ],
  569. [ '10' ],
  570. [ '20', '21' ]
  571. ] ) );
  572. const tableCell = root.getNodeByPath( [ 0, 0, 1 ] );
  573. tableUtils.splitCellHorizontally( tableCell, 3 );
  574. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  575. [ { rowspan: 2, contents: '00' }, '01[]' ],
  576. [ '' ],
  577. [ '10', '' ],
  578. [ '20', '21' ]
  579. ] ) );
  580. } );
  581. it( 'should split rowspanned & colspaned cell', () => {
  582. setData( model, modelTable( [
  583. [ '00', { colspan: 2, contents: '01[]' } ],
  584. [ '10', '11' ]
  585. ] ) );
  586. const tableCell = root.getNodeByPath( [ 0, 0, 1 ] );
  587. tableUtils.splitCellHorizontally( tableCell, 3 );
  588. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  589. [ { rowspan: 3, contents: '00' }, { colspan: 2, contents: '01[]' } ],
  590. [ { colspan: 2, contents: '' } ],
  591. [ { colspan: 2, contents: '' } ],
  592. [ '10', '11' ]
  593. ] ) );
  594. } );
  595. it( 'should split table cell from a heading section', () => {
  596. setData( model, modelTable( [
  597. [ '00[]', '01', '02' ],
  598. [ '10', '11', '12' ],
  599. [ '20', '21', '22' ]
  600. ], { headingRows: 1 } ) );
  601. tableUtils.splitCellHorizontally( root.getNodeByPath( [ 0, 0, 0 ] ), 3 );
  602. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  603. [ '00[]', { rowspan: 3, contents: '01' }, { rowspan: 3, contents: '02' } ],
  604. [ '' ],
  605. [ '' ],
  606. [ '10', '11', '12' ],
  607. [ '20', '21', '22' ]
  608. ], { headingRows: 3 } ) );
  609. } );
  610. } );
  611. describe( 'getColumns()', () => {
  612. it( 'should return proper number of columns', () => {
  613. setData( model, modelTable( [
  614. [ '00', { colspan: 3, contents: '01' }, '04' ]
  615. ] ) );
  616. expect( tableUtils.getColumns( root.getNodeByPath( [ 0 ] ) ) ).to.equal( 5 );
  617. } );
  618. } );
  619. } );