mergecellscommand.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. } );
  235. describe( 'execute()', () => {
  236. it( 'should merge simple table cell selection', () => {
  237. setData( model, modelTable( [
  238. [ '[]00', '01' ]
  239. ] ) );
  240. tableSelection._setCellSelection(
  241. root.getNodeByPath( [ 0, 0, 0 ] ),
  242. root.getNodeByPath( [ 0, 0, 1 ] )
  243. );
  244. command.execute();
  245. assertEqualMarkup( getData( model ), modelTable( [
  246. [ { colspan: 2, contents: '<paragraph>[00</paragraph><paragraph>01]</paragraph>' } ]
  247. ] ) );
  248. } );
  249. it( 'should merge selection with a cell with rowspan in the selection', () => {
  250. setData( model, modelTable( [
  251. [ '[]00', '01', '02' ],
  252. [ '10', { contents: '11', rowspan: 2 }, '12' ],
  253. [ '20', '22' ]
  254. ] ) );
  255. tableSelection._setCellSelection(
  256. root.getNodeByPath( [ 0, 1, 0 ] ),
  257. root.getNodeByPath( [ 0, 2, 1 ] )
  258. );
  259. command.execute();
  260. assertEqualMarkup( getData( model ), modelTable( [
  261. [ '00', '01', '02' ],
  262. [ {
  263. colspan: 3,
  264. contents: '<paragraph>[10</paragraph><paragraph>11</paragraph><paragraph>12</paragraph>' +
  265. '<paragraph>20</paragraph><paragraph>22]</paragraph>'
  266. } ]
  267. ] ) );
  268. } );
  269. it( 'should merge selection with a cell with rowspan in the selection (reverse selection)', () => {
  270. setData( model, modelTable( [
  271. [ '[]00', '01', '02' ],
  272. [ '10', { contents: '11', rowspan: 2 }, '12' ],
  273. [ '20', '22' ]
  274. ] ) );
  275. tableSelection._setCellSelection(
  276. root.getNodeByPath( [ 0, 2, 1 ] ),
  277. root.getNodeByPath( [ 0, 1, 0 ] )
  278. );
  279. command.execute();
  280. assertEqualMarkup( getData( model ), modelTable( [
  281. [ '00', '01', '02' ],
  282. [ {
  283. colspan: 3,
  284. contents: '<paragraph>[10</paragraph><paragraph>11</paragraph><paragraph>12</paragraph>' +
  285. '<paragraph>20</paragraph><paragraph>22]</paragraph>'
  286. } ]
  287. ] ) );
  288. } );
  289. it( 'should merge selection inside a table (properly calculate target rowspan/colspan)', () => {
  290. setData( model, modelTable( [
  291. [ '[]00', '01', '02', '03' ],
  292. [ '10', '11', { contents: '12', rowspan: 2 }, '13' ],
  293. [ '20', '21', '23' ],
  294. [ '30', '31', '32', '33' ]
  295. ] ) );
  296. tableSelection._setCellSelection(
  297. root.getNodeByPath( [ 0, 2, 1 ] ),
  298. root.getNodeByPath( [ 0, 1, 2 ] )
  299. );
  300. command.execute();
  301. assertEqualMarkup( getData( model ), modelTable( [
  302. [ '00', '01', '02', '03' ],
  303. [ '10', {
  304. colspan: 2,
  305. rowspan: 2,
  306. contents: '<paragraph>[11</paragraph><paragraph>12</paragraph><paragraph>21]</paragraph>'
  307. }, '13' ],
  308. [ '20', '23' ],
  309. [ '30', '31', '32', '33' ]
  310. ] ) );
  311. } );
  312. it( 'should merge table cells - extend colspan attribute', () => {
  313. setData( model, modelTable( [
  314. [ { colspan: 2, contents: '00' }, '02', '03' ],
  315. [ '10', '11', '12', '13' ]
  316. ] ) );
  317. selectNodes( [
  318. [ 0, 0, 0 ], [ 0, 0, 1 ],
  319. [ 0, 1, 0 ], [ 0, 1, 1 ], [ 0, 1, 2 ]
  320. ] );
  321. command.execute();
  322. assertEqualMarkup( getData( model ), modelTable( [
  323. [ {
  324. colspan: 3,
  325. rowspan: 2,
  326. contents: '<paragraph>[00</paragraph>' +
  327. '<paragraph>02</paragraph>' +
  328. '<paragraph>10</paragraph>' +
  329. '<paragraph>11</paragraph>' +
  330. '<paragraph>12]</paragraph>'
  331. }, '03' ],
  332. [ '13' ]
  333. ] ) );
  334. } );
  335. it( 'should merge to a single paragraph - every cell is empty', () => {
  336. setData( model, modelTable( [
  337. [ '[]', '' ]
  338. ] ) );
  339. selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 1 ] ] );
  340. command.execute();
  341. assertEqualMarkup( getData( model ), modelTable( [
  342. [ { colspan: 2, contents: '<paragraph>[]</paragraph>' } ]
  343. ] ) );
  344. } );
  345. it( 'should merge to a single paragraph - merged cell is empty', () => {
  346. setData( model, modelTable( [
  347. [ 'foo', '' ]
  348. ] ) );
  349. selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 1 ] ] );
  350. command.execute();
  351. assertEqualMarkup( getData( model ), modelTable( [
  352. [ { colspan: 2, contents: '<paragraph>[foo]</paragraph>' } ]
  353. ] ) );
  354. } );
  355. it( 'should merge to a single paragraph - cell to which others are merged is empty', () => {
  356. setData( model, modelTable( [
  357. [ '', 'foo' ]
  358. ] ) );
  359. selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 1 ] ] );
  360. command.execute();
  361. assertEqualMarkup( getData( model ), modelTable( [
  362. [ { colspan: 2, contents: '<paragraph>[foo]</paragraph>' } ]
  363. ] ) );
  364. } );
  365. it( 'should not merge empty blocks other then <paragraph> to a single block', () => {
  366. model.schema.register( 'block', {
  367. allowWhere: '$block',
  368. allowContentOf: '$block',
  369. isBlock: true
  370. } );
  371. setData( model, modelTable( [
  372. [ '<block>[]</block>', '<block></block>' ]
  373. ] ) );
  374. selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 1 ] ] );
  375. command.execute();
  376. assertEqualMarkup( getData( model ), modelTable( [
  377. [ { colspan: 2, contents: '<block>[</block><block>]</block>' } ]
  378. ] ) );
  379. } );
  380. describe( 'removing empty row', () => {
  381. it( 'should remove empty row if merging all table cells from that row', () => {
  382. setData( model, modelTable( [
  383. [ '00' ],
  384. [ '10' ],
  385. [ '20' ]
  386. ] ) );
  387. selectNodes( [
  388. [ 0, 0, 0 ],
  389. [ 0, 1, 0 ],
  390. [ 0, 2, 0 ]
  391. ] );
  392. command.execute();
  393. assertEqualMarkup( getData( model ), modelTable( [
  394. [
  395. '<paragraph>[00</paragraph><paragraph>10</paragraph><paragraph>20]</paragraph>'
  396. ]
  397. ] ) );
  398. } );
  399. it( 'should decrease rowspan if cell overlaps removed row', () => {
  400. setData( model, modelTable( [
  401. [ '00', { rowspan: 2, contents: '01' }, { rowspan: 3, contents: '02' } ],
  402. [ '10' ],
  403. [ '20', '21' ]
  404. ] ) );
  405. selectNodes( [
  406. [ 0, 0, 0 ],
  407. [ 0, 1, 0 ],
  408. [ 0, 2, 0 ]
  409. ] );
  410. command.execute();
  411. assertEqualMarkup( getData( model ), modelTable( [
  412. [
  413. { rowspan: 2, contents: '<paragraph>[00</paragraph><paragraph>10</paragraph><paragraph>20]</paragraph>' },
  414. '01',
  415. { rowspan: 2, contents: '02' }
  416. ],
  417. [ '21' ]
  418. ] ) );
  419. } );
  420. it( 'should not decrease rowspan if cell from previous row does not overlaps removed row', () => {
  421. setData( model, modelTable( [
  422. [ '00', { rowspan: 2, contents: '01' } ],
  423. [ '10' ],
  424. [ '20', '21' ],
  425. [ '30', '31' ]
  426. ] ) );
  427. selectNodes( [
  428. [ 0, 2, 0 ], [ 0, 2, 1 ],
  429. [ 0, 3, 0 ], [ 0, 3, 1 ]
  430. ] );
  431. command.execute();
  432. assertEqualMarkup( getData( model ), modelTable( [
  433. [ '00', { rowspan: 2, contents: '01' } ],
  434. [ '10' ],
  435. [
  436. {
  437. colspan: 2,
  438. contents: '<paragraph>[20</paragraph><paragraph>21</paragraph>' +
  439. '<paragraph>30</paragraph><paragraph>31]</paragraph>'
  440. }
  441. ]
  442. ] ) );
  443. } );
  444. } );
  445. } );
  446. function selectNodes( paths ) {
  447. model.change( writer => {
  448. const ranges = paths.map( path => writer.createRangeOn( root.getNodeByPath( path ) ) );
  449. writer.setSelection( ranges );
  450. } );
  451. }
  452. } );