mergecellcommand.js 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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. it( 'should adjust heading rows if empty row was removed ', () => {
  546. // +----+----+
  547. // | 00 | 01 |
  548. // + +----+
  549. // | | 11 |
  550. // +----+----+ <-- heading rows
  551. // | 20 | 21 |
  552. // +----+----+
  553. setData( model, modelTable( [
  554. [ { contents: '00', rowspan: 2 }, '[]01' ],
  555. [ '11' ],
  556. [ '20', '21' ]
  557. ], { headingRows: 2 } ) );
  558. command.execute();
  559. assertEqualMarkup( getData( model ), modelTable( [
  560. [ '00', '<paragraph>[01</paragraph><paragraph>11]</paragraph>' ],
  561. [ '20', '21' ]
  562. ], { headingRows: 1 } ) );
  563. } );
  564. it( 'should create one undo step (1 batch)', () => {
  565. // +----+----+
  566. // | 00 | 01 |
  567. // + +----+
  568. // | | 11 |
  569. // +----+----+ <-- heading rows
  570. // | 20 | 21 |
  571. // +----+----+
  572. setData( model, modelTable( [
  573. [ { contents: '00', rowspan: 2 }, '[]01' ],
  574. [ '11' ],
  575. [ '20', '21' ]
  576. ], { headingRows: 2 } ) );
  577. const createdBatches = new Set();
  578. model.on( 'applyOperation', ( evt, [ operation ] ) => {
  579. createdBatches.add( operation.batch );
  580. } );
  581. command.execute();
  582. expect( createdBatches.size ).to.equal( 1 );
  583. } );
  584. } );
  585. } );
  586. describe( 'direction=up', () => {
  587. beforeEach( () => {
  588. command = new MergeCellCommand( editor, { direction: 'up' } );
  589. } );
  590. describe( 'isEnabled', () => {
  591. it( 'should be true if in cell that has mergeable cell in previous row', () => {
  592. setData( model, modelTable( [
  593. [ '00', '01' ],
  594. [ '10', '11[]' ]
  595. ] ) );
  596. expect( command.isEnabled ).to.be.true;
  597. } );
  598. it( 'should be false if in first row', () => {
  599. setData( model, modelTable( [
  600. [ '00[]', '01' ],
  601. [ '10', '11' ]
  602. ] ) );
  603. expect( command.isEnabled ).to.be.false;
  604. } );
  605. it( 'should be true if in a cell that has mergeable cell with the same colspan', () => {
  606. setData( model, modelTable( [
  607. [ { colspan: 2, contents: '00' }, '02' ],
  608. [ { colspan: 2, contents: '01[]' }, '12' ]
  609. ] ) );
  610. expect( command.isEnabled ).to.be.true;
  611. } );
  612. it( 'should be false if in a cell that potential mergeable cell has different colspan', () => {
  613. setData( model, modelTable( [
  614. [ { colspan: 2, contents: '00' }, '02' ],
  615. [ { colspan: 3, contents: '01[]' } ]
  616. ] ) );
  617. expect( command.isEnabled ).to.be.false;
  618. } );
  619. it( 'should be false if not in a cell', () => {
  620. setData( model, '<paragraph>11[]</paragraph>' );
  621. expect( command.isEnabled ).to.be.false;
  622. } );
  623. it( 'should be false if mergeable cell is in other table section then current cell', () => {
  624. setData( model, modelTable( [
  625. [ '00', '01' ],
  626. [ '10[]', '11' ]
  627. ], { headingRows: 1 } ) );
  628. expect( command.isEnabled ).to.be.false;
  629. } );
  630. } );
  631. describe( 'value', () => {
  632. it( 'should be set to mergeable cell', () => {
  633. setData( model, modelTable( [
  634. [ '00', '01' ],
  635. [ '10', '11[]' ]
  636. ] ) );
  637. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 1 ] ) );
  638. } );
  639. it( 'should be set to mergeable cell (selection in block content)', () => {
  640. setData( model, modelTable( [
  641. [ '00' ],
  642. [ '<paragraph>10[]</paragraph>' ],
  643. [ '20' ]
  644. ] ) );
  645. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 0 ] ) );
  646. } );
  647. it( 'should be undefined if in first row', () => {
  648. setData( model, modelTable( [
  649. [ '00[]', '01' ],
  650. [ '10', '11' ]
  651. ] ) );
  652. expect( command.value ).to.be.undefined;
  653. } );
  654. it( 'should be set to mergeable cell with the same rowspan', () => {
  655. setData( model, modelTable( [
  656. [ { colspan: 2, contents: '00' }, '02' ],
  657. [ { colspan: 2, contents: '01[]' }, '12' ]
  658. ] ) );
  659. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 0 ] ) );
  660. } );
  661. it( 'should be set to mergeable cell in rows with spanned cells', () => {
  662. setData( model, modelTable( [
  663. [ { rowspan: 3, contents: '00' }, '11', '12', '13' ],
  664. [ { rowspan: 2, contents: '21' }, '22', '23' ],
  665. [ '32', { rowspan: 2, contents: '33[]' } ],
  666. [ { colspan: 2, contents: '40' }, '42' ]
  667. ] ) );
  668. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 1, 2 ] ) );
  669. } );
  670. it( 'should be undefined if in a cell that potential mergeable cell has different rowspan', () => {
  671. setData( model, modelTable( [
  672. [ { colspan: 2, contents: '00' }, '02' ],
  673. [ { colspan: 3, contents: '01[]' } ]
  674. ] ) );
  675. expect( command.value ).to.be.undefined;
  676. } );
  677. it( 'should be undefined if not in a cell', () => {
  678. setData( model, '<paragraph>11[]</paragraph>' );
  679. expect( command.value ).to.be.undefined;
  680. } );
  681. } );
  682. describe( 'execute()', () => {
  683. it( 'should merge table cells', () => {
  684. setData( model, modelTable( [
  685. [ '00', '01' ],
  686. [ '10', '[]11' ]
  687. ] ) );
  688. command.execute();
  689. assertEqualMarkup( getData( model ), modelTable( [
  690. [ '00', { rowspan: 2, contents: '<paragraph>[01</paragraph><paragraph>11]</paragraph>' } ],
  691. [ '10' ]
  692. ] ) );
  693. } );
  694. it( 'should result in single empty paragraph if both cells are empty', () => {
  695. setData( model, modelTable( [
  696. [ '', '' ],
  697. [ '[]', '' ]
  698. ] ) );
  699. command.execute();
  700. assertEqualMarkup( getData( model ), modelTable( [
  701. [ { rowspan: 2, contents: '[]' }, '' ],
  702. [ '' ]
  703. ] ) );
  704. } );
  705. it( 'should result in single paragraph (other cell is empty)', () => {
  706. setData( model, modelTable( [
  707. [ '', '' ],
  708. [ 'foo[]', '' ]
  709. ] ) );
  710. command.execute();
  711. assertEqualMarkup( getData( model ), modelTable( [
  712. [ { rowspan: 2, contents: '[foo]' }, '' ],
  713. [ '' ]
  714. ] ) );
  715. } );
  716. it( 'should result in single paragraph (selection cell is empty)', () => {
  717. setData( model, modelTable( [
  718. [ 'foo', '' ],
  719. [ '[]', '' ]
  720. ] ) );
  721. command.execute();
  722. assertEqualMarkup( getData( model ), modelTable( [
  723. [ { rowspan: 2, contents: '[foo]' }, '' ],
  724. [ '' ]
  725. ] ) );
  726. } );
  727. it( 'should not merge other empty blocks to single block', () => {
  728. model.schema.register( 'block', {
  729. allowWhere: '$block',
  730. allowContentOf: '$block',
  731. isBlock: true
  732. } );
  733. setData( model, modelTable( [
  734. [ '<block></block>', '' ],
  735. [ '<block>[]</block>', '' ]
  736. ] ) );
  737. command.execute();
  738. assertEqualMarkup( getData( model ), modelTable( [
  739. [ { rowspan: 2, contents: '<block>[</block><block>]</block>' }, '' ],
  740. [ '' ]
  741. ] ) );
  742. } );
  743. it( 'should properly merge cells in rows with spaned cells', () => {
  744. setData( model, modelTable( [
  745. [ { rowspan: 3, contents: '00' }, '11', '12', '13' ],
  746. [ { rowspan: 2, contents: '21' }, '22', '23' ],
  747. [ '32', { rowspan: 2, contents: '33[]' } ],
  748. [ { colspan: 2, contents: '40' }, '42' ]
  749. ] ) );
  750. command.execute();
  751. assertEqualMarkup( getData( model ), modelTable( [
  752. [ { rowspan: 3, contents: '00' }, '11', '12', '13' ],
  753. [
  754. { rowspan: 2, contents: '21' },
  755. '22',
  756. { rowspan: 3, contents: '<paragraph>[23</paragraph><paragraph>33]</paragraph>' }
  757. ],
  758. [ '32' ],
  759. [ { colspan: 2, contents: '40' }, '42' ]
  760. ] ) );
  761. } );
  762. it( 'should remove empty row if merging table cells ', () => {
  763. setData( model, modelTable( [
  764. [ { rowspan: 2, contents: '00' }, '01', { rowspan: 3, contents: '02' } ],
  765. [ '11[]' ],
  766. [ '20', '21' ]
  767. ] ) );
  768. command.execute();
  769. assertEqualMarkup( getData( model ), modelTable( [
  770. [ '00', '<paragraph>[01</paragraph><paragraph>11]</paragraph>', { rowspan: 2, contents: '02' } ],
  771. [ '20', '21' ]
  772. ] ) );
  773. } );
  774. it( 'should not reduce rowspan on cells above removed empty row when merging table cells ', () => {
  775. setData( model, modelTable( [
  776. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  777. [ '11', '12' ],
  778. [ { rowspan: 2, contents: '20' }, '21', { rowspan: 3, contents: '22' } ],
  779. [ '31[]' ],
  780. [ '40', '41' ]
  781. ] ) );
  782. command.execute();
  783. assertEqualMarkup( getData( model ), modelTable( [
  784. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  785. [ '11', '12' ],
  786. [ '20', '<paragraph>[21</paragraph><paragraph>31]</paragraph>', { rowspan: 2, contents: '22' } ],
  787. [ '40', '41' ]
  788. ] ) );
  789. } );
  790. it( 'should adjust heading rows if empty row was removed ', () => {
  791. // +----+----+
  792. // | 00 | 01 |
  793. // + +----+
  794. // | | 11 |
  795. // +----+----+ <-- heading rows
  796. // | 20 | 21 |
  797. // +----+----+
  798. setData( model, modelTable( [
  799. [ { contents: '00', rowspan: 2 }, '01' ],
  800. [ '[]11' ],
  801. [ '20', '21' ]
  802. ], { headingRows: 2 } ) );
  803. command.execute();
  804. assertEqualMarkup( getData( model ), modelTable( [
  805. [ '00', '<paragraph>[01</paragraph><paragraph>11]</paragraph>' ],
  806. [ '20', '21' ]
  807. ], { headingRows: 1 } ) );
  808. } );
  809. it( 'should create one undo step (1 batch)', () => {
  810. // +----+----+
  811. // | 00 | 01 |
  812. // + +----+
  813. // | | 11 |
  814. // +----+----+ <-- heading rows
  815. // | 20 | 21 |
  816. // +----+----+
  817. setData( model, modelTable( [
  818. [ { contents: '00', rowspan: 2 }, '01' ],
  819. [ '[]11' ],
  820. [ '20', '21' ]
  821. ], { headingRows: 2 } ) );
  822. const createdBatches = new Set();
  823. model.on( 'applyOperation', ( evt, [ operation ] ) => {
  824. createdBatches.add( operation.batch );
  825. } );
  826. command.execute();
  827. expect( createdBatches.size ).to.equal( 1 );
  828. } );
  829. } );
  830. } );
  831. } );