8
0

mergecellcommand.js 26 KB

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