8
0

tableutils.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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( 'insertRows()', () => {
  60. it( 'should insert row in given table at given index', () => {
  61. setData( model, modelTable( [
  62. [ '11[]', '12' ],
  63. [ '21', '22' ]
  64. ] ) );
  65. tableUtils.insertRows( root.getNodeByPath( [ 0 ] ), { at: 1 } );
  66. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  67. [ '11[]', '12' ],
  68. [ '', '' ],
  69. [ '21', '22' ]
  70. ] ) );
  71. } );
  72. it( 'should insert row in given table at default index', () => {
  73. setData( model, modelTable( [
  74. [ '11[]', '12' ],
  75. [ '21', '22' ]
  76. ] ) );
  77. tableUtils.insertRows( root.getNodeByPath( [ 0 ] ) );
  78. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  79. [ '', '' ],
  80. [ '11[]', '12' ],
  81. [ '21', '22' ]
  82. ] ) );
  83. } );
  84. it( 'should update table heading rows attribute when inserting row in headings section', () => {
  85. setData( model, modelTable( [
  86. [ '11[]', '12' ],
  87. [ '21', '22' ],
  88. [ '31', '32' ]
  89. ], { headingRows: 2 } ) );
  90. tableUtils.insertRows( root.getNodeByPath( [ 0 ] ), { at: 1 } );
  91. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  92. [ '11[]', '12' ],
  93. [ '', '' ],
  94. [ '21', '22' ],
  95. [ '31', '32' ]
  96. ], { headingRows: 3 } ) );
  97. } );
  98. it( 'should not update table heading rows attribute when inserting row after headings section', () => {
  99. setData( model, modelTable( [
  100. [ '11[]', '12' ],
  101. [ '21', '22' ],
  102. [ '31', '32' ]
  103. ], { headingRows: 2 } ) );
  104. tableUtils.insertRows( root.getNodeByPath( [ 0 ] ), { at: 2 } );
  105. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  106. [ '11[]', '12' ],
  107. [ '21', '22' ],
  108. [ '', '' ],
  109. [ '31', '32' ]
  110. ], { headingRows: 2 } ) );
  111. } );
  112. it( 'should expand rowspan of a cell that overlaps inserted rows', () => {
  113. setData( model, modelTable( [
  114. [ { colspan: 2, contents: '11[]' }, '13', '14' ],
  115. [ { colspan: 2, rowspan: 4, contents: '21' }, '23', '24' ],
  116. [ '33', '34' ]
  117. ], { headingColumns: 3, headingRows: 1 } ) );
  118. tableUtils.insertRows( root.getNodeByPath( [ 0 ] ), { at: 2, rows: 3 } );
  119. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  120. [ { colspan: 2, contents: '11[]' }, '13', '14' ],
  121. [ { colspan: 2, rowspan: 7, contents: '21' }, '23', '24' ],
  122. [ '', '' ],
  123. [ '', '' ],
  124. [ '', '' ],
  125. [ '33', '34' ]
  126. ], { headingColumns: 3, headingRows: 1 } ) );
  127. } );
  128. it( 'should not expand rowspan of a cell that does not overlaps inserted rows', () => {
  129. setData( model, modelTable( [
  130. [ { rowspan: 2, contents: '11[]' }, '12', '13' ],
  131. [ '22', '23' ],
  132. [ '31', '32', '33' ]
  133. ], { headingColumns: 3, headingRows: 1 } ) );
  134. tableUtils.insertRows( root.getNodeByPath( [ 0 ] ), { at: 2, rows: 3 } );
  135. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  136. [ { rowspan: 2, contents: '11[]' }, '12', '13' ],
  137. [ '22', '23' ],
  138. [ '', '', '' ],
  139. [ '', '', '' ],
  140. [ '', '', '' ],
  141. [ '31', '32', '33' ]
  142. ], { headingColumns: 3, headingRows: 1 } ) );
  143. } );
  144. it( 'should properly calculate columns if next row has colspans', () => {
  145. setData( model, modelTable( [
  146. [ { rowspan: 2, contents: '11[]' }, '12', '13' ],
  147. [ '22', '23' ],
  148. [ { colspan: 3, contents: '31' } ]
  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. [ { colspan: 3, contents: '31' } ]
  158. ], { headingColumns: 3, headingRows: 1 } ) );
  159. } );
  160. it( 'should insert rows at the end of a table', () => {
  161. setData( model, modelTable( [
  162. [ '11[]', '12' ],
  163. [ '21', '22' ]
  164. ] ) );
  165. tableUtils.insertRows( root.getNodeByPath( [ 0 ] ), { at: 2, rows: 3 } );
  166. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  167. [ '11[]', '12' ],
  168. [ '21', '22' ],
  169. [ '', '' ],
  170. [ '', '' ],
  171. [ '', '' ]
  172. ] ) );
  173. } );
  174. } );
  175. describe( 'insertColumns()', () => {
  176. it( 'should insert column in given table at given index', () => {
  177. setData( model, modelTable( [
  178. [ '11[]', '12' ],
  179. [ '21', '22' ]
  180. ] ) );
  181. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 1 } );
  182. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  183. [ '11[]', '', '12' ],
  184. [ '21', '', '22' ]
  185. ] ) );
  186. } );
  187. it( 'should insert column in given table with default values', () => {
  188. setData( model, modelTable( [
  189. [ '11[]', '12' ],
  190. [ '21', '22' ]
  191. ] ) );
  192. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ) );
  193. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  194. [ '', '11[]', '12' ],
  195. [ '', '21', '22' ]
  196. ] ) );
  197. } );
  198. it( 'should insert column in given table at default index', () => {
  199. setData( model, modelTable( [
  200. [ '11[]', '12' ],
  201. [ '21', '22' ]
  202. ] ) );
  203. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ) );
  204. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  205. [ '', '11[]', '12' ],
  206. [ '', '21', '22' ]
  207. ] ) );
  208. } );
  209. it( 'should insert columns at table end', () => {
  210. setData( model, modelTable( [
  211. [ '11[]', '12' ],
  212. [ '21', '22' ]
  213. ] ) );
  214. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 2, columns: 2 } );
  215. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  216. [ '11[]', '12', '', '' ],
  217. [ '21', '22', '', '' ]
  218. ] ) );
  219. } );
  220. it( 'should update table heading columns attribute when inserting column in headings section', () => {
  221. setData( model, modelTable( [
  222. [ '11[]', '12' ],
  223. [ '21', '22' ],
  224. [ '31', '32' ]
  225. ], { headingColumns: 2 } ) );
  226. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 1 } );
  227. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  228. [ '11[]', '', '12' ],
  229. [ '21', '', '22' ],
  230. [ '31', '', '32' ]
  231. ], { headingColumns: 3 } ) );
  232. } );
  233. it( 'should not update table heading columns attribute when inserting column after headings section', () => {
  234. setData( model, modelTable( [
  235. [ '11[]', '12', '13' ],
  236. [ '21', '22', '23' ],
  237. [ '31', '32', '33' ]
  238. ], { headingColumns: 2 } ) );
  239. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 2 } );
  240. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  241. [ '11[]', '12', '', '13' ],
  242. [ '21', '22', '', '23' ],
  243. [ '31', '32', '', '33' ]
  244. ], { headingColumns: 2 } ) );
  245. } );
  246. it( 'should skip spanned columns', () => {
  247. setData( model, modelTable( [
  248. [ '11[]', '12' ],
  249. [ { colspan: 2, contents: '21' } ],
  250. [ '31', '32' ]
  251. ], { headingColumns: 2 } ) );
  252. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 1 } );
  253. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  254. [ '11[]', '', '12' ],
  255. [ { colspan: 3, contents: '21' } ],
  256. [ '31', '', '32' ]
  257. ], { headingColumns: 3 } ) );
  258. } );
  259. it( 'should skip wide spanned columns', () => {
  260. setData( model, modelTable( [
  261. [ '11[]', '12', '13', '14', '15' ],
  262. [ '21', '22', { colspan: 2, contents: '23' }, '25' ],
  263. [ { colspan: 4, contents: '31' }, { colspan: 2, contents: '34' } ]
  264. ], { headingColumns: 4 } ) );
  265. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 2, columns: 2 } );
  266. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  267. [ '11[]', '12', '', '', '13', '14', '15' ],
  268. [ '21', '22', '', '', { colspan: 2, contents: '23' }, '25' ],
  269. [ { colspan: 6, contents: '31' }, { colspan: 2, contents: '34' } ]
  270. ], { headingColumns: 6 } ) );
  271. } );
  272. // TODO fix me
  273. it.skip( 'should skip row spanned cells', () => {
  274. setData( model, modelTable( [
  275. [ { colspan: 2, rowspan: 2, contents: '11[]' }, '13' ],
  276. [ '23' ]
  277. ], { headingColumns: 2 } ) );
  278. tableUtils.insertColumns( root.getNodeByPath( [ 0 ] ), { at: 1, columns: 2 } );
  279. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  280. [ { colspan: 4, rowspan: 2, contents: '11[]' }, '13' ],
  281. [ '23' ]
  282. ], { headingColumns: 4 } ) );
  283. } );
  284. } );
  285. describe( 'splitCellHorizontally()', () => {
  286. it( 'should split table cell to given table cells number', () => {
  287. setData( model, modelTable( [
  288. [ '00', '01', '02' ],
  289. [ '10', '[]11', '12' ],
  290. [ '20', { colspan: 2, contents: '21' } ],
  291. [ { colspan: 2, contents: '30' }, '32' ]
  292. ] ) );
  293. tableUtils.splitCellHorizontally( root.getNodeByPath( [ 0, 1, 1 ] ), 3 );
  294. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  295. [ '00', { colspan: 3, contents: '01' }, '02' ],
  296. [ '10', '[]11', '', '', '12' ],
  297. [ '20', { colspan: 4, contents: '21' } ],
  298. [ { colspan: 4, contents: '30' }, '32' ]
  299. ] ) );
  300. } );
  301. it( 'should split table cell for two table cells as a default', () => {
  302. setData( model, modelTable( [
  303. [ '00', '01', '02' ],
  304. [ '10', '[]11', '12' ],
  305. [ '20', { colspan: 2, contents: '21' } ],
  306. [ { colspan: 2, contents: '30' }, '32' ]
  307. ] ) );
  308. tableUtils.splitCellHorizontally( root.getNodeByPath( [ 0, 1, 1 ] ) );
  309. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  310. [ '00', { colspan: 2, contents: '01' }, '02' ],
  311. [ '10', '[]11', '', '12' ],
  312. [ '20', { colspan: 3, contents: '21' } ],
  313. [ { colspan: 3, contents: '30' }, '32' ]
  314. ] ) );
  315. } );
  316. it( 'should unsplit table cell if split is equal to colspan', () => {
  317. setData( model, modelTable( [
  318. [ '00', '01', '02' ],
  319. [ '10', '11', '12' ],
  320. [ '20', { colspan: 2, contents: '21[]' } ],
  321. [ { colspan: 2, contents: '30' }, '32' ]
  322. ] ) );
  323. tableUtils.splitCellHorizontally( root.getNodeByPath( [ 0, 2, 1 ] ), 2 );
  324. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  325. [ '00', '01', '02' ],
  326. [ '10', '11', '12' ],
  327. [ '20', '21[]', '' ],
  328. [ { colspan: 2, contents: '30' }, '32' ]
  329. ] ) );
  330. } );
  331. it( 'should properly unsplit table cell if split is uneven', () => {
  332. setData( model, modelTable( [
  333. [ '00', '01', '02' ],
  334. [ { colspan: 3, contents: '10[]' } ]
  335. ] ) );
  336. tableUtils.splitCellHorizontally( root.getNodeByPath( [ 0, 1, 0 ] ), 2 );
  337. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  338. [ '00', '01', '02' ],
  339. [ { colspan: 2, contents: '10[]' }, '' ]
  340. ] ) );
  341. } );
  342. it( 'should properly set colspan of inserted cells', () => {
  343. setData( model, modelTable( [
  344. [ '00', '01', '02', '03' ],
  345. [ { colspan: 4, contents: '10[]' } ]
  346. ] ) );
  347. tableUtils.splitCellHorizontally( root.getNodeByPath( [ 0, 1, 0 ] ), 2 );
  348. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  349. [ '00', '01', '02', '03' ],
  350. [ { colspan: 2, contents: '10[]' }, { colspan: 2, contents: '' } ]
  351. ] ) );
  352. } );
  353. it( 'should keep rowspan attribute for newly inserted cells', () => {
  354. setData( model, modelTable( [
  355. [ '00', '01', '02', '03', '04', '05' ],
  356. [ { colspan: 5, rowspan: 2, contents: '10[]' }, '15' ],
  357. [ '25' ]
  358. ] ) );
  359. tableUtils.splitCellHorizontally( root.getNodeByPath( [ 0, 1, 0 ] ), 2 );
  360. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  361. [ '00', '01', '02', '03', '04', '05' ],
  362. [ { colspan: 3, rowspan: 2, contents: '10[]' }, { colspan: 2, rowspan: 2, contents: '' }, '15' ],
  363. [ '25' ]
  364. ] ) );
  365. } );
  366. } );
  367. describe( 'splitCellVertically()', () => {
  368. it( 'should split table cell to default table cells number', () => {
  369. setData( model, modelTable( [
  370. [ '00', '01', '02' ],
  371. [ '10', '[]11', '12' ],
  372. [ '20', '21', '22' ]
  373. ] ) );
  374. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 1, 1 ] ) );
  375. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  376. [ '00', '01', '02' ],
  377. [ { rowspan: 2, contents: '10' }, '[]11', { rowspan: 2, contents: '12' } ],
  378. [ '' ],
  379. [ '20', '21', '22' ]
  380. ] ) );
  381. } );
  382. it( 'should split table cell to given table cells number', () => {
  383. setData( model, modelTable( [
  384. [ '00', '01', '02' ],
  385. [ '10', '[]11', '12' ],
  386. [ '20', '21', '22' ]
  387. ] ) );
  388. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 1, 1 ] ), 4 );
  389. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  390. [ '00', '01', '02' ],
  391. [ { rowspan: 4, contents: '10' }, '[]11', { rowspan: 4, contents: '12' } ],
  392. [ '' ],
  393. [ '' ],
  394. [ '' ],
  395. [ '20', '21', '22' ]
  396. ] ) );
  397. } );
  398. it( 'should properly update rowspanned cells overlapping selected cell', () => {
  399. setData( model, modelTable( [
  400. [ { rowspan: 2, contents: '00' }, '01', { rowspan: 3, contents: '02' } ],
  401. [ '[]11' ],
  402. [ '20', '21' ]
  403. ] ) );
  404. tableUtils.splitCellVertically( root.getNodeByPath( [ 0, 1, 0 ] ), 3 );
  405. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  406. [ { rowspan: 4, contents: '00' }, '01', { rowspan: 5, contents: '02' } ],
  407. [ '[]11' ],
  408. [ '' ],
  409. [ '' ],
  410. [ '20', '21' ]
  411. ] ) );
  412. } );
  413. } );
  414. } );