8
0

mergecellcommand.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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 not in a cell', () => {
  303. setData( model, '<paragraph>11[]</paragraph>' );
  304. expect( command.value ).to.be.undefined;
  305. } );
  306. } );
  307. describe( 'execute()', () => {
  308. it( 'should merge table cells', () => {
  309. setData( model, modelTable( [
  310. [ '00', '01[]' ],
  311. [ '10', '11' ]
  312. ] ) );
  313. command.execute();
  314. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  315. [ '00', { rowspan: 2, contents: '[<paragraph>01</paragraph><paragraph>11</paragraph>]' } ],
  316. [ '10' ]
  317. ] ) );
  318. } );
  319. it( 'should remove empty row if merging table cells ', () => {
  320. setData( model, modelTable( [
  321. [ { rowspan: 2, contents: '00' }, '01[]', { rowspan: 3, contents: '02' } ],
  322. [ '11' ],
  323. [ '20', '21' ]
  324. ] ) );
  325. command.execute();
  326. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  327. [ '00', '[<paragraph>01</paragraph><paragraph>11</paragraph>]', { rowspan: 2, contents: '02' } ],
  328. [ '20', '21' ]
  329. ] ) );
  330. } );
  331. it( 'should not reduce rowspan on cells above removed empty row when merging table cells ', () => {
  332. setData( model, modelTable( [
  333. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  334. [ '11', '12' ],
  335. [ { rowspan: 2, contents: '20' }, '21[]', { rowspan: 3, contents: '22' } ],
  336. [ '31' ],
  337. [ '40', '41' ]
  338. ] ) );
  339. command.execute();
  340. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  341. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  342. [ '11', '12' ],
  343. [ '20', '[<paragraph>21</paragraph><paragraph>31</paragraph>]', { rowspan: 2, contents: '22' } ],
  344. [ '40', '41' ]
  345. ] ) );
  346. } );
  347. } );
  348. } );
  349. describe( 'direction=up', () => {
  350. beforeEach( () => {
  351. command = new MergeCellCommand( editor, { direction: 'up' } );
  352. } );
  353. describe( 'isEnabled', () => {
  354. it( 'should be true if in cell that has mergeable cell in previous row', () => {
  355. setData( model, modelTable( [
  356. [ '00', '01' ],
  357. [ '10', '11[]' ]
  358. ] ) );
  359. expect( command.isEnabled ).to.be.true;
  360. } );
  361. it( 'should be false if in first row', () => {
  362. setData( model, modelTable( [
  363. [ '00[]', '01' ],
  364. [ '10', '11' ]
  365. ] ) );
  366. expect( command.isEnabled ).to.be.false;
  367. } );
  368. it( 'should be true if in a cell that has mergeable cell with the same colspan', () => {
  369. setData( model, modelTable( [
  370. [ { colspan: 2, contents: '00' }, '02' ],
  371. [ { colspan: 2, contents: '01[]' }, '12' ]
  372. ] ) );
  373. expect( command.isEnabled ).to.be.true;
  374. } );
  375. it( 'should be false if in a cell that potential mergeable cell has different colspan', () => {
  376. setData( model, modelTable( [
  377. [ { colspan: 2, contents: '00' }, '02' ],
  378. [ { colspan: 3, contents: '01[]' } ]
  379. ] ) );
  380. expect( command.isEnabled ).to.be.false;
  381. } );
  382. it( 'should be false if not in a cell', () => {
  383. setData( model, '<paragraph>11[]</paragraph>' );
  384. expect( command.isEnabled ).to.be.false;
  385. } );
  386. it( 'should be false if mergeable cell is in other table section then current cell', () => {
  387. setData( model, modelTable( [
  388. [ '00', '01' ],
  389. [ '10[]', '11' ]
  390. ], { headingRows: 1 } ) );
  391. expect( command.isEnabled ).to.be.false;
  392. } );
  393. } );
  394. describe( 'value', () => {
  395. it( 'should be set to mergeable cell', () => {
  396. setData( model, modelTable( [
  397. [ '00', '01' ],
  398. [ '10', '11[]' ]
  399. ] ) );
  400. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 1 ] ) );
  401. } );
  402. it( 'should be set to mergeable cell (selection in block content)', () => {
  403. setData( model, modelTable( [
  404. [ '00' ],
  405. [ '<paragraph>10[]</paragraph>' ],
  406. [ '20' ]
  407. ] ) );
  408. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 0 ] ) );
  409. } );
  410. it( 'should be undefined if in first row', () => {
  411. setData( model, modelTable( [
  412. [ '00[]', '01' ],
  413. [ '10', '11' ]
  414. ] ) );
  415. expect( command.value ).to.be.undefined;
  416. } );
  417. it( 'should be set to mergeable cell with the same rowspan', () => {
  418. setData( model, modelTable( [
  419. [ { colspan: 2, contents: '00' }, '02' ],
  420. [ { colspan: 2, contents: '01[]' }, '12' ]
  421. ] ) );
  422. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 0 ] ) );
  423. } );
  424. it( 'should be set to mergeable cell in rows with spanned cells', () => {
  425. setData( model, modelTable( [
  426. [ { rowspan: 3, contents: '00' }, '11', '12', '13' ],
  427. [ { rowspan: 2, contents: '21' }, '22', '23' ],
  428. [ '32', { rowspan: 2, contents: '33[]' } ],
  429. [ { colspan: 2, contents: '40' }, '42' ]
  430. ] ) );
  431. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 1, 2 ] ) );
  432. } );
  433. it( 'should be undefined if in a cell that potential mergeable cell has different rowspan', () => {
  434. setData( model, modelTable( [
  435. [ { colspan: 2, contents: '00' }, '02' ],
  436. [ { colspan: 3, contents: '01[]' } ]
  437. ] ) );
  438. expect( command.value ).to.be.undefined;
  439. } );
  440. it( 'should be undefined if not in a cell', () => {
  441. setData( model, '<paragraph>11[]</paragraph>' );
  442. expect( command.value ).to.be.undefined;
  443. } );
  444. } );
  445. describe( 'execute()', () => {
  446. it( 'should merge table cells', () => {
  447. setData( model, modelTable( [
  448. [ '00', '01' ],
  449. [ '10', '[]11' ]
  450. ] ) );
  451. command.execute();
  452. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  453. [ '00', { rowspan: 2, contents: '[<paragraph>01</paragraph><paragraph>11</paragraph>]' } ],
  454. [ '10' ]
  455. ] ) );
  456. } );
  457. it( 'should properly merge cells in rows with spaned cells', () => {
  458. setData( model, modelTable( [
  459. [ { rowspan: 3, contents: '00' }, '11', '12', '13' ],
  460. [ { rowspan: 2, contents: '21' }, '22', '23' ],
  461. [ '32', { rowspan: 2, contents: '33[]' } ],
  462. [ { colspan: 2, contents: '40' }, '42' ]
  463. ] ) );
  464. command.execute();
  465. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  466. [ { rowspan: 3, contents: '00' }, '11', '12', '13' ],
  467. [
  468. { rowspan: 2, contents: '21' },
  469. '22',
  470. { rowspan: 3, contents: '[<paragraph>23</paragraph><paragraph>33</paragraph>]' }
  471. ],
  472. [ '32' ],
  473. [ { colspan: 2, contents: '40' }, '42' ]
  474. ] ) );
  475. } );
  476. it( 'should remove empty row if merging table cells ', () => {
  477. setData( model, modelTable( [
  478. [ { rowspan: 2, contents: '00' }, '01', { rowspan: 3, contents: '02' } ],
  479. [ '11[]' ],
  480. [ '20', '21' ]
  481. ] ) );
  482. command.execute();
  483. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  484. [ '00', '[<paragraph>01</paragraph><paragraph>11</paragraph>]', { rowspan: 2, contents: '02' } ],
  485. [ '20', '21' ]
  486. ] ) );
  487. } );
  488. it( 'should not reduce rowspan on cells above removed empty row when merging table cells ', () => {
  489. setData( model, modelTable( [
  490. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  491. [ '11', '12' ],
  492. [ { rowspan: 2, contents: '20' }, '21', { rowspan: 3, contents: '22' } ],
  493. [ '31[]' ],
  494. [ '40', '41' ]
  495. ] ) );
  496. command.execute();
  497. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  498. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  499. [ '11', '12' ],
  500. [ '20', '[<paragraph>21</paragraph><paragraph>31</paragraph>]', { rowspan: 2, contents: '22' } ],
  501. [ '40', '41' ]
  502. ] ) );
  503. } );
  504. } );
  505. } );
  506. } );