mergecellscommand.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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 MergeCellsCommand from '../../src/commands/mergecellscommand';
  8. import { modelTable } from '../_utils/utils';
  9. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  10. import TableSelection from '../../src/tableselection';
  11. import TableEditing from '../../src/tableediting';
  12. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  13. describe( 'MergeCellsCommand', () => {
  14. let editor, model, command, root, tableSelection;
  15. beforeEach( async () => {
  16. editor = await ModelTestEditor.create( {
  17. plugins: [ Paragraph, TableEditing, TableSelection ]
  18. } );
  19. model = editor.model;
  20. root = model.document.getRoot( 'main' );
  21. tableSelection = editor.plugins.get( TableSelection );
  22. command = new MergeCellsCommand( editor );
  23. } );
  24. afterEach( () => {
  25. return editor.destroy();
  26. } );
  27. describe( 'isEnabled', () => {
  28. it( 'should be false if collapsed selection in table cell', () => {
  29. setData( model, modelTable( [
  30. [ '00[]', '01' ]
  31. ] ) );
  32. expect( command.isEnabled ).to.be.false;
  33. } );
  34. it( 'should be false if only one table cell is selected', () => {
  35. setData( model, modelTable( [
  36. [ '00', '01' ]
  37. ] ) );
  38. selectNodes( [ [ 0, 0, 0 ] ] );
  39. expect( command.isEnabled ).to.be.false;
  40. } );
  41. it( 'should be true if at least two adjacent table cells are selected', () => {
  42. setData( model, modelTable( [
  43. [ '00', '01' ]
  44. ] ) );
  45. selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 1 ] ] );
  46. expect( command.isEnabled ).to.be.true;
  47. } );
  48. it( 'should be true if many table cells are selected', () => {
  49. setData( model, modelTable( [
  50. [ '00', '01', '02', '03' ],
  51. [ '10', '11', '12', '13' ],
  52. [ '20', '21', '22', '23' ],
  53. [ '30', '31', '32', '33' ]
  54. ] ) );
  55. selectNodes( [
  56. [ 0, 0, 1 ], [ 0, 0, 2 ],
  57. [ 0, 1, 1 ], [ 0, 1, 2 ],
  58. [ 0, 2, 1 ], [ 0, 2, 2 ],
  59. [ 0, 3, 1 ], [ 0, 3, 2 ]
  60. ] );
  61. expect( command.isEnabled ).to.be.true;
  62. } );
  63. it( 'should be false if at least one table cell is not selected from an area', () => {
  64. setData( model, modelTable( [
  65. [ '00', '01', '02', '03' ],
  66. [ '10', '11', '12', '13' ],
  67. [ '20', '21', '22', '23' ],
  68. [ '30', '31', '32', '33' ]
  69. ] ) );
  70. selectNodes( [
  71. [ 0, 0, 1 ], [ 0, 0, 2 ],
  72. [ 0, 1, 2 ], // one table cell not selected from this row
  73. [ 0, 2, 1 ], [ 0, 2, 2 ],
  74. [ 0, 3, 1 ], [ 0, 3, 2 ]
  75. ] );
  76. expect( command.isEnabled ).to.be.false;
  77. } );
  78. it( 'should be false if table cells are not in adjacent rows', () => {
  79. setData( model, modelTable( [
  80. [ '00', '01' ],
  81. [ '10', '11' ]
  82. ] ) );
  83. selectNodes( [
  84. [ 0, 1, 0 ],
  85. [ 0, 0, 1 ]
  86. ] );
  87. expect( command.isEnabled ).to.be.false;
  88. } );
  89. it( 'should be false if table cells are not in adjacent columns', () => {
  90. setData( model, modelTable( [
  91. [ '00', '01', '02' ]
  92. ] ) );
  93. selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 2 ] ] );
  94. expect( command.isEnabled ).to.be.false;
  95. } );
  96. it( 'should be false if any table cell with colspan attribute extends over selection area', () => {
  97. setData( model, modelTable( [
  98. [ '00', { colspan: 2, contents: '01' } ],
  99. [ '10', '11', '12' ]
  100. ] ) );
  101. selectNodes( [
  102. [ 0, 0, 0 ], [ 0, 0, 1 ],
  103. [ 0, 1, 0 ], [ 0, 1, 1 ]
  104. ] );
  105. expect( command.isEnabled ).to.be.false;
  106. } );
  107. it( 'should be true if none table cell with colspan attribute extends over selection area', () => {
  108. setData( model, modelTable( [
  109. [ '00', { colspan: 2, contents: '01' } ],
  110. [ '10', '11', '12' ]
  111. ] ) );
  112. selectNodes( [
  113. [ 0, 0, 0 ], [ 0, 0, 1 ],
  114. [ 0, 1, 0 ], [ 0, 1, 1 ],
  115. [ 0, 1, 2 ]
  116. ] );
  117. expect( command.isEnabled ).to.be.true;
  118. } );
  119. it( 'should be true if first table cell is inside selection area', () => {
  120. setData( model, modelTable( [
  121. [ { colspan: 2, rowspan: 2, contents: '00' }, '02', '03' ],
  122. [ '12', '13' ]
  123. ] ) );
  124. selectNodes( [
  125. [ 0, 0, 0 ], [ 0, 0, 1 ],
  126. [ 0, 1, 0 ]
  127. ] );
  128. expect( command.isEnabled ).to.be.true;
  129. } );
  130. it( 'should be false if any table cell with rowspan attribute extends over selection area', () => {
  131. setData( model, modelTable( [
  132. [ '00', { rowspan: 2, contents: '01' } ],
  133. [ '10' ]
  134. ] ) );
  135. selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 1 ] ] );
  136. expect( command.isEnabled ).to.be.false;
  137. } );
  138. it( 'should be true if none table cell with rowspan attribute extends over selection area', () => {
  139. setData( model, modelTable( [
  140. [ '00', { rowspan: 2, contents: '01' } ],
  141. [ '10' ]
  142. ] ) );
  143. selectNodes( [
  144. [ 0, 0, 0 ], [ 0, 0, 1 ],
  145. [ 0, 1, 0 ]
  146. ] );
  147. expect( command.isEnabled ).to.be.true;
  148. } );
  149. it( 'should be false if not in a cell', () => {
  150. setData( model, '<paragraph>11[]</paragraph>' );
  151. expect( command.isEnabled ).to.be.false;
  152. } );
  153. it( 'should be false if selection has cells from header and body sections', () => {
  154. setData( model, modelTable( [
  155. [ '00[]', '01' ],
  156. [ '10', '11' ]
  157. ], { headingRows: 1 } ) );
  158. tableSelection.setCellSelection(
  159. root.getNodeByPath( [ 0, 0, 0 ] ),
  160. root.getNodeByPath( [ 0, 1, 0 ] )
  161. );
  162. expect( command.isEnabled ).to.be.false;
  163. } );
  164. it( 'should be false if more than 10 rows selected and some are in heading section', () => {
  165. setData( model, modelTable( [
  166. [ '0' ],
  167. [ '1' ],
  168. [ '2' ],
  169. [ '3' ],
  170. [ '4' ],
  171. [ '5' ],
  172. [ '6' ],
  173. [ '7' ],
  174. [ '8' ],
  175. [ '9' ],
  176. [ '10' ],
  177. [ '11' ],
  178. [ '12' ],
  179. [ '13' ],
  180. [ '14' ]
  181. ], { headingRows: 10 } ) );
  182. const tableSelection = editor.plugins.get( TableSelection );
  183. const modelRoot = model.document.getRoot();
  184. tableSelection.setCellSelection(
  185. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  186. modelRoot.getNodeByPath( [ 0, 12, 0 ] )
  187. );
  188. expect( command.isEnabled ).to.be.false;
  189. } );
  190. it( 'should be true if selection has cells only from column headers - rows in body section', () => {
  191. setData( model, modelTable( [
  192. [ '00[]', '01', '02', '03' ],
  193. [ '10', '11', '12', '13' ]
  194. ], { headingColumns: 2 } ) );
  195. tableSelection.setCellSelection(
  196. root.getNodeByPath( [ 0, 0, 0 ] ),
  197. root.getNodeByPath( [ 0, 1, 1 ] )
  198. );
  199. expect( command.isEnabled ).to.be.true;
  200. } );
  201. it( 'should be false if selection has cells from column headers and other cells - rows in body section', () => {
  202. setData( model, modelTable( [
  203. [ '00[]', '01', '02', '03' ],
  204. [ '10', '11', '12', '13' ]
  205. ], { headingColumns: 2 } ) );
  206. tableSelection.setCellSelection(
  207. root.getNodeByPath( [ 0, 0, 0 ] ),
  208. root.getNodeByPath( [ 0, 1, 2 ] )
  209. );
  210. expect( command.isEnabled ).to.be.false;
  211. } );
  212. it( 'should be true if selection has cells only from column headers - rows in header section', () => {
  213. setData( model, modelTable( [
  214. [ '00[]', '01', '02', '03' ],
  215. [ '10', '11', '12', '13' ]
  216. ], { headingColumns: 2, headingRows: 1 } ) );
  217. tableSelection.setCellSelection(
  218. root.getNodeByPath( [ 0, 0, 0 ] ),
  219. root.getNodeByPath( [ 0, 0, 1 ] )
  220. );
  221. expect( command.isEnabled ).to.be.true;
  222. } );
  223. it( 'should be false if selection has cells only from column headers and other cells - rows in header section', () => {
  224. setData( model, modelTable( [
  225. [ '00[]', '01', '02', '03' ],
  226. [ '10', '11', '12', '13' ]
  227. ], { headingColumns: 2, headingRows: 1 } ) );
  228. tableSelection.setCellSelection(
  229. root.getNodeByPath( [ 0, 0, 0 ] ),
  230. root.getNodeByPath( [ 0, 0, 2 ] )
  231. );
  232. expect( command.isEnabled ).to.be.false;
  233. } );
  234. it( 'should be false if selection has cells from column headers, row headers and body sections', () => {
  235. setData( model, modelTable( [
  236. [ '00[]', '01', '02', '03' ],
  237. [ '10', '11', '12', '13' ]
  238. ], { headingColumns: 2, headingRows: 1 } ) );
  239. tableSelection.setCellSelection(
  240. root.getNodeByPath( [ 0, 0, 0 ] ),
  241. root.getNodeByPath( [ 0, 1, 2 ] )
  242. );
  243. expect( command.isEnabled ).to.be.false;
  244. } );
  245. } );
  246. describe( 'execute()', () => {
  247. it( 'should merge simple table cell selection', () => {
  248. setData( model, modelTable( [
  249. [ '[]00', '01' ]
  250. ] ) );
  251. tableSelection.setCellSelection(
  252. root.getNodeByPath( [ 0, 0, 0 ] ),
  253. root.getNodeByPath( [ 0, 0, 1 ] )
  254. );
  255. command.execute();
  256. assertEqualMarkup( getData( model ), modelTable( [
  257. [ { colspan: 2, contents: '<paragraph>[00</paragraph><paragraph>01]</paragraph>' } ]
  258. ] ) );
  259. } );
  260. it( 'should merge selection with a cell with rowspan in the selection', () => {
  261. setData( model, modelTable( [
  262. [ '[]00', '01', '02' ],
  263. [ '10', { contents: '11', rowspan: 2 }, '12' ],
  264. [ '20', '22' ]
  265. ] ) );
  266. tableSelection.setCellSelection(
  267. root.getNodeByPath( [ 0, 1, 0 ] ),
  268. root.getNodeByPath( [ 0, 2, 1 ] )
  269. );
  270. command.execute();
  271. assertEqualMarkup( getData( model ), modelTable( [
  272. [ '00', '01', '02' ],
  273. [ {
  274. colspan: 3,
  275. contents: '<paragraph>[10</paragraph><paragraph>11</paragraph><paragraph>12</paragraph>' +
  276. '<paragraph>20</paragraph><paragraph>22]</paragraph>'
  277. } ]
  278. ] ) );
  279. } );
  280. it( 'should merge selection with a cell with rowspan in the selection (reverse selection)', () => {
  281. setData( model, modelTable( [
  282. [ '[]00', '01', '02' ],
  283. [ '10', { contents: '11', rowspan: 2 }, '12' ],
  284. [ '20', '22' ]
  285. ] ) );
  286. tableSelection.setCellSelection(
  287. root.getNodeByPath( [ 0, 2, 1 ] ),
  288. root.getNodeByPath( [ 0, 1, 0 ] )
  289. );
  290. command.execute();
  291. assertEqualMarkup( getData( model ), modelTable( [
  292. [ '00', '01', '02' ],
  293. [ {
  294. colspan: 3,
  295. contents: '<paragraph>[10</paragraph><paragraph>11</paragraph><paragraph>12</paragraph>' +
  296. '<paragraph>20</paragraph><paragraph>22]</paragraph>'
  297. } ]
  298. ] ) );
  299. } );
  300. it( 'should merge selection inside a table (properly calculate target rowspan/colspan)', () => {
  301. setData( model, modelTable( [
  302. [ '[]00', '01', '02', '03' ],
  303. [ '10', '11', { contents: '12', rowspan: 2 }, '13' ],
  304. [ '20', '21', '23' ],
  305. [ '30', '31', '32', '33' ]
  306. ] ) );
  307. tableSelection.setCellSelection(
  308. root.getNodeByPath( [ 0, 2, 1 ] ),
  309. root.getNodeByPath( [ 0, 1, 2 ] )
  310. );
  311. command.execute();
  312. assertEqualMarkup( getData( model ), modelTable( [
  313. [ '00', '01', '02', '03' ],
  314. [ '10', {
  315. colspan: 2,
  316. rowspan: 2,
  317. contents: '<paragraph>[11</paragraph><paragraph>12</paragraph><paragraph>21]</paragraph>'
  318. }, '13' ],
  319. [ '20', '23' ],
  320. [ '30', '31', '32', '33' ]
  321. ] ) );
  322. } );
  323. it( 'should merge table cells - extend colspan attribute', () => {
  324. setData( model, modelTable( [
  325. [ { colspan: 2, contents: '00' }, '02', '03' ],
  326. [ '10', '11', '12', '13' ]
  327. ] ) );
  328. selectNodes( [
  329. [ 0, 0, 0 ], [ 0, 0, 1 ],
  330. [ 0, 1, 0 ], [ 0, 1, 1 ], [ 0, 1, 2 ]
  331. ] );
  332. command.execute();
  333. assertEqualMarkup( getData( model ), modelTable( [
  334. [ {
  335. colspan: 3,
  336. rowspan: 2,
  337. contents: '<paragraph>[00</paragraph>' +
  338. '<paragraph>02</paragraph>' +
  339. '<paragraph>10</paragraph>' +
  340. '<paragraph>11</paragraph>' +
  341. '<paragraph>12]</paragraph>'
  342. }, '03' ],
  343. [ '13' ]
  344. ] ) );
  345. } );
  346. it( 'should merge to a single paragraph - every cell is empty', () => {
  347. setData( model, modelTable( [
  348. [ '[]', '' ]
  349. ] ) );
  350. selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 1 ] ] );
  351. command.execute();
  352. assertEqualMarkup( getData( model ), modelTable( [
  353. [ { colspan: 2, contents: '<paragraph>[]</paragraph>' } ]
  354. ] ) );
  355. } );
  356. it( 'should merge to a single paragraph - merged cell is empty', () => {
  357. setData( model, modelTable( [
  358. [ 'foo', '' ]
  359. ] ) );
  360. selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 1 ] ] );
  361. command.execute();
  362. assertEqualMarkup( getData( model ), modelTable( [
  363. [ { colspan: 2, contents: '<paragraph>[foo]</paragraph>' } ]
  364. ] ) );
  365. } );
  366. it( 'should merge to a single paragraph - cell to which others are merged is empty', () => {
  367. setData( model, modelTable( [
  368. [ '', 'foo' ]
  369. ] ) );
  370. selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 1 ] ] );
  371. command.execute();
  372. assertEqualMarkup( getData( model ), modelTable( [
  373. [ { colspan: 2, contents: '<paragraph>[foo]</paragraph>' } ]
  374. ] ) );
  375. } );
  376. it( 'should not merge empty blocks other then <paragraph> to a single block', () => {
  377. model.schema.register( 'block', {
  378. allowWhere: '$block',
  379. allowContentOf: '$block',
  380. isBlock: true
  381. } );
  382. setData( model, modelTable( [
  383. [ '<block>[]</block>', '<block></block>' ]
  384. ] ) );
  385. selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 1 ] ] );
  386. command.execute();
  387. assertEqualMarkup( getData( model ), modelTable( [
  388. [ { colspan: 2, contents: '<block>[</block><block>]</block>' } ]
  389. ] ) );
  390. } );
  391. describe( 'removing empty row', () => {
  392. it( 'should remove empty row if merging all table cells from that row', () => {
  393. setData( model, modelTable( [
  394. [ '00' ],
  395. [ '10' ],
  396. [ '20' ]
  397. ] ) );
  398. selectNodes( [
  399. [ 0, 0, 0 ],
  400. [ 0, 1, 0 ],
  401. [ 0, 2, 0 ]
  402. ] );
  403. command.execute();
  404. assertEqualMarkup( getData( model ), modelTable( [
  405. [
  406. '<paragraph>[00</paragraph><paragraph>10</paragraph><paragraph>20]</paragraph>'
  407. ]
  408. ] ) );
  409. } );
  410. it( 'should decrease heading rows if some heading rows were removed', () => {
  411. setData( model, modelTable( [
  412. [ '00' ],
  413. [ '10' ],
  414. [ '20' ]
  415. ], { headingRows: 2 } ) );
  416. selectNodes( [
  417. [ 0, 0, 0 ],
  418. [ 0, 1, 0 ]
  419. ] );
  420. command.execute();
  421. assertEqualMarkup( getData( model ), modelTable( [
  422. [
  423. '<paragraph>[00</paragraph><paragraph>10]</paragraph>'
  424. ],
  425. [ '20' ]
  426. ], { headingRows: 1 } ) );
  427. } );
  428. it( 'should create one undo step (1 batch)', () => {
  429. setData( model, modelTable( [
  430. [ '00' ],
  431. [ '10' ],
  432. [ '20' ]
  433. ], { headingRows: 2 } ) );
  434. selectNodes( [
  435. [ 0, 0, 0 ],
  436. [ 0, 1, 0 ]
  437. ] );
  438. const createdBatches = new Set();
  439. model.on( 'applyOperation', ( evt, [ operation ] ) => {
  440. createdBatches.add( operation.batch );
  441. } );
  442. command.execute();
  443. expect( createdBatches.size ).to.equal( 1 );
  444. } );
  445. it( 'should decrease rowspan if cell overlaps removed row', () => {
  446. setData( model, modelTable( [
  447. [ '00', { rowspan: 2, contents: '01' }, { rowspan: 3, contents: '02' } ],
  448. [ '10' ],
  449. [ '20', '21' ]
  450. ] ) );
  451. selectNodes( [
  452. [ 0, 0, 0 ],
  453. [ 0, 1, 0 ],
  454. [ 0, 2, 0 ]
  455. ] );
  456. command.execute();
  457. assertEqualMarkup( getData( model ), modelTable( [
  458. [
  459. { rowspan: 2, contents: '<paragraph>[00</paragraph><paragraph>10</paragraph><paragraph>20]</paragraph>' },
  460. '01',
  461. { rowspan: 2, contents: '02' }
  462. ],
  463. [ '21' ]
  464. ] ) );
  465. } );
  466. it( 'should not decrease rowspan if cell from previous row does not overlaps removed row', () => {
  467. setData( model, modelTable( [
  468. [ '00', { rowspan: 2, contents: '01' } ],
  469. [ '10' ],
  470. [ '20', '21' ],
  471. [ '30', '31' ]
  472. ] ) );
  473. selectNodes( [
  474. [ 0, 2, 0 ], [ 0, 2, 1 ],
  475. [ 0, 3, 0 ], [ 0, 3, 1 ]
  476. ] );
  477. command.execute();
  478. assertEqualMarkup( getData( model ), modelTable( [
  479. [ '00', { rowspan: 2, contents: '01' } ],
  480. [ '10' ],
  481. [
  482. {
  483. colspan: 2,
  484. contents: '<paragraph>[20</paragraph><paragraph>21</paragraph>' +
  485. '<paragraph>30</paragraph><paragraph>31]</paragraph>'
  486. }
  487. ]
  488. ] ) );
  489. } );
  490. } );
  491. } );
  492. function selectNodes( paths ) {
  493. model.change( writer => {
  494. const ranges = paths.map( path => writer.createRangeOn( root.getNodeByPath( path ) ) );
  495. writer.setSelection( ranges );
  496. } );
  497. }
  498. } );