mergecellcommand.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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 { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  7. import { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
  8. import MergeCellCommand from '../../src/commands/mergecellcommand';
  9. import {
  10. downcastInsertCell,
  11. downcastInsertRow,
  12. downcastInsertTable,
  13. downcastRemoveRow,
  14. downcastTableHeadingColumnsChange,
  15. downcastTableHeadingRowsChange
  16. } from '../../src/converters/downcast';
  17. import upcastTable from '../../src/converters/upcasttable';
  18. import { formatTable, formattedModelTable, modelTable } from '../_utils/utils';
  19. describe( 'MergeCellCommand', () => {
  20. let editor, model, command, root;
  21. beforeEach( () => {
  22. return ModelTestEditor.create()
  23. .then( newEditor => {
  24. editor = newEditor;
  25. model = editor.model;
  26. root = model.document.getRoot( 'main' );
  27. const conversion = editor.conversion;
  28. const schema = model.schema;
  29. schema.register( 'table', {
  30. allowWhere: '$block',
  31. allowAttributes: [ 'headingRows' ],
  32. isObject: true
  33. } );
  34. schema.register( 'tableRow', { allowIn: 'table' } );
  35. schema.register( 'tableCell', {
  36. allowIn: 'tableRow',
  37. allowContentOf: '$block',
  38. allowAttributes: [ 'colspan', 'rowspan' ],
  39. isLimit: true
  40. } );
  41. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  42. // Table conversion.
  43. conversion.for( 'upcast' ).add( upcastTable() );
  44. conversion.for( 'downcast' ).add( downcastInsertTable() );
  45. // Insert row conversion.
  46. conversion.for( 'downcast' ).add( downcastInsertRow() );
  47. // Remove row conversion.
  48. conversion.for( 'downcast' ).add( downcastRemoveRow() );
  49. // Table cell conversion.
  50. conversion.for( 'downcast' ).add( downcastInsertCell() );
  51. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'td' } ) );
  52. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'th' } ) );
  53. // Table attributes conversion.
  54. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  55. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  56. conversion.for( 'downcast' ).add( downcastTableHeadingColumnsChange() );
  57. conversion.for( 'downcast' ).add( downcastTableHeadingRowsChange() );
  58. } );
  59. } );
  60. afterEach( () => {
  61. return editor.destroy();
  62. } );
  63. describe( 'direction=right', () => {
  64. beforeEach( () => {
  65. command = new MergeCellCommand( editor, { direction: 'right' } );
  66. } );
  67. describe( 'isEnabled', () => {
  68. it( 'should be true if in cell that has sibling on the right', () => {
  69. setData( model, modelTable( [
  70. [ '00[]', '01' ]
  71. ] ) );
  72. expect( command.isEnabled ).to.be.true;
  73. } );
  74. it( 'should be false if last cell of a row', () => {
  75. setData( model, modelTable( [
  76. [ '00', '01[]' ]
  77. ] ) );
  78. expect( command.isEnabled ).to.be.false;
  79. } );
  80. it( 'should be true if in a cell that has sibling on the right with the same rowspan', () => {
  81. setData( model, modelTable( [
  82. [ { rowspan: 2, contents: '00[]' }, { rowspan: 2, contents: '01' } ]
  83. ] ) );
  84. expect( command.isEnabled ).to.be.true;
  85. } );
  86. it( 'should be false if in a cell that has sibling but with different rowspan', () => {
  87. setData( model, modelTable( [
  88. [ { rowspan: 2, contents: '00[]' }, { rowspan: 3, contents: '01' } ]
  89. ] ) );
  90. expect( command.isEnabled ).to.be.false;
  91. } );
  92. it( 'should be false if not in a cell', () => {
  93. setData( model, '<p>11[]</p>' );
  94. expect( command.isEnabled ).to.be.false;
  95. } );
  96. } );
  97. describe( 'value', () => {
  98. it( 'should be set to mergeable sibling if in cell that has sibling on the right', () => {
  99. setData( model, modelTable( [
  100. [ '00[]', '01' ]
  101. ] ) );
  102. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 1 ] ) );
  103. } );
  104. it( 'should be undefined if last cell of a row', () => {
  105. setData( model, modelTable( [
  106. [ '00', '01[]' ]
  107. ] ) );
  108. expect( command.value ).to.be.undefined;
  109. } );
  110. it( 'should be set to mergeable sibling if in a cell that has sibling on the right with the same rowspan', () => {
  111. setData( model, modelTable( [
  112. [ { rowspan: 2, contents: '00[]' }, { rowspan: 2, contents: '01' } ]
  113. ] ) );
  114. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 1 ] ) );
  115. } );
  116. it( 'should be undefined if in a cell that has sibling but with different rowspan', () => {
  117. setData( model, modelTable( [
  118. [ { rowspan: 2, contents: '00[]' }, { rowspan: 3, contents: '01' } ]
  119. ] ) );
  120. expect( command.value ).to.be.undefined;
  121. } );
  122. it( 'should be undefined if not in a cell', () => {
  123. setData( model, '<p>11[]</p>' );
  124. expect( command.value ).to.be.undefined;
  125. } );
  126. } );
  127. describe( 'execute()', () => {
  128. it( 'should merge table cells ', () => {
  129. setData( model, modelTable( [
  130. [ '[]00', '01' ]
  131. ] ) );
  132. command.execute();
  133. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  134. [ { colspan: 2, contents: '[0001]' } ]
  135. ] ) );
  136. } );
  137. } );
  138. } );
  139. describe( 'direction=left', () => {
  140. beforeEach( () => {
  141. command = new MergeCellCommand( editor, { direction: 'left' } );
  142. } );
  143. describe( 'isEnabled', () => {
  144. it( 'should be true if in cell that has sibling on the left', () => {
  145. setData( model, modelTable( [
  146. [ '00', '01[]' ]
  147. ] ) );
  148. expect( command.isEnabled ).to.be.true;
  149. } );
  150. it( 'should be false if first cell of a row', () => {
  151. setData( model, modelTable( [
  152. [ '00[]', '01' ]
  153. ] ) );
  154. expect( command.isEnabled ).to.be.false;
  155. } );
  156. it( 'should be true if in a cell that has sibling on the left with the same rowspan', () => {
  157. setData( model, modelTable( [
  158. [ { rowspan: 2, contents: '00' }, { rowspan: 2, contents: '01[]' } ]
  159. ] ) );
  160. expect( command.isEnabled ).to.be.true;
  161. } );
  162. it( 'should be false if in a cell that has sibling but with different rowspan', () => {
  163. setData( model, modelTable( [
  164. [ { rowspan: 2, contents: '00' }, { rowspan: 3, contents: '01[]' } ]
  165. ] ) );
  166. expect( command.isEnabled ).to.be.false;
  167. } );
  168. it( 'should be false if not in a cell', () => {
  169. setData( model, '<p>11[]</p>' );
  170. expect( command.isEnabled ).to.be.false;
  171. } );
  172. } );
  173. describe( 'value', () => {
  174. it( 'should be set to mergeable sibling if in cell that has sibling on the left', () => {
  175. setData( model, modelTable( [
  176. [ '00', '01[]' ]
  177. ] ) );
  178. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 0 ] ) );
  179. } );
  180. it( 'should be undefined if first cell of a row', () => {
  181. setData( model, modelTable( [
  182. [ '00[]', '01' ]
  183. ] ) );
  184. expect( command.value ).to.be.undefined;
  185. } );
  186. it( 'should be set to mergeable sibling if in a cell that has sibling on the left with the same rowspan', () => {
  187. setData( model, modelTable( [
  188. [ { rowspan: 2, contents: '00' }, { rowspan: 2, contents: '01[]' } ]
  189. ] ) );
  190. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 0 ] ) );
  191. } );
  192. it( 'should be undefined if in a cell that has sibling but with different rowspan', () => {
  193. setData( model, modelTable( [
  194. [ { rowspan: 2, contents: '00' }, { rowspan: 3, contents: '01[]' } ]
  195. ] ) );
  196. expect( command.value ).to.be.undefined;
  197. } );
  198. it( 'should be undefined if not in a cell', () => {
  199. setData( model, '<p>11[]</p>' );
  200. expect( command.value ).to.be.undefined;
  201. } );
  202. } );
  203. describe( 'execute()', () => {
  204. it( 'should merge table cells ', () => {
  205. setData( model, modelTable( [
  206. [ '00', '[]01' ]
  207. ] ) );
  208. command.execute();
  209. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  210. [ { colspan: 2, contents: '[0001]' } ]
  211. ] ) );
  212. } );
  213. } );
  214. } );
  215. describe( 'direction=down', () => {
  216. beforeEach( () => {
  217. command = new MergeCellCommand( editor, { direction: 'down' } );
  218. } );
  219. describe( 'isEnabled', () => {
  220. it( 'should be true if in cell that has mergeable cell in next row', () => {
  221. setData( model, modelTable( [
  222. [ '00', '01[]' ],
  223. [ '10', '11' ]
  224. ] ) );
  225. expect( command.isEnabled ).to.be.true;
  226. } );
  227. it( 'should be false if in last row', () => {
  228. setData( model, modelTable( [
  229. [ '00', '01' ],
  230. [ '10[]', '11' ]
  231. ] ) );
  232. expect( command.isEnabled ).to.be.false;
  233. } );
  234. it( 'should be true if in a cell that has mergeable cell with the same colspan', () => {
  235. setData( model, modelTable( [
  236. [ { colspan: 2, contents: '00[]' }, '02' ],
  237. [ { colspan: 2, contents: '01' }, '12' ]
  238. ] ) );
  239. expect( command.isEnabled ).to.be.true;
  240. } );
  241. it( 'should be false if in a cell that potential mergeable cell has different colspan', () => {
  242. setData( model, modelTable( [
  243. [ { colspan: 2, contents: '00[]' }, '02' ],
  244. [ { colspan: 3, contents: '01' } ]
  245. ] ) );
  246. expect( command.isEnabled ).to.be.false;
  247. } );
  248. it( 'should be false if not in a cell', () => {
  249. setData( model, '<p>11[]</p>' );
  250. expect( command.isEnabled ).to.be.false;
  251. } );
  252. it( 'should be false if mergeable cell is in other table section then current cell', () => {
  253. setData( model, modelTable( [
  254. [ '00[]', '01' ],
  255. [ '10', '11' ]
  256. ], { headingRows: 1 } ) );
  257. expect( command.isEnabled ).to.be.false;
  258. } );
  259. } );
  260. describe( 'value', () => {
  261. it( 'should be set to mergeable cell', () => {
  262. setData( model, modelTable( [
  263. [ '00', '01[]' ],
  264. [ '10', '11' ]
  265. ] ) );
  266. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 1, 1 ] ) );
  267. } );
  268. it( 'should be undefined if in last row', () => {
  269. setData( model, modelTable( [
  270. [ '00', '01' ],
  271. [ '10[]', '11' ]
  272. ] ) );
  273. expect( command.value ).to.be.undefined;
  274. } );
  275. it( 'should be set to mergeable cell with the same rowspan', () => {
  276. setData( model, modelTable( [
  277. [ { colspan: 2, contents: '00[]' }, '02' ],
  278. [ { colspan: 2, contents: '01' }, '12' ]
  279. ] ) );
  280. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 1, 0 ] ) );
  281. } );
  282. it( 'should be undefined if in a cell that potential mergeable cell has different rowspan', () => {
  283. setData( model, modelTable( [
  284. [ { colspan: 2, contents: '00[]' }, '02' ],
  285. [ { colspan: 3, contents: '01' } ]
  286. ] ) );
  287. expect( command.value ).to.be.undefined;
  288. } );
  289. it( 'should be undefined if not in a cell', () => {
  290. setData( model, '<p>11[]</p>' );
  291. expect( command.value ).to.be.undefined;
  292. } );
  293. } );
  294. describe( 'execute()', () => {
  295. it( 'should merge table cells ', () => {
  296. setData( model, modelTable( [
  297. [ '00', '01[]' ],
  298. [ '10', '11' ]
  299. ] ) );
  300. command.execute();
  301. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  302. [ '00', { rowspan: 2, contents: '[0111]' } ],
  303. [ '10' ]
  304. ] ) );
  305. } );
  306. it( 'should remove empty row if merging table cells ', () => {
  307. setData( model, modelTable( [
  308. [ { rowspan: 2, contents: '00' }, '01[]', { rowspan: 3, contents: '02' } ],
  309. [ '11' ],
  310. [ '20', '21' ]
  311. ] ) );
  312. command.execute();
  313. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  314. [ '00', '[0111]', { rowspan: 2, contents: '02' } ],
  315. [ '20', '21' ]
  316. ] ) );
  317. } );
  318. it( 'should not reduce rowspan on cells above removed empty row when merging table cells ', () => {
  319. setData( model, modelTable( [
  320. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  321. [ '11', '12' ],
  322. [ { rowspan: 2, contents: '20' }, '21[]', { rowspan: 3, contents: '22' } ],
  323. [ '31' ],
  324. [ '40', '41' ]
  325. ] ) );
  326. command.execute();
  327. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  328. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  329. [ '11', '12' ],
  330. [ '20', '[2131]', { rowspan: 2, contents: '22' } ],
  331. [ '40', '41' ]
  332. ] ) );
  333. } );
  334. } );
  335. } );
  336. describe( 'direction=up', () => {
  337. beforeEach( () => {
  338. command = new MergeCellCommand( editor, { direction: 'up' } );
  339. } );
  340. describe( 'isEnabled', () => {
  341. it( 'should be true if in cell that has mergeable cell in previous row', () => {
  342. setData( model, modelTable( [
  343. [ '00', '01' ],
  344. [ '10', '11[]' ]
  345. ] ) );
  346. expect( command.isEnabled ).to.be.true;
  347. } );
  348. it( 'should be false if in first row', () => {
  349. setData( model, modelTable( [
  350. [ '00[]', '01' ],
  351. [ '10', '11' ]
  352. ] ) );
  353. expect( command.isEnabled ).to.be.false;
  354. } );
  355. it( 'should be true if in a cell that has mergeable cell with the same colspan', () => {
  356. setData( model, modelTable( [
  357. [ { colspan: 2, contents: '00' }, '02' ],
  358. [ { colspan: 2, contents: '01[]' }, '12' ]
  359. ] ) );
  360. expect( command.isEnabled ).to.be.true;
  361. } );
  362. it( 'should be false if in a cell that potential mergeable cell has different colspan', () => {
  363. setData( model, modelTable( [
  364. [ { colspan: 2, contents: '00' }, '02' ],
  365. [ { colspan: 3, contents: '01[]' } ]
  366. ] ) );
  367. expect( command.isEnabled ).to.be.false;
  368. } );
  369. it( 'should be false if not in a cell', () => {
  370. setData( model, '<p>11[]</p>' );
  371. expect( command.isEnabled ).to.be.false;
  372. } );
  373. it( 'should be false if mergeable cell is in other table section then current cell', () => {
  374. setData( model, modelTable( [
  375. [ '00', '01' ],
  376. [ '10[]', '11' ]
  377. ], { headingRows: 1 } ) );
  378. expect( command.isEnabled ).to.be.false;
  379. } );
  380. } );
  381. describe( 'value', () => {
  382. it( 'should be set to mergeable cell', () => {
  383. setData( model, modelTable( [
  384. [ '00', '01' ],
  385. [ '10', '11[]' ]
  386. ] ) );
  387. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 1 ] ) );
  388. } );
  389. it( 'should be undefined if in first row', () => {
  390. setData( model, modelTable( [
  391. [ '00[]', '01' ],
  392. [ '10', '11' ]
  393. ] ) );
  394. expect( command.value ).to.be.undefined;
  395. } );
  396. it( 'should be set to mergeable cell with the same rowspan', () => {
  397. setData( model, modelTable( [
  398. [ { colspan: 2, contents: '00' }, '02' ],
  399. [ { colspan: 2, contents: '01[]' }, '12' ]
  400. ] ) );
  401. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 0 ] ) );
  402. } );
  403. it( 'should be set to mergeable cell in rows with spanned cells', () => {
  404. setData( model, modelTable( [
  405. [ { rowspan: 3, contents: '00' }, '11', '12', '13' ],
  406. [ { rowspan: 2, contents: '21' }, '22', '23' ],
  407. [ '32', { rowspan: 2, contents: '33[]' } ],
  408. [ { colspan: 2, contents: '40' }, '42' ]
  409. ] ) );
  410. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 1, 2 ] ) );
  411. } );
  412. it( 'should be undefined if in a cell that potential mergeable cell has different rowspan', () => {
  413. setData( model, modelTable( [
  414. [ { colspan: 2, contents: '00' }, '02' ],
  415. [ { colspan: 3, contents: '01[]' } ]
  416. ] ) );
  417. expect( command.value ).to.be.undefined;
  418. } );
  419. it( 'should be undefined if not in a cell', () => {
  420. setData( model, '<p>11[]</p>' );
  421. expect( command.value ).to.be.undefined;
  422. } );
  423. } );
  424. describe( 'execute()', () => {
  425. it( 'should merge table cells', () => {
  426. setData( model, modelTable( [
  427. [ '00', '01' ],
  428. [ '10', '[]11' ]
  429. ] ) );
  430. command.execute();
  431. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  432. [ '00', { rowspan: 2, contents: '[0111]' } ],
  433. [ '10' ]
  434. ] ) );
  435. } );
  436. it( 'should properly merge cells in rows with spaned cells', () => {
  437. setData( model, modelTable( [
  438. [ { rowspan: 3, contents: '00' }, '11', '12', '13' ],
  439. [ { rowspan: 2, contents: '21' }, '22', '23' ],
  440. [ '32', { rowspan: 2, contents: '33[]' } ],
  441. [ { colspan: 2, contents: '40' }, '42' ]
  442. ] ) );
  443. command.execute();
  444. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  445. [ { rowspan: 3, contents: '00' }, '11', '12', '13' ],
  446. [ { rowspan: 2, contents: '21' }, '22', { rowspan: 3, contents: '[2333]' } ],
  447. [ '32' ],
  448. [ { colspan: 2, contents: '40' }, '42' ]
  449. ] ) );
  450. } );
  451. it( 'should remove empty row if merging table cells ', () => {
  452. setData( model, modelTable( [
  453. [ { rowspan: 2, contents: '00' }, '01', { rowspan: 3, contents: '02' } ],
  454. [ '11[]' ],
  455. [ '20', '21' ]
  456. ] ) );
  457. command.execute();
  458. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  459. [ '00', '[0111]', { rowspan: 2, contents: '02' } ],
  460. [ '20', '21' ]
  461. ] ) );
  462. } );
  463. it( 'should not reduce rowspan on cells above removed empty row when merging table cells ', () => {
  464. setData( model, modelTable( [
  465. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  466. [ '11', '12' ],
  467. [ { rowspan: 2, contents: '20' }, '21', { rowspan: 3, contents: '22' } ],
  468. [ '31[]' ],
  469. [ '40', '41' ]
  470. ] ) );
  471. command.execute();
  472. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  473. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  474. [ '11', '12' ],
  475. [ '20', '[2131]', { rowspan: 2, contents: '22' } ],
  476. [ '40', '41' ]
  477. ] ) );
  478. } );
  479. } );
  480. } );
  481. } );