8
0

mergecellcommand.js 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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. [ '10', '11' ]
  150. ] ) );
  151. command.execute();
  152. assertEqualMarkup( getData( model ), modelTable( [
  153. [ { colspan: 2, contents: '<paragraph>[00</paragraph><paragraph>01]</paragraph>' } ],
  154. [ '10', '11' ]
  155. ] ) );
  156. } );
  157. it( 'should merge table cells and remove empty columns', () => {
  158. setData( model, modelTable( [
  159. [ '[]00', '01' ]
  160. ] ) );
  161. command.execute();
  162. assertEqualMarkup( getData( model ), modelTable( [
  163. [ '<paragraph>[00</paragraph><paragraph>01]</paragraph>' ]
  164. ] ) );
  165. } );
  166. it( 'should result in single empty paragraph if both cells are empty', () => {
  167. setData( model, modelTable( [
  168. [ '[]', '' ],
  169. [ '10', '11' ]
  170. ] ) );
  171. command.execute();
  172. assertEqualMarkup( getData( model ), modelTable( [
  173. [ { colspan: 2, contents: '<paragraph>[]</paragraph>' } ],
  174. [ '10', '11' ]
  175. ] ) );
  176. } );
  177. it( 'should result in single paragraph (other cell is empty)', () => {
  178. setData( model, modelTable( [
  179. [ 'foo[]', '' ],
  180. [ '10', '11' ]
  181. ] ) );
  182. command.execute();
  183. assertEqualMarkup( getData( model ), modelTable( [
  184. [ { colspan: 2, contents: '<paragraph>[foo]</paragraph>' } ],
  185. [ '10', '11' ]
  186. ] ) );
  187. } );
  188. it( 'should result in single paragraph (selection cell is empty)', () => {
  189. setData( model, modelTable( [
  190. [ '[]', 'foo' ],
  191. [ '10', '11' ]
  192. ] ) );
  193. command.execute();
  194. assertEqualMarkup( getData( model ), modelTable( [
  195. [ { colspan: 2, contents: '<paragraph>[foo]</paragraph>' } ],
  196. [ '10', '11' ]
  197. ] ) );
  198. } );
  199. it( 'should not merge other empty blocks to single block', () => {
  200. model.schema.register( 'block', {
  201. allowWhere: '$block',
  202. allowContentOf: '$block',
  203. isBlock: true
  204. } );
  205. setData( model, modelTable( [
  206. [ '<block>[]</block>', '<block></block>' ],
  207. [ '10', '11' ]
  208. ] ) );
  209. command.execute();
  210. assertEqualMarkup( getData( model ), modelTable( [
  211. [ { colspan: 2, contents: '<block>[</block><block>]</block>' } ],
  212. [ '10', '11' ]
  213. ] ) );
  214. } );
  215. } );
  216. } );
  217. describe( 'direction=left', () => {
  218. beforeEach( () => {
  219. command = new MergeCellCommand( editor, { direction: 'left' } );
  220. } );
  221. describe( 'isEnabled', () => {
  222. it( 'should be true if in cell that has sibling on the left', () => {
  223. setData( model, modelTable( [
  224. [ '00', '01[]' ]
  225. ] ) );
  226. expect( command.isEnabled ).to.be.true;
  227. } );
  228. it( 'should be false if first cell of a row', () => {
  229. setData( model, modelTable( [
  230. [ '00[]', '01' ]
  231. ] ) );
  232. expect( command.isEnabled ).to.be.false;
  233. } );
  234. it( 'should be true if in a cell that has sibling on the left with the same rowspan', () => {
  235. setData( model, modelTable( [
  236. [ { rowspan: 2, contents: '00' }, { rowspan: 2, contents: '01[]' } ]
  237. ] ) );
  238. expect( command.isEnabled ).to.be.true;
  239. } );
  240. it( 'should be false if in a cell that has sibling but with different rowspan', () => {
  241. setData( model, modelTable( [
  242. [ { rowspan: 2, contents: '00' }, { rowspan: 3, contents: '01[]' } ]
  243. ] ) );
  244. expect( command.isEnabled ).to.be.false;
  245. } );
  246. it( 'should be false when next cell is rowspanned', () => {
  247. setData( model, modelTable( [
  248. [ '00', { rowspan: 3, contents: '01' }, '02' ],
  249. [ '10', '12[]' ],
  250. [ '20', '22' ]
  251. ] ) );
  252. expect( command.isEnabled ).to.be.false;
  253. } );
  254. it( 'should be true when mergeable cell is colspanned', () => {
  255. setData( model, modelTable( [
  256. [ { colspan: 2, contents: '00' }, '02[]' ]
  257. ] ) );
  258. expect( command.isEnabled ).to.be.true;
  259. } );
  260. it( 'should be false if not in a cell', () => {
  261. setData( model, '<paragraph>11[]</paragraph>' );
  262. expect( command.isEnabled ).to.be.false;
  263. } );
  264. describe( 'when the heading section is in the table', () => {
  265. it( 'should be false if mergeable cell is in other table section then current cell', () => {
  266. setData( model, modelTable( [
  267. [ '00', '01[]' ]
  268. ], { headingColumns: 1 } ) );
  269. expect( command.isEnabled ).to.be.false;
  270. } );
  271. it( 'should be false if merged cell would cross heading section (mergeable cell with colspan)', () => {
  272. setData( model, modelTable( [
  273. [ { colspan: 2, contents: '00' }, '02[]', '03' ]
  274. ], { headingColumns: 2 } ) );
  275. expect( command.isEnabled ).to.be.false;
  276. } );
  277. it( 'should be true if merged cell would not cross the section boundary (in regular section)', () => {
  278. setData( model, modelTable( [
  279. [ '00', '01', '02[]', '03' ]
  280. ], { headingColumns: 1 } ) );
  281. expect( command.isEnabled ).to.be.true;
  282. } );
  283. it( 'should be true if merged cell would not cross the section boundary (in heading section)', () => {
  284. setData( model, modelTable( [
  285. [ '00', '01[]', '02', '03' ]
  286. ], { headingColumns: 2 } ) );
  287. expect( command.isEnabled ).to.be.true;
  288. } );
  289. } );
  290. } );
  291. describe( 'value', () => {
  292. it( 'should be set to mergeable sibling if in cell that has sibling on the left', () => {
  293. setData( model, modelTable( [
  294. [ '00', '01[]' ]
  295. ] ) );
  296. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 0 ] ) );
  297. } );
  298. it( 'should be set to mergeable sibling if in cell that has sibling on the left (selection in block content)', () => {
  299. setData( model, modelTable( [
  300. [ '00', '<paragraph>01[]</paragraph>', '02' ]
  301. ] ) );
  302. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 0 ] ) );
  303. } );
  304. it( 'should be undefined if first cell of a row', () => {
  305. setData( model, modelTable( [
  306. [ '00[]', '01' ]
  307. ] ) );
  308. expect( command.value ).to.be.undefined;
  309. } );
  310. it( 'should be set to mergeable sibling if in a cell that has sibling on the left with the same rowspan', () => {
  311. setData( model, modelTable( [
  312. [ { rowspan: 2, contents: '00' }, { rowspan: 2, contents: '01[]' } ]
  313. ] ) );
  314. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 0 ] ) );
  315. } );
  316. it( 'should be undefined if in a cell that has sibling but with different rowspan', () => {
  317. setData( model, modelTable( [
  318. [ { rowspan: 2, contents: '00' }, { rowspan: 3, contents: '01[]' } ]
  319. ] ) );
  320. expect( command.value ).to.be.undefined;
  321. } );
  322. it( 'should be undefined if not in a cell', () => {
  323. setData( model, '<paragraph>11[]</paragraph>' );
  324. expect( command.value ).to.be.undefined;
  325. } );
  326. } );
  327. describe( 'execute()', () => {
  328. it( 'should merge table cells', () => {
  329. setData( model, modelTable( [
  330. [ '00', '[]01' ],
  331. [ '10', '11' ]
  332. ] ) );
  333. command.execute();
  334. assertEqualMarkup( getData( model ), modelTable( [
  335. [ { colspan: 2, contents: '<paragraph>[00</paragraph><paragraph>01]</paragraph>' } ],
  336. [ '10', '11' ]
  337. ] ) );
  338. } );
  339. it( 'should merge table cells and remove empty columns', () => {
  340. setData( model, modelTable( [
  341. [ '00', '[]01' ]
  342. ] ) );
  343. command.execute();
  344. assertEqualMarkup( getData( model ), modelTable( [
  345. [ '<paragraph>[00</paragraph><paragraph>01]</paragraph>' ]
  346. ] ) );
  347. } );
  348. it( 'should result in single empty paragraph if both cells are empty', () => {
  349. setData( model, modelTable( [
  350. [ '', '[]' ],
  351. [ '10', '11' ]
  352. ] ) );
  353. command.execute();
  354. assertEqualMarkup( getData( model ), modelTable( [
  355. [ { colspan: 2, contents: '<paragraph>[]</paragraph>' } ],
  356. [ '10', '11' ]
  357. ] ) );
  358. } );
  359. it( 'should result in single paragraph (other cell is empty)', () => {
  360. setData( model, modelTable( [
  361. [ '', 'foo[]' ],
  362. [ '10', '11' ]
  363. ] ) );
  364. command.execute();
  365. assertEqualMarkup( getData( model ), modelTable( [
  366. [ { colspan: 2, contents: '<paragraph>[foo]</paragraph>' } ],
  367. [ '10', '11' ]
  368. ] ) );
  369. } );
  370. it( 'should result in single paragraph (selection cell is empty)', () => {
  371. setData( model, modelTable( [
  372. [ 'foo', '[]' ],
  373. [ '10', '11' ]
  374. ] ) );
  375. command.execute();
  376. assertEqualMarkup( getData( model ), modelTable( [
  377. [ { colspan: 2, contents: '<paragraph>[foo]</paragraph>' } ],
  378. [ '10', '11' ]
  379. ] ) );
  380. } );
  381. it( 'should not merge other empty blocks to single block', () => {
  382. model.schema.register( 'block', {
  383. allowWhere: '$block',
  384. allowContentOf: '$block',
  385. isBlock: true
  386. } );
  387. setData( model, modelTable( [
  388. [ '<block></block>', '<block>[]</block>' ],
  389. [ '10', '11' ]
  390. ] ) );
  391. command.execute();
  392. assertEqualMarkup( getData( model ), modelTable( [
  393. [ { colspan: 2, contents: '<block>[</block><block>]</block>' } ],
  394. [ '10', '11' ]
  395. ] ) );
  396. } );
  397. } );
  398. } );
  399. describe( 'direction=down', () => {
  400. beforeEach( () => {
  401. command = new MergeCellCommand( editor, { direction: 'down' } );
  402. } );
  403. describe( 'isEnabled', () => {
  404. it( 'should be true if in cell that has mergeable cell in next row', () => {
  405. setData( model, modelTable( [
  406. [ '00', '01[]' ],
  407. [ '10', '11' ]
  408. ] ) );
  409. expect( command.isEnabled ).to.be.true;
  410. } );
  411. it( 'should be false if in last row', () => {
  412. setData( model, modelTable( [
  413. [ '00', '01' ],
  414. [ '10[]', '11' ]
  415. ] ) );
  416. expect( command.isEnabled ).to.be.false;
  417. } );
  418. it( 'should be true if in a cell that has mergeable cell with the same colspan', () => {
  419. setData( model, modelTable( [
  420. [ { colspan: 2, contents: '00[]' }, '02' ],
  421. [ { colspan: 2, contents: '01' }, '12' ]
  422. ] ) );
  423. expect( command.isEnabled ).to.be.true;
  424. } );
  425. it( 'should be false if in a cell that potential mergeable cell has different colspan', () => {
  426. setData( model, modelTable( [
  427. [ { colspan: 2, contents: '00[]' }, '02' ],
  428. [ { colspan: 3, contents: '01' } ]
  429. ] ) );
  430. expect( command.isEnabled ).to.be.false;
  431. } );
  432. it( 'should be false if not in a cell', () => {
  433. setData( model, '<paragraph>11[]</paragraph>' );
  434. expect( command.isEnabled ).to.be.false;
  435. } );
  436. it( 'should be false if mergeable cell is in other table section then current cell', () => {
  437. setData( model, modelTable( [
  438. [ '00[]', '01' ],
  439. [ '10', '11' ]
  440. ], { headingRows: 1 } ) );
  441. expect( command.isEnabled ).to.be.false;
  442. } );
  443. } );
  444. describe( 'value', () => {
  445. it( 'should be set to mergeable cell', () => {
  446. setData( model, modelTable( [
  447. [ '00', '01[]' ],
  448. [ '10', '11' ]
  449. ] ) );
  450. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 1, 1 ] ) );
  451. } );
  452. it( 'should be set to mergeable cell (selection in block content)', () => {
  453. setData( model, modelTable( [
  454. [ '00' ],
  455. [ '<paragraph>10[]</paragraph>' ],
  456. [ '20' ]
  457. ] ) );
  458. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 2, 0 ] ) );
  459. } );
  460. it( 'should be undefined if in last row', () => {
  461. setData( model, modelTable( [
  462. [ '00', '01' ],
  463. [ '10[]', '11' ]
  464. ] ) );
  465. expect( command.value ).to.be.undefined;
  466. } );
  467. it( 'should be set to mergeable cell with the same rowspan', () => {
  468. setData( model, modelTable( [
  469. [ { colspan: 2, contents: '00[]' }, '02' ],
  470. [ { colspan: 2, contents: '01' }, '12' ]
  471. ] ) );
  472. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 1, 0 ] ) );
  473. } );
  474. it( 'should be undefined if in a cell that potential mergeable cell has different rowspan', () => {
  475. setData( model, modelTable( [
  476. [ { colspan: 2, contents: '00[]' }, '02' ],
  477. [ { colspan: 3, contents: '01' } ]
  478. ] ) );
  479. expect( command.value ).to.be.undefined;
  480. } );
  481. it( 'should be undefined if mergable cell is in other table section', () => {
  482. setData( model, modelTable( [
  483. [ { rowspan: 2, contents: '00[]' }, '02' ],
  484. [ '12' ],
  485. [ '21', '22' ]
  486. ], { headingRows: 2 } ) );
  487. expect( command.value ).to.be.undefined;
  488. } );
  489. it( 'should be undefined if not in a cell', () => {
  490. setData( model, '<paragraph>11[]</paragraph>' );
  491. expect( command.value ).to.be.undefined;
  492. } );
  493. } );
  494. describe( 'execute()', () => {
  495. it( 'should merge table cells', () => {
  496. setData( model, modelTable( [
  497. [ '00', '01[]' ],
  498. [ '10', '11' ]
  499. ] ) );
  500. command.execute();
  501. assertEqualMarkup( getData( model ), modelTable( [
  502. [ '00', { rowspan: 2, contents: '<paragraph>[01</paragraph><paragraph>11]</paragraph>' } ],
  503. [ '10' ]
  504. ] ) );
  505. } );
  506. it( 'should result in single empty paragraph if both cells are empty', () => {
  507. setData( model, modelTable( [
  508. [ '[]', '' ],
  509. [ '', '' ]
  510. ] ) );
  511. command.execute();
  512. assertEqualMarkup( getData( model ), modelTable( [
  513. [ { rowspan: 2, contents: '[]' }, '' ],
  514. [ '' ]
  515. ] ) );
  516. } );
  517. it( 'should result in single paragraph (other cell is empty)', () => {
  518. setData( model, modelTable( [
  519. [ 'foo[]', '' ],
  520. [ '', '' ]
  521. ] ) );
  522. command.execute();
  523. assertEqualMarkup( getData( model ), modelTable( [
  524. [ { rowspan: 2, contents: '[foo]' }, '' ],
  525. [ '' ]
  526. ] ) );
  527. } );
  528. it( 'should result in single paragraph (selection cell is empty)', () => {
  529. setData( model, modelTable( [
  530. [ '[]', '' ],
  531. [ 'foo', '' ]
  532. ] ) );
  533. command.execute();
  534. assertEqualMarkup( getData( model ), modelTable( [
  535. [ { rowspan: 2, contents: '[foo]' }, '' ],
  536. [ '' ]
  537. ] ) );
  538. } );
  539. it( 'should not merge other empty blocks to single block', () => {
  540. model.schema.register( 'block', {
  541. allowWhere: '$block',
  542. allowContentOf: '$block',
  543. isBlock: true
  544. } );
  545. setData( model, modelTable( [
  546. [ '<block>[]</block>', '' ],
  547. [ '<block></block>', '' ]
  548. ] ) );
  549. command.execute();
  550. assertEqualMarkup( getData( model ), modelTable( [
  551. [ { rowspan: 2, contents: '<block>[</block><block>]</block>' }, '' ],
  552. [ '' ]
  553. ] ) );
  554. } );
  555. it( 'should remove empty row if merging table cells ', () => {
  556. setData( model, modelTable( [
  557. [ { rowspan: 2, contents: '00' }, '01[]', { rowspan: 3, contents: '02' } ],
  558. [ '11' ],
  559. [ '20', '21' ]
  560. ] ) );
  561. command.execute();
  562. assertEqualMarkup( getData( model ), modelTable( [
  563. [ '00', '<paragraph>[01</paragraph><paragraph>11]</paragraph>', { rowspan: 2, contents: '02' } ],
  564. [ '20', '21' ]
  565. ] ) );
  566. } );
  567. it( 'should not reduce rowspan on cells above removed empty row when merging table cells ', () => {
  568. setData( model, modelTable( [
  569. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  570. [ '11', '12' ],
  571. [ { rowspan: 2, contents: '20' }, '21[]', { rowspan: 3, contents: '22' } ],
  572. [ '31' ],
  573. [ '40', '41' ]
  574. ] ) );
  575. command.execute();
  576. assertEqualMarkup( getData( model ), modelTable( [
  577. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  578. [ '11', '12' ],
  579. [ '20', '<paragraph>[21</paragraph><paragraph>31]</paragraph>', { rowspan: 2, contents: '22' } ],
  580. [ '40', '41' ]
  581. ] ) );
  582. } );
  583. it( 'should adjust heading rows if empty row was removed ', () => {
  584. // +----+----+
  585. // | 00 | 01 |
  586. // + +----+
  587. // | | 11 |
  588. // +----+----+ <-- heading rows
  589. // | 20 | 21 |
  590. // +----+----+
  591. setData( model, modelTable( [
  592. [ { contents: '00', rowspan: 2 }, '[]01' ],
  593. [ '11' ],
  594. [ '20', '21' ]
  595. ], { headingRows: 2 } ) );
  596. command.execute();
  597. assertEqualMarkup( getData( model ), modelTable( [
  598. [ '00', '<paragraph>[01</paragraph><paragraph>11]</paragraph>' ],
  599. [ '20', '21' ]
  600. ], { headingRows: 1 } ) );
  601. } );
  602. it( 'should create one undo step (1 batch)', () => {
  603. // +----+----+
  604. // | 00 | 01 |
  605. // + +----+
  606. // | | 11 |
  607. // +----+----+ <-- heading rows
  608. // | 20 | 21 |
  609. // +----+----+
  610. setData( model, modelTable( [
  611. [ { contents: '00', rowspan: 2 }, '[]01' ],
  612. [ '11' ],
  613. [ '20', '21' ]
  614. ], { headingRows: 2 } ) );
  615. const createdBatches = new Set();
  616. model.on( 'applyOperation', ( evt, [ operation ] ) => {
  617. createdBatches.add( operation.batch );
  618. } );
  619. command.execute();
  620. expect( createdBatches.size ).to.equal( 1 );
  621. } );
  622. } );
  623. } );
  624. describe( 'direction=up', () => {
  625. beforeEach( () => {
  626. command = new MergeCellCommand( editor, { direction: 'up' } );
  627. } );
  628. describe( 'isEnabled', () => {
  629. it( 'should be true if in cell that has mergeable cell in previous row', () => {
  630. setData( model, modelTable( [
  631. [ '00', '01' ],
  632. [ '10', '11[]' ]
  633. ] ) );
  634. expect( command.isEnabled ).to.be.true;
  635. } );
  636. it( 'should be false if in first row', () => {
  637. setData( model, modelTable( [
  638. [ '00[]', '01' ],
  639. [ '10', '11' ]
  640. ] ) );
  641. expect( command.isEnabled ).to.be.false;
  642. } );
  643. it( 'should be true if in a cell that has mergeable cell with the same colspan', () => {
  644. setData( model, modelTable( [
  645. [ { colspan: 2, contents: '00' }, '02' ],
  646. [ { colspan: 2, contents: '01[]' }, '12' ]
  647. ] ) );
  648. expect( command.isEnabled ).to.be.true;
  649. } );
  650. it( 'should be false if in a cell that potential mergeable cell has different colspan', () => {
  651. setData( model, modelTable( [
  652. [ { colspan: 2, contents: '00' }, '02' ],
  653. [ { colspan: 3, contents: '01[]' } ]
  654. ] ) );
  655. expect( command.isEnabled ).to.be.false;
  656. } );
  657. it( 'should be false if not in a cell', () => {
  658. setData( model, '<paragraph>11[]</paragraph>' );
  659. expect( command.isEnabled ).to.be.false;
  660. } );
  661. it( 'should be false if mergeable cell is in other table section then current cell', () => {
  662. setData( model, modelTable( [
  663. [ '00', '01' ],
  664. [ '10[]', '11' ]
  665. ], { headingRows: 1 } ) );
  666. expect( command.isEnabled ).to.be.false;
  667. } );
  668. } );
  669. describe( 'value', () => {
  670. it( 'should be set to mergeable cell', () => {
  671. setData( model, modelTable( [
  672. [ '00', '01' ],
  673. [ '10', '11[]' ]
  674. ] ) );
  675. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 1 ] ) );
  676. } );
  677. it( 'should be set to mergeable cell (selection in block content)', () => {
  678. setData( model, modelTable( [
  679. [ '00' ],
  680. [ '<paragraph>10[]</paragraph>' ],
  681. [ '20' ]
  682. ] ) );
  683. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 0 ] ) );
  684. } );
  685. it( 'should be undefined if in first row', () => {
  686. setData( model, modelTable( [
  687. [ '00[]', '01' ],
  688. [ '10', '11' ]
  689. ] ) );
  690. expect( command.value ).to.be.undefined;
  691. } );
  692. it( 'should be set to mergeable cell with the same rowspan', () => {
  693. setData( model, modelTable( [
  694. [ { colspan: 2, contents: '00' }, '02' ],
  695. [ { colspan: 2, contents: '01[]' }, '12' ]
  696. ] ) );
  697. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 0 ] ) );
  698. } );
  699. it( 'should be set to mergeable cell in rows with spanned cells', () => {
  700. setData( model, modelTable( [
  701. [ { rowspan: 3, contents: '00' }, '11', '12', '13' ],
  702. [ { rowspan: 2, contents: '21' }, '22', '23' ],
  703. [ '32', { rowspan: 2, contents: '33[]' } ],
  704. [ { colspan: 2, contents: '40' }, '42' ]
  705. ] ) );
  706. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 1, 2 ] ) );
  707. } );
  708. it( 'should be undefined if in a cell that potential mergeable cell has different rowspan', () => {
  709. setData( model, modelTable( [
  710. [ { colspan: 2, contents: '00' }, '02' ],
  711. [ { colspan: 3, contents: '01[]' } ]
  712. ] ) );
  713. expect( command.value ).to.be.undefined;
  714. } );
  715. it( 'should be undefined if not in a cell', () => {
  716. setData( model, '<paragraph>11[]</paragraph>' );
  717. expect( command.value ).to.be.undefined;
  718. } );
  719. } );
  720. describe( 'execute()', () => {
  721. it( 'should merge table cells', () => {
  722. setData( model, modelTable( [
  723. [ '00', '01' ],
  724. [ '10', '[]11' ]
  725. ] ) );
  726. command.execute();
  727. assertEqualMarkup( getData( model ), modelTable( [
  728. [ '00', { rowspan: 2, contents: '<paragraph>[01</paragraph><paragraph>11]</paragraph>' } ],
  729. [ '10' ]
  730. ] ) );
  731. } );
  732. it( 'should result in single empty paragraph if both cells are empty', () => {
  733. setData( model, modelTable( [
  734. [ '', '' ],
  735. [ '[]', '' ]
  736. ] ) );
  737. command.execute();
  738. assertEqualMarkup( getData( model ), modelTable( [
  739. [ { rowspan: 2, contents: '[]' }, '' ],
  740. [ '' ]
  741. ] ) );
  742. } );
  743. it( 'should result in single paragraph (other cell is empty)', () => {
  744. setData( model, modelTable( [
  745. [ '', '' ],
  746. [ 'foo[]', '' ]
  747. ] ) );
  748. command.execute();
  749. assertEqualMarkup( getData( model ), modelTable( [
  750. [ { rowspan: 2, contents: '[foo]' }, '' ],
  751. [ '' ]
  752. ] ) );
  753. } );
  754. it( 'should result in single paragraph (selection cell is empty)', () => {
  755. setData( model, modelTable( [
  756. [ 'foo', '' ],
  757. [ '[]', '' ]
  758. ] ) );
  759. command.execute();
  760. assertEqualMarkup( getData( model ), modelTable( [
  761. [ { rowspan: 2, contents: '[foo]' }, '' ],
  762. [ '' ]
  763. ] ) );
  764. } );
  765. it( 'should not merge other empty blocks to single block', () => {
  766. model.schema.register( 'block', {
  767. allowWhere: '$block',
  768. allowContentOf: '$block',
  769. isBlock: true
  770. } );
  771. setData( model, modelTable( [
  772. [ '<block></block>', '' ],
  773. [ '<block>[]</block>', '' ]
  774. ] ) );
  775. command.execute();
  776. assertEqualMarkup( getData( model ), modelTable( [
  777. [ { rowspan: 2, contents: '<block>[</block><block>]</block>' }, '' ],
  778. [ '' ]
  779. ] ) );
  780. } );
  781. it( 'should properly merge cells in rows with spaned cells', () => {
  782. setData( model, modelTable( [
  783. [ { rowspan: 3, contents: '00' }, '11', '12', '13' ],
  784. [ { rowspan: 2, contents: '21' }, '22', '23' ],
  785. [ '32', { rowspan: 2, contents: '33[]' } ],
  786. [ { colspan: 2, contents: '40' }, '42' ]
  787. ] ) );
  788. command.execute();
  789. assertEqualMarkup( getData( model ), modelTable( [
  790. [ { rowspan: 3, contents: '00' }, '11', '12', '13' ],
  791. [
  792. { rowspan: 2, contents: '21' },
  793. '22',
  794. { rowspan: 3, contents: '<paragraph>[23</paragraph><paragraph>33]</paragraph>' }
  795. ],
  796. [ '32' ],
  797. [ { colspan: 2, contents: '40' }, '42' ]
  798. ] ) );
  799. } );
  800. it( 'should remove empty row if merging table cells ', () => {
  801. setData( model, modelTable( [
  802. [ { rowspan: 2, contents: '00' }, '01', { rowspan: 3, contents: '02' } ],
  803. [ '11[]' ],
  804. [ '20', '21' ]
  805. ] ) );
  806. command.execute();
  807. assertEqualMarkup( getData( model ), modelTable( [
  808. [ '00', '<paragraph>[01</paragraph><paragraph>11]</paragraph>', { rowspan: 2, contents: '02' } ],
  809. [ '20', '21' ]
  810. ] ) );
  811. } );
  812. it( 'should not reduce rowspan on cells above removed empty row when merging table cells ', () => {
  813. setData( model, modelTable( [
  814. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  815. [ '11', '12' ],
  816. [ { rowspan: 2, contents: '20' }, '21', { rowspan: 3, contents: '22' } ],
  817. [ '31[]' ],
  818. [ '40', '41' ]
  819. ] ) );
  820. command.execute();
  821. assertEqualMarkup( getData( model ), modelTable( [
  822. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  823. [ '11', '12' ],
  824. [ '20', '<paragraph>[21</paragraph><paragraph>31]</paragraph>', { rowspan: 2, contents: '22' } ],
  825. [ '40', '41' ]
  826. ] ) );
  827. } );
  828. it( 'should adjust heading rows if empty row was removed ', () => {
  829. // +----+----+
  830. // | 00 | 01 |
  831. // + +----+
  832. // | | 11 |
  833. // +----+----+ <-- heading rows
  834. // | 20 | 21 |
  835. // +----+----+
  836. setData( model, modelTable( [
  837. [ { contents: '00', rowspan: 2 }, '01' ],
  838. [ '[]11' ],
  839. [ '20', '21' ]
  840. ], { headingRows: 2 } ) );
  841. command.execute();
  842. assertEqualMarkup( getData( model ), modelTable( [
  843. [ '00', '<paragraph>[01</paragraph><paragraph>11]</paragraph>' ],
  844. [ '20', '21' ]
  845. ], { headingRows: 1 } ) );
  846. } );
  847. it( 'should create one undo step (1 batch)', () => {
  848. // +----+----+
  849. // | 00 | 01 |
  850. // + +----+
  851. // | | 11 |
  852. // +----+----+ <-- heading rows
  853. // | 20 | 21 |
  854. // +----+----+
  855. setData( model, modelTable( [
  856. [ { contents: '00', rowspan: 2 }, '01' ],
  857. [ '[]11' ],
  858. [ '20', '21' ]
  859. ], { headingRows: 2 } ) );
  860. const createdBatches = new Set();
  861. model.on( 'applyOperation', ( evt, [ operation ] ) => {
  862. createdBatches.add( operation.batch );
  863. } );
  864. command.execute();
  865. expect( createdBatches.size ).to.equal( 1 );
  866. } );
  867. } );
  868. } );
  869. } );