mergecellcommand.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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. it( 'should result in single empty paragraph if both cells are empty', () => {
  123. setData( model, modelTable( [
  124. [ '[]', '' ]
  125. ] ) );
  126. command.execute();
  127. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  128. [ { colspan: 2, contents: '<paragraph>[]</paragraph>' } ]
  129. ] ) );
  130. } );
  131. it( 'should result in single paragraph (other cell is empty)', () => {
  132. setData( model, modelTable( [
  133. [ 'foo[]', '' ]
  134. ] ) );
  135. command.execute();
  136. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  137. [ { colspan: 2, contents: '<paragraph>[foo]</paragraph>' } ]
  138. ] ) );
  139. } );
  140. it( 'should result in single paragraph (selection cell is empty)', () => {
  141. setData( model, modelTable( [
  142. [ '[]', 'foo' ]
  143. ] ) );
  144. command.execute();
  145. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  146. [ { colspan: 2, contents: '<paragraph>[foo]</paragraph>' } ]
  147. ] ) );
  148. } );
  149. it( 'should not merge other empty blocks to single block', () => {
  150. model.schema.register( 'block', {
  151. allowWhere: '$block',
  152. allowContentOf: '$block',
  153. isBlock: true
  154. } );
  155. setData( model, modelTable( [
  156. [ '<block>[]</block>', '<block></block>' ]
  157. ] ) );
  158. command.execute();
  159. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  160. [ { colspan: 2, contents: '<block>[</block><block>]</block>' } ]
  161. ] ) );
  162. } );
  163. } );
  164. } );
  165. describe( 'direction=left', () => {
  166. beforeEach( () => {
  167. command = new MergeCellCommand( editor, { direction: 'left' } );
  168. } );
  169. describe( 'isEnabled', () => {
  170. it( 'should be true if in cell that has sibling on the left', () => {
  171. setData( model, modelTable( [
  172. [ '00', '01[]' ]
  173. ] ) );
  174. expect( command.isEnabled ).to.be.true;
  175. } );
  176. it( 'should be false if first cell of a row', () => {
  177. setData( model, modelTable( [
  178. [ '00[]', '01' ]
  179. ] ) );
  180. expect( command.isEnabled ).to.be.false;
  181. } );
  182. it( 'should be true if in a cell that has sibling on the left with the same rowspan', () => {
  183. setData( model, modelTable( [
  184. [ { rowspan: 2, contents: '00' }, { rowspan: 2, contents: '01[]' } ]
  185. ] ) );
  186. expect( command.isEnabled ).to.be.true;
  187. } );
  188. it( 'should be false if in a cell that has sibling but with different rowspan', () => {
  189. setData( model, modelTable( [
  190. [ { rowspan: 2, contents: '00' }, { rowspan: 3, contents: '01[]' } ]
  191. ] ) );
  192. expect( command.isEnabled ).to.be.false;
  193. } );
  194. it( 'should be false when next cell is rowspanned', () => {
  195. setData( model, modelTable( [
  196. [ '00', { rowspan: 3, contents: '01' }, '02' ],
  197. [ '10', '12[]' ],
  198. [ '20', '22' ]
  199. ] ) );
  200. expect( command.isEnabled ).to.be.false;
  201. } );
  202. it( 'should be true when mergeable cell is colspanned', () => {
  203. setData( model, modelTable( [
  204. [ { colspan: 2, contents: '00' }, '02[]' ]
  205. ] ) );
  206. expect( command.isEnabled ).to.be.true;
  207. } );
  208. it( 'should be false if not in a cell', () => {
  209. setData( model, '<paragraph>11[]</paragraph>' );
  210. expect( command.isEnabled ).to.be.false;
  211. } );
  212. } );
  213. describe( 'value', () => {
  214. it( 'should be set to mergeable sibling if in cell that has sibling on the left', () => {
  215. setData( model, modelTable( [
  216. [ '00', '01[]' ]
  217. ] ) );
  218. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 0 ] ) );
  219. } );
  220. it( 'should be set to mergeable sibling if in cell that has sibling on the left (selection in block content)', () => {
  221. setData( model, modelTable( [
  222. [ '00', '<paragraph>01[]</paragraph>', '02' ]
  223. ] ) );
  224. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 0 ] ) );
  225. } );
  226. it( 'should be undefined if first cell of a row', () => {
  227. setData( model, modelTable( [
  228. [ '00[]', '01' ]
  229. ] ) );
  230. expect( command.value ).to.be.undefined;
  231. } );
  232. it( 'should be set to mergeable sibling if in a cell that has sibling on the left with the same rowspan', () => {
  233. setData( model, modelTable( [
  234. [ { rowspan: 2, contents: '00' }, { rowspan: 2, contents: '01[]' } ]
  235. ] ) );
  236. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 0 ] ) );
  237. } );
  238. it( 'should be undefined if in a cell that has sibling but with different rowspan', () => {
  239. setData( model, modelTable( [
  240. [ { rowspan: 2, contents: '00' }, { rowspan: 3, contents: '01[]' } ]
  241. ] ) );
  242. expect( command.value ).to.be.undefined;
  243. } );
  244. it( 'should be undefined if not in a cell', () => {
  245. setData( model, '<paragraph>11[]</paragraph>' );
  246. expect( command.value ).to.be.undefined;
  247. } );
  248. } );
  249. describe( 'execute()', () => {
  250. it( 'should merge table cells', () => {
  251. setData( model, modelTable( [
  252. [ '00', '[]01' ]
  253. ] ) );
  254. command.execute();
  255. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  256. [ { colspan: 2, contents: '<paragraph>[00</paragraph><paragraph>01]</paragraph>' } ]
  257. ] ) );
  258. } );
  259. it( 'should result in single empty paragraph if both cells are empty', () => {
  260. setData( model, modelTable( [
  261. [ '', '[]' ]
  262. ] ) );
  263. command.execute();
  264. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  265. [ { colspan: 2, contents: '<paragraph>[]</paragraph>' } ]
  266. ] ) );
  267. } );
  268. it( 'should result in single paragraph (other cell is empty)', () => {
  269. setData( model, modelTable( [
  270. [ '', 'foo[]' ]
  271. ] ) );
  272. command.execute();
  273. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  274. [ { colspan: 2, contents: '<paragraph>[foo]</paragraph>' } ]
  275. ] ) );
  276. } );
  277. it( 'should result in single paragraph (selection cell is empty)', () => {
  278. setData( model, modelTable( [
  279. [ 'foo', '[]' ]
  280. ] ) );
  281. command.execute();
  282. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  283. [ { colspan: 2, contents: '<paragraph>[foo]</paragraph>' } ]
  284. ] ) );
  285. } );
  286. it( 'should not merge other empty blocks to single block', () => {
  287. model.schema.register( 'block', {
  288. allowWhere: '$block',
  289. allowContentOf: '$block',
  290. isBlock: true
  291. } );
  292. setData( model, modelTable( [
  293. [ '<block></block>', '<block>[]</block>' ]
  294. ] ) );
  295. command.execute();
  296. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  297. [ { colspan: 2, contents: '<block>[</block><block>]</block>' } ]
  298. ] ) );
  299. } );
  300. } );
  301. } );
  302. describe( 'direction=down', () => {
  303. beforeEach( () => {
  304. command = new MergeCellCommand( editor, { direction: 'down' } );
  305. } );
  306. describe( 'isEnabled', () => {
  307. it( 'should be true if in cell that has mergeable cell in next row', () => {
  308. setData( model, modelTable( [
  309. [ '00', '01[]' ],
  310. [ '10', '11' ]
  311. ] ) );
  312. expect( command.isEnabled ).to.be.true;
  313. } );
  314. it( 'should be false if in last row', () => {
  315. setData( model, modelTable( [
  316. [ '00', '01' ],
  317. [ '10[]', '11' ]
  318. ] ) );
  319. expect( command.isEnabled ).to.be.false;
  320. } );
  321. it( 'should be true if in a cell that has mergeable cell with the same colspan', () => {
  322. setData( model, modelTable( [
  323. [ { colspan: 2, contents: '00[]' }, '02' ],
  324. [ { colspan: 2, contents: '01' }, '12' ]
  325. ] ) );
  326. expect( command.isEnabled ).to.be.true;
  327. } );
  328. it( 'should be false if in a cell that potential mergeable cell has different colspan', () => {
  329. setData( model, modelTable( [
  330. [ { colspan: 2, contents: '00[]' }, '02' ],
  331. [ { colspan: 3, contents: '01' } ]
  332. ] ) );
  333. expect( command.isEnabled ).to.be.false;
  334. } );
  335. it( 'should be false if not in a cell', () => {
  336. setData( model, '<paragraph>11[]</paragraph>' );
  337. expect( command.isEnabled ).to.be.false;
  338. } );
  339. it( 'should be false if mergeable cell is in other table section then current cell', () => {
  340. setData( model, modelTable( [
  341. [ '00[]', '01' ],
  342. [ '10', '11' ]
  343. ], { headingRows: 1 } ) );
  344. expect( command.isEnabled ).to.be.false;
  345. } );
  346. } );
  347. describe( 'value', () => {
  348. it( 'should be set to mergeable cell', () => {
  349. setData( model, modelTable( [
  350. [ '00', '01[]' ],
  351. [ '10', '11' ]
  352. ] ) );
  353. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 1, 1 ] ) );
  354. } );
  355. it( 'should be set to mergeable cell (selection in block content)', () => {
  356. setData( model, modelTable( [
  357. [ '00' ],
  358. [ '<paragraph>10[]</paragraph>' ],
  359. [ '20' ]
  360. ] ) );
  361. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 2, 0 ] ) );
  362. } );
  363. it( 'should be undefined if in last row', () => {
  364. setData( model, modelTable( [
  365. [ '00', '01' ],
  366. [ '10[]', '11' ]
  367. ] ) );
  368. expect( command.value ).to.be.undefined;
  369. } );
  370. it( 'should be set to mergeable cell with the same rowspan', () => {
  371. setData( model, modelTable( [
  372. [ { colspan: 2, contents: '00[]' }, '02' ],
  373. [ { colspan: 2, contents: '01' }, '12' ]
  374. ] ) );
  375. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 1, 0 ] ) );
  376. } );
  377. it( 'should be undefined if in a cell that potential mergeable cell has different rowspan', () => {
  378. setData( model, modelTable( [
  379. [ { colspan: 2, contents: '00[]' }, '02' ],
  380. [ { colspan: 3, contents: '01' } ]
  381. ] ) );
  382. expect( command.value ).to.be.undefined;
  383. } );
  384. it( 'should be undefined if mergable cell is in other table section', () => {
  385. setData( model, modelTable( [
  386. [ { rowspan: 2, contents: '00[]' }, '02' ],
  387. [ '12' ],
  388. [ '21', '22' ]
  389. ], { headingRows: 2 } ) );
  390. expect( command.value ).to.be.undefined;
  391. } );
  392. it( 'should be undefined if not in a cell', () => {
  393. setData( model, '<paragraph>11[]</paragraph>' );
  394. expect( command.value ).to.be.undefined;
  395. } );
  396. } );
  397. describe( 'execute()', () => {
  398. it( 'should merge table cells', () => {
  399. setData( model, modelTable( [
  400. [ '00', '01[]' ],
  401. [ '10', '11' ]
  402. ] ) );
  403. command.execute();
  404. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  405. [ '00', { rowspan: 2, contents: '<paragraph>[01</paragraph><paragraph>11]</paragraph>' } ],
  406. [ '10' ]
  407. ] ) );
  408. } );
  409. it( 'should result in single empty paragraph if both cells are empty', () => {
  410. setData( model, modelTable( [
  411. [ '[]', '' ],
  412. [ '', '' ]
  413. ] ) );
  414. command.execute();
  415. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  416. [ { rowspan: 2, contents: '[]' }, '' ],
  417. [ '' ]
  418. ] ) );
  419. } );
  420. it( 'should result in single paragraph (other cell is empty)', () => {
  421. setData( model, modelTable( [
  422. [ 'foo[]', '' ],
  423. [ '', '' ]
  424. ] ) );
  425. command.execute();
  426. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  427. [ { rowspan: 2, contents: '[foo]' }, '' ],
  428. [ '' ]
  429. ] ) );
  430. } );
  431. it( 'should result in single paragraph (selection cell is empty)', () => {
  432. setData( model, modelTable( [
  433. [ '[]', '' ],
  434. [ 'foo', '' ]
  435. ] ) );
  436. command.execute();
  437. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  438. [ { rowspan: 2, contents: '[foo]' }, '' ],
  439. [ '' ]
  440. ] ) );
  441. } );
  442. it( 'should not merge other empty blocks to single block', () => {
  443. model.schema.register( 'block', {
  444. allowWhere: '$block',
  445. allowContentOf: '$block',
  446. isBlock: true
  447. } );
  448. setData( model, modelTable( [
  449. [ '<block>[]</block>', '' ],
  450. [ '<block></block>', '' ]
  451. ] ) );
  452. command.execute();
  453. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  454. [ { rowspan: 2, contents: '<block>[</block><block>]</block>' }, '' ],
  455. [ '' ]
  456. ] ) );
  457. } );
  458. it( 'should remove empty row if merging table cells ', () => {
  459. setData( model, modelTable( [
  460. [ { rowspan: 2, contents: '00' }, '01[]', { rowspan: 3, contents: '02' } ],
  461. [ '11' ],
  462. [ '20', '21' ]
  463. ] ) );
  464. command.execute();
  465. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  466. [ '00', '<paragraph>[01</paragraph><paragraph>11]</paragraph>', { rowspan: 2, contents: '02' } ],
  467. [ '20', '21' ]
  468. ] ) );
  469. } );
  470. it( 'should not reduce rowspan on cells above removed empty row when merging table cells ', () => {
  471. setData( model, modelTable( [
  472. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  473. [ '11', '12' ],
  474. [ { rowspan: 2, contents: '20' }, '21[]', { rowspan: 3, contents: '22' } ],
  475. [ '31' ],
  476. [ '40', '41' ]
  477. ] ) );
  478. command.execute();
  479. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  480. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  481. [ '11', '12' ],
  482. [ '20', '<paragraph>[21</paragraph><paragraph>31]</paragraph>', { rowspan: 2, contents: '22' } ],
  483. [ '40', '41' ]
  484. ] ) );
  485. } );
  486. } );
  487. } );
  488. describe( 'direction=up', () => {
  489. beforeEach( () => {
  490. command = new MergeCellCommand( editor, { direction: 'up' } );
  491. } );
  492. describe( 'isEnabled', () => {
  493. it( 'should be true if in cell that has mergeable cell in previous row', () => {
  494. setData( model, modelTable( [
  495. [ '00', '01' ],
  496. [ '10', '11[]' ]
  497. ] ) );
  498. expect( command.isEnabled ).to.be.true;
  499. } );
  500. it( 'should be false if in first row', () => {
  501. setData( model, modelTable( [
  502. [ '00[]', '01' ],
  503. [ '10', '11' ]
  504. ] ) );
  505. expect( command.isEnabled ).to.be.false;
  506. } );
  507. it( 'should be true if in a cell that has mergeable cell with the same colspan', () => {
  508. setData( model, modelTable( [
  509. [ { colspan: 2, contents: '00' }, '02' ],
  510. [ { colspan: 2, contents: '01[]' }, '12' ]
  511. ] ) );
  512. expect( command.isEnabled ).to.be.true;
  513. } );
  514. it( 'should be false if in a cell that potential mergeable cell has different colspan', () => {
  515. setData( model, modelTable( [
  516. [ { colspan: 2, contents: '00' }, '02' ],
  517. [ { colspan: 3, contents: '01[]' } ]
  518. ] ) );
  519. expect( command.isEnabled ).to.be.false;
  520. } );
  521. it( 'should be false if not in a cell', () => {
  522. setData( model, '<paragraph>11[]</paragraph>' );
  523. expect( command.isEnabled ).to.be.false;
  524. } );
  525. it( 'should be false if mergeable cell is in other table section then current cell', () => {
  526. setData( model, modelTable( [
  527. [ '00', '01' ],
  528. [ '10[]', '11' ]
  529. ], { headingRows: 1 } ) );
  530. expect( command.isEnabled ).to.be.false;
  531. } );
  532. } );
  533. describe( 'value', () => {
  534. it( 'should be set to mergeable cell', () => {
  535. setData( model, modelTable( [
  536. [ '00', '01' ],
  537. [ '10', '11[]' ]
  538. ] ) );
  539. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 1 ] ) );
  540. } );
  541. it( 'should be set to mergeable cell (selection in block content)', () => {
  542. setData( model, modelTable( [
  543. [ '00' ],
  544. [ '<paragraph>10[]</paragraph>' ],
  545. [ '20' ]
  546. ] ) );
  547. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 0 ] ) );
  548. } );
  549. it( 'should be undefined if in first row', () => {
  550. setData( model, modelTable( [
  551. [ '00[]', '01' ],
  552. [ '10', '11' ]
  553. ] ) );
  554. expect( command.value ).to.be.undefined;
  555. } );
  556. it( 'should be set to mergeable cell with the same rowspan', () => {
  557. setData( model, modelTable( [
  558. [ { colspan: 2, contents: '00' }, '02' ],
  559. [ { colspan: 2, contents: '01[]' }, '12' ]
  560. ] ) );
  561. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 0 ] ) );
  562. } );
  563. it( 'should be set to mergeable cell in rows with spanned cells', () => {
  564. setData( model, modelTable( [
  565. [ { rowspan: 3, contents: '00' }, '11', '12', '13' ],
  566. [ { rowspan: 2, contents: '21' }, '22', '23' ],
  567. [ '32', { rowspan: 2, contents: '33[]' } ],
  568. [ { colspan: 2, contents: '40' }, '42' ]
  569. ] ) );
  570. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 1, 2 ] ) );
  571. } );
  572. it( 'should be undefined if in a cell that potential mergeable cell has different rowspan', () => {
  573. setData( model, modelTable( [
  574. [ { colspan: 2, contents: '00' }, '02' ],
  575. [ { colspan: 3, contents: '01[]' } ]
  576. ] ) );
  577. expect( command.value ).to.be.undefined;
  578. } );
  579. it( 'should be undefined if not in a cell', () => {
  580. setData( model, '<paragraph>11[]</paragraph>' );
  581. expect( command.value ).to.be.undefined;
  582. } );
  583. } );
  584. describe( 'execute()', () => {
  585. it( 'should merge table cells', () => {
  586. setData( model, modelTable( [
  587. [ '00', '01' ],
  588. [ '10', '[]11' ]
  589. ] ) );
  590. command.execute();
  591. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  592. [ '00', { rowspan: 2, contents: '<paragraph>[01</paragraph><paragraph>11]</paragraph>' } ],
  593. [ '10' ]
  594. ] ) );
  595. } );
  596. it( 'should result in single empty paragraph if both cells are empty', () => {
  597. setData( model, modelTable( [
  598. [ '', '' ],
  599. [ '[]', '' ]
  600. ] ) );
  601. command.execute();
  602. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  603. [ { rowspan: 2, contents: '[]' }, '' ],
  604. [ '' ]
  605. ] ) );
  606. } );
  607. it( 'should result in single paragraph (other cell is empty)', () => {
  608. setData( model, modelTable( [
  609. [ '', '' ],
  610. [ 'foo[]', '' ]
  611. ] ) );
  612. command.execute();
  613. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  614. [ { rowspan: 2, contents: '[foo]' }, '' ],
  615. [ '' ]
  616. ] ) );
  617. } );
  618. it( 'should result in single paragraph (selection cell is empty)', () => {
  619. setData( model, modelTable( [
  620. [ 'foo', '' ],
  621. [ '[]', '' ]
  622. ] ) );
  623. command.execute();
  624. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  625. [ { rowspan: 2, contents: '[foo]' }, '' ],
  626. [ '' ]
  627. ] ) );
  628. } );
  629. it( 'should not merge other empty blocks to single block', () => {
  630. model.schema.register( 'block', {
  631. allowWhere: '$block',
  632. allowContentOf: '$block',
  633. isBlock: true
  634. } );
  635. setData( model, modelTable( [
  636. [ '<block></block>', '' ],
  637. [ '<block>[]</block>', '' ]
  638. ] ) );
  639. command.execute();
  640. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  641. [ { rowspan: 2, contents: '<block>[</block><block>]</block>' }, '' ],
  642. [ '' ]
  643. ] ) );
  644. } );
  645. it( 'should properly merge cells in rows with spaned cells', () => {
  646. setData( model, modelTable( [
  647. [ { rowspan: 3, contents: '00' }, '11', '12', '13' ],
  648. [ { rowspan: 2, contents: '21' }, '22', '23' ],
  649. [ '32', { rowspan: 2, contents: '33[]' } ],
  650. [ { colspan: 2, contents: '40' }, '42' ]
  651. ] ) );
  652. command.execute();
  653. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  654. [ { rowspan: 3, contents: '00' }, '11', '12', '13' ],
  655. [
  656. { rowspan: 2, contents: '21' },
  657. '22',
  658. { rowspan: 3, contents: '<paragraph>[23</paragraph><paragraph>33]</paragraph>' }
  659. ],
  660. [ '32' ],
  661. [ { colspan: 2, contents: '40' }, '42' ]
  662. ] ) );
  663. } );
  664. it( 'should remove empty row if merging table cells ', () => {
  665. setData( model, modelTable( [
  666. [ { rowspan: 2, contents: '00' }, '01', { rowspan: 3, contents: '02' } ],
  667. [ '11[]' ],
  668. [ '20', '21' ]
  669. ] ) );
  670. command.execute();
  671. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  672. [ '00', '<paragraph>[01</paragraph><paragraph>11]</paragraph>', { rowspan: 2, contents: '02' } ],
  673. [ '20', '21' ]
  674. ] ) );
  675. } );
  676. it( 'should not reduce rowspan on cells above removed empty row when merging table cells ', () => {
  677. setData( model, modelTable( [
  678. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  679. [ '11', '12' ],
  680. [ { rowspan: 2, contents: '20' }, '21', { rowspan: 3, contents: '22' } ],
  681. [ '31[]' ],
  682. [ '40', '41' ]
  683. ] ) );
  684. command.execute();
  685. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  686. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  687. [ '11', '12' ],
  688. [ '20', '<paragraph>[21</paragraph><paragraph>31]</paragraph>', { rowspan: 2, contents: '22' } ],
  689. [ '40', '41' ]
  690. ] ) );
  691. } );
  692. } );
  693. } );
  694. } );