8
0

mergecellcommand.js 18 KB

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