mergecellscommand.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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 false if selection has cells from column headers and other cells - rows in body section', () => {
  191. setData( model, modelTable( [
  192. [ '00[]', '01' ],
  193. [ '10', '11' ]
  194. ], { headingColumns: 1 } ) );
  195. tableSelection._setCellSelection(
  196. root.getNodeByPath( [ 0, 0, 0 ] ),
  197. root.getNodeByPath( [ 0, 0, 1 ] )
  198. );
  199. expect( command.isEnabled ).to.be.false;
  200. } );
  201. it( 'should be true if selection has cells from column headers and other cells - rows in header section', () => {
  202. setData( model, modelTable( [
  203. [ '00[]', '01' ],
  204. [ '10', '11' ]
  205. ], { headingColumns: 1, headingRows: 1 } ) );
  206. tableSelection._setCellSelection(
  207. root.getNodeByPath( [ 0, 0, 0 ] ),
  208. root.getNodeByPath( [ 0, 0, 1 ] )
  209. );
  210. expect( command.isEnabled ).to.be.true;
  211. } );
  212. } );
  213. describe( 'execute()', () => {
  214. it( 'should merge simple table cell selection', () => {
  215. setData( model, modelTable( [
  216. [ '[]00', '01' ]
  217. ] ) );
  218. tableSelection._setCellSelection(
  219. root.getNodeByPath( [ 0, 0, 0 ] ),
  220. root.getNodeByPath( [ 0, 0, 1 ] )
  221. );
  222. command.execute();
  223. assertEqualMarkup( getData( model ), modelTable( [
  224. [ { colspan: 2, contents: '<paragraph>[00</paragraph><paragraph>01]</paragraph>' } ]
  225. ] ) );
  226. } );
  227. it( 'should merge selection with a cell with rowspan in the selection', () => {
  228. setData( model, modelTable( [
  229. [ '[]00', '01', '02' ],
  230. [ '10', { contents: '11', rowspan: 2 }, '12' ],
  231. [ '20', '22' ]
  232. ] ) );
  233. tableSelection._setCellSelection(
  234. root.getNodeByPath( [ 0, 1, 0 ] ),
  235. root.getNodeByPath( [ 0, 2, 1 ] )
  236. );
  237. command.execute();
  238. assertEqualMarkup( getData( model ), modelTable( [
  239. [ '00', '01', '02' ],
  240. [ {
  241. colspan: 3,
  242. contents: '<paragraph>[10</paragraph><paragraph>11</paragraph><paragraph>12</paragraph>' +
  243. '<paragraph>20</paragraph><paragraph>22]</paragraph>'
  244. } ]
  245. ] ) );
  246. } );
  247. it( 'should merge selection with a cell with rowspan in the selection (reverse selection)', () => {
  248. setData( model, modelTable( [
  249. [ '[]00', '01', '02' ],
  250. [ '10', { contents: '11', rowspan: 2 }, '12' ],
  251. [ '20', '22' ]
  252. ] ) );
  253. tableSelection._setCellSelection(
  254. root.getNodeByPath( [ 0, 2, 1 ] ),
  255. root.getNodeByPath( [ 0, 1, 0 ] )
  256. );
  257. command.execute();
  258. assertEqualMarkup( getData( model ), modelTable( [
  259. [ '00', '01', '02' ],
  260. [ {
  261. colspan: 3,
  262. contents: '<paragraph>[10</paragraph><paragraph>11</paragraph><paragraph>12</paragraph>' +
  263. '<paragraph>20</paragraph><paragraph>22]</paragraph>'
  264. } ]
  265. ] ) );
  266. } );
  267. it( 'should merge selection inside a table (properly calculate target rowspan/colspan)', () => {
  268. setData( model, modelTable( [
  269. [ '[]00', '01', '02', '03' ],
  270. [ '10', '11', { contents: '12', rowspan: 2 }, '13' ],
  271. [ '20', '21', '23' ],
  272. [ '30', '31', '32', '33' ]
  273. ] ) );
  274. tableSelection._setCellSelection(
  275. root.getNodeByPath( [ 0, 2, 1 ] ),
  276. root.getNodeByPath( [ 0, 1, 2 ] )
  277. );
  278. command.execute();
  279. assertEqualMarkup( getData( model ), modelTable( [
  280. [ '00', '01', '02', '03' ],
  281. [ '10', {
  282. colspan: 2,
  283. rowspan: 2,
  284. contents: '<paragraph>[11</paragraph><paragraph>12</paragraph><paragraph>21]</paragraph>'
  285. }, '13' ],
  286. [ '20', '23' ],
  287. [ '30', '31', '32', '33' ]
  288. ] ) );
  289. } );
  290. it( 'should merge table cells - extend colspan attribute', () => {
  291. setData( model, modelTable( [
  292. [ { colspan: 2, contents: '00' }, '02', '03' ],
  293. [ '10', '11', '12', '13' ]
  294. ] ) );
  295. selectNodes( [
  296. [ 0, 0, 0 ], [ 0, 0, 1 ],
  297. [ 0, 1, 0 ], [ 0, 1, 1 ], [ 0, 1, 2 ]
  298. ] );
  299. command.execute();
  300. assertEqualMarkup( getData( model ), modelTable( [
  301. [ {
  302. colspan: 3,
  303. rowspan: 2,
  304. contents: '<paragraph>[00</paragraph>' +
  305. '<paragraph>02</paragraph>' +
  306. '<paragraph>10</paragraph>' +
  307. '<paragraph>11</paragraph>' +
  308. '<paragraph>12]</paragraph>'
  309. }, '03' ],
  310. [ '13' ]
  311. ] ) );
  312. } );
  313. it( 'should merge to a single paragraph - every cell is empty', () => {
  314. setData( model, modelTable( [
  315. [ '[]', '' ]
  316. ] ) );
  317. selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 1 ] ] );
  318. command.execute();
  319. assertEqualMarkup( getData( model ), modelTable( [
  320. [ { colspan: 2, contents: '<paragraph>[]</paragraph>' } ]
  321. ] ) );
  322. } );
  323. it( 'should merge to a single paragraph - merged cell is empty', () => {
  324. setData( model, modelTable( [
  325. [ 'foo', '' ]
  326. ] ) );
  327. selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 1 ] ] );
  328. command.execute();
  329. assertEqualMarkup( getData( model ), modelTable( [
  330. [ { colspan: 2, contents: '<paragraph>[foo]</paragraph>' } ]
  331. ] ) );
  332. } );
  333. it( 'should merge to a single paragraph - cell to which others are merged is empty', () => {
  334. setData( model, modelTable( [
  335. [ '', 'foo' ]
  336. ] ) );
  337. selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 1 ] ] );
  338. command.execute();
  339. assertEqualMarkup( getData( model ), modelTable( [
  340. [ { colspan: 2, contents: '<paragraph>[foo]</paragraph>' } ]
  341. ] ) );
  342. } );
  343. it( 'should not merge empty blocks other then <paragraph> to a single block', () => {
  344. model.schema.register( 'block', {
  345. allowWhere: '$block',
  346. allowContentOf: '$block',
  347. isBlock: true
  348. } );
  349. setData( model, modelTable( [
  350. [ '<block>[]</block>', '<block></block>' ]
  351. ] ) );
  352. selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 1 ] ] );
  353. command.execute();
  354. assertEqualMarkup( getData( model ), modelTable( [
  355. [ { colspan: 2, contents: '<block>[</block><block>]</block>' } ]
  356. ] ) );
  357. } );
  358. describe( 'removing empty row', () => {
  359. it( 'should remove empty row if merging all table cells from that row', () => {
  360. setData( model, modelTable( [
  361. [ '00' ],
  362. [ '10' ],
  363. [ '20' ]
  364. ] ) );
  365. selectNodes( [
  366. [ 0, 0, 0 ],
  367. [ 0, 1, 0 ],
  368. [ 0, 2, 0 ]
  369. ] );
  370. command.execute();
  371. assertEqualMarkup( getData( model ), modelTable( [
  372. [
  373. '<paragraph>[00</paragraph><paragraph>10</paragraph><paragraph>20]</paragraph>'
  374. ]
  375. ] ) );
  376. } );
  377. it( 'should decrease rowspan if cell overlaps removed row', () => {
  378. setData( model, modelTable( [
  379. [ '00', { rowspan: 2, contents: '01' }, { rowspan: 3, contents: '02' } ],
  380. [ '10' ],
  381. [ '20', '21' ]
  382. ] ) );
  383. selectNodes( [
  384. [ 0, 0, 0 ],
  385. [ 0, 1, 0 ],
  386. [ 0, 2, 0 ]
  387. ] );
  388. command.execute();
  389. assertEqualMarkup( getData( model ), modelTable( [
  390. [
  391. { rowspan: 2, contents: '<paragraph>[00</paragraph><paragraph>10</paragraph><paragraph>20]</paragraph>' },
  392. '01',
  393. { rowspan: 2, contents: '02' }
  394. ],
  395. [ '21' ]
  396. ] ) );
  397. } );
  398. it( 'should not decrease rowspan if cell from previous row does not overlaps removed row', () => {
  399. setData( model, modelTable( [
  400. [ '00', { rowspan: 2, contents: '01' } ],
  401. [ '10' ],
  402. [ '20', '21' ],
  403. [ '30', '31' ]
  404. ] ) );
  405. selectNodes( [
  406. [ 0, 2, 0 ], [ 0, 2, 1 ],
  407. [ 0, 3, 0 ], [ 0, 3, 1 ]
  408. ] );
  409. command.execute();
  410. assertEqualMarkup( getData( model ), modelTable( [
  411. [ '00', { rowspan: 2, contents: '01' } ],
  412. [ '10' ],
  413. [
  414. {
  415. colspan: 2,
  416. contents: '<paragraph>[20</paragraph><paragraph>21</paragraph>' +
  417. '<paragraph>30</paragraph><paragraph>31]</paragraph>'
  418. }
  419. ]
  420. ] ) );
  421. } );
  422. } );
  423. } );
  424. function selectNodes( paths ) {
  425. model.change( writer => {
  426. const ranges = paths.map( path => writer.createRangeOn( root.getNodeByPath( path ) ) );
  427. writer.setSelection( ranges );
  428. } );
  429. }
  430. } );