mergecellcommand.js 30 KB

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