mergecellcommand.js 26 KB

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