mergecellcommand.js 24 KB

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