mergecellscommand.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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 range is collapsed in selection', () => {
  97. setData( model, modelTable( [
  98. [ '00', '01', '02' ]
  99. ] ) );
  100. selectNodes( [
  101. [ 0, 0, 0, 0, 0 ], // The "00" text node
  102. [ 0, 0, 1 ]
  103. ] );
  104. expect( command.isEnabled ).to.be.false;
  105. } );
  106. it( 'should be false if any ranges are on different tables', () => {
  107. setData( model,
  108. modelTable( [ [ '00', '01' ] ] ) +
  109. modelTable( [ [ 'aa', 'ab' ] ] )
  110. );
  111. selectNodes( [
  112. [ 0, 0, 0 ], // first table
  113. [ 1, 0, 1 ] // second table
  114. ] );
  115. expect( command.isEnabled ).to.be.false;
  116. } );
  117. it( 'should be false if any table cell with colspan attribute extends over selection area', () => {
  118. setData( model, modelTable( [
  119. [ '00', { colspan: 2, contents: '01' } ],
  120. [ '10', '11', '12' ]
  121. ] ) );
  122. selectNodes( [
  123. [ 0, 0, 0 ], [ 0, 0, 1 ],
  124. [ 0, 1, 0 ], [ 0, 1, 1 ]
  125. ] );
  126. expect( command.isEnabled ).to.be.false;
  127. } );
  128. it( 'should be true if none table cell with colspan attribute extends over selection area', () => {
  129. setData( model, modelTable( [
  130. [ '00', { colspan: 2, contents: '01' } ],
  131. [ '10', '11', '12' ]
  132. ] ) );
  133. selectNodes( [
  134. [ 0, 0, 0 ], [ 0, 0, 1 ],
  135. [ 0, 1, 0 ], [ 0, 1, 1 ],
  136. [ 0, 1, 2 ]
  137. ] );
  138. expect( command.isEnabled ).to.be.true;
  139. } );
  140. it( 'should be true if first table cell is inside selection area', () => {
  141. setData( model, modelTable( [
  142. [ { colspan: 2, rowspan: 2, contents: '00' }, '02', '03' ],
  143. [ '12', '13' ]
  144. ] ) );
  145. selectNodes( [
  146. [ 0, 0, 0 ], [ 0, 0, 1 ],
  147. [ 0, 1, 0 ]
  148. ] );
  149. expect( command.isEnabled ).to.be.true;
  150. } );
  151. it( 'should be false if any table cell with rowspan attribute extends over selection area', () => {
  152. setData( model, modelTable( [
  153. [ '00', { rowspan: 2, contents: '01' } ],
  154. [ '10' ]
  155. ] ) );
  156. selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 1 ] ] );
  157. expect( command.isEnabled ).to.be.false;
  158. } );
  159. it( 'should be true if none table cell with rowspan attribute extends over selection area', () => {
  160. setData( model, modelTable( [
  161. [ '00', { rowspan: 2, contents: '01' } ],
  162. [ '10' ]
  163. ] ) );
  164. selectNodes( [
  165. [ 0, 0, 0 ], [ 0, 0, 1 ],
  166. [ 0, 1, 0 ]
  167. ] );
  168. expect( command.isEnabled ).to.be.true;
  169. } );
  170. it( 'should be false if not in a cell', () => {
  171. setData( model, '<paragraph>11[]</paragraph>' );
  172. expect( command.isEnabled ).to.be.false;
  173. } );
  174. it( 'should be false if selection has cells from header and body sections', () => {
  175. setData( model, modelTable( [
  176. [ '00[]', '01' ],
  177. [ '10', '11' ]
  178. ], { headingRows: 1 } ) );
  179. tableSelection._setCellSelection(
  180. root.getNodeByPath( [ 0, 0, 0 ] ),
  181. root.getNodeByPath( [ 0, 1, 0 ] )
  182. );
  183. expect( command.isEnabled ).to.be.false;
  184. } );
  185. } );
  186. describe( 'execute()', () => {
  187. it( 'should merge simple table cell selection', () => {
  188. setData( model, modelTable( [
  189. [ '[]00', '01' ]
  190. ] ) );
  191. tableSelection._setCellSelection(
  192. root.getNodeByPath( [ 0, 0, 0 ] ),
  193. root.getNodeByPath( [ 0, 0, 1 ] )
  194. );
  195. command.execute();
  196. assertEqualMarkup( getData( model ), modelTable( [
  197. [ { colspan: 2, contents: '<paragraph>[00</paragraph><paragraph>01]</paragraph>' } ]
  198. ] ) );
  199. } );
  200. it( 'should merge selection with a cell with rowspan in the selection', () => {
  201. setData( model, modelTable( [
  202. [ '[]00', '01', '02' ],
  203. [ '10', { contents: '11', rowspan: 2 }, '12' ],
  204. [ '20', '22' ]
  205. ] ) );
  206. tableSelection._setCellSelection(
  207. root.getNodeByPath( [ 0, 1, 0 ] ),
  208. root.getNodeByPath( [ 0, 2, 1 ] )
  209. );
  210. command.execute();
  211. assertEqualMarkup( getData( model ), modelTable( [
  212. [ '00', '01', '02' ],
  213. [ {
  214. colspan: 3,
  215. contents: '<paragraph>[10</paragraph><paragraph>11</paragraph><paragraph>12</paragraph>' +
  216. '<paragraph>20</paragraph><paragraph>22]</paragraph>'
  217. } ]
  218. ] ) );
  219. } );
  220. it( 'should merge selection with a cell with rowspan in the selection (reverse selection)', () => {
  221. setData( model, modelTable( [
  222. [ '[]00', '01', '02' ],
  223. [ '10', { contents: '11', rowspan: 2 }, '12' ],
  224. [ '20', '22' ]
  225. ] ) );
  226. tableSelection._setCellSelection(
  227. root.getNodeByPath( [ 0, 2, 1 ] ),
  228. root.getNodeByPath( [ 0, 1, 0 ] )
  229. );
  230. command.execute();
  231. assertEqualMarkup( getData( model ), modelTable( [
  232. [ '00', '01', '02' ],
  233. [ {
  234. colspan: 3,
  235. contents: '<paragraph>[10</paragraph><paragraph>11</paragraph><paragraph>12</paragraph>' +
  236. '<paragraph>20</paragraph><paragraph>22]</paragraph>'
  237. } ]
  238. ] ) );
  239. } );
  240. it( 'should merge selection inside a table (properly calculate target rowspan/colspan)', () => {
  241. setData( model, modelTable( [
  242. [ '[]00', '01', '02', '03' ],
  243. [ '10', '11', { contents: '12', rowspan: 2 }, '13' ],
  244. [ '20', '21', '23' ],
  245. [ '30', '31', '32', '33' ]
  246. ] ) );
  247. tableSelection._setCellSelection(
  248. root.getNodeByPath( [ 0, 2, 1 ] ),
  249. root.getNodeByPath( [ 0, 1, 2 ] )
  250. );
  251. command.execute();
  252. assertEqualMarkup( getData( model ), modelTable( [
  253. [ '00', '01', '02', '03' ],
  254. [ '10', {
  255. colspan: 2,
  256. rowspan: 2,
  257. contents: '<paragraph>[11</paragraph><paragraph>12</paragraph><paragraph>21]</paragraph>'
  258. }, '13' ],
  259. [ '20', '23' ],
  260. [ '30', '31', '32', '33' ]
  261. ] ) );
  262. } );
  263. it( 'should merge table cells - extend colspan attribute', () => {
  264. setData( model, modelTable( [
  265. [ { colspan: 2, contents: '00' }, '02', '03' ],
  266. [ '10', '11', '12', '13' ]
  267. ] ) );
  268. selectNodes( [
  269. [ 0, 0, 0 ], [ 0, 0, 1 ],
  270. [ 0, 1, 0 ], [ 0, 1, 1 ], [ 0, 1, 2 ]
  271. ] );
  272. command.execute();
  273. assertEqualMarkup( getData( model ), modelTable( [
  274. [ {
  275. colspan: 3,
  276. rowspan: 2,
  277. contents: '<paragraph>[00</paragraph>' +
  278. '<paragraph>02</paragraph>' +
  279. '<paragraph>10</paragraph>' +
  280. '<paragraph>11</paragraph>' +
  281. '<paragraph>12]</paragraph>'
  282. }, '03' ],
  283. [ '13' ]
  284. ] ) );
  285. } );
  286. it( 'should merge to a single paragraph - every cell is empty', () => {
  287. setData( model, modelTable( [
  288. [ '[]', '' ]
  289. ] ) );
  290. selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 1 ] ] );
  291. command.execute();
  292. assertEqualMarkup( getData( model ), modelTable( [
  293. [ { colspan: 2, contents: '<paragraph>[]</paragraph>' } ]
  294. ] ) );
  295. } );
  296. it( 'should merge to a single paragraph - merged cell is empty', () => {
  297. setData( model, modelTable( [
  298. [ 'foo', '' ]
  299. ] ) );
  300. selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 1 ] ] );
  301. command.execute();
  302. assertEqualMarkup( getData( model ), modelTable( [
  303. [ { colspan: 2, contents: '<paragraph>[foo]</paragraph>' } ]
  304. ] ) );
  305. } );
  306. it( 'should merge to a single paragraph - cell to which others are merged is empty', () => {
  307. setData( model, modelTable( [
  308. [ '', 'foo' ]
  309. ] ) );
  310. selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 1 ] ] );
  311. command.execute();
  312. assertEqualMarkup( getData( model ), modelTable( [
  313. [ { colspan: 2, contents: '<paragraph>[foo]</paragraph>' } ]
  314. ] ) );
  315. } );
  316. it( 'should not merge empty blocks other then <paragraph> to a single block', () => {
  317. model.schema.register( 'block', {
  318. allowWhere: '$block',
  319. allowContentOf: '$block',
  320. isBlock: true
  321. } );
  322. setData( model, modelTable( [
  323. [ '<block>[]</block>', '<block></block>' ]
  324. ] ) );
  325. selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 1 ] ] );
  326. command.execute();
  327. assertEqualMarkup( getData( model ), modelTable( [
  328. [ { colspan: 2, contents: '<block>[</block><block>]</block>' } ]
  329. ] ) );
  330. } );
  331. describe( 'removing empty row', () => {
  332. it( 'should remove empty row if merging all table cells from that row', () => {
  333. setData( model, modelTable( [
  334. [ '00' ],
  335. [ '10' ],
  336. [ '20' ]
  337. ] ) );
  338. selectNodes( [
  339. [ 0, 0, 0 ],
  340. [ 0, 1, 0 ],
  341. [ 0, 2, 0 ]
  342. ] );
  343. command.execute();
  344. assertEqualMarkup( getData( model ), modelTable( [
  345. [
  346. '<paragraph>[00</paragraph><paragraph>10</paragraph><paragraph>20]</paragraph>'
  347. ]
  348. ] ) );
  349. } );
  350. it( 'should decrease rowspan if cell overlaps removed row', () => {
  351. setData( model, modelTable( [
  352. [ '00', { rowspan: 2, contents: '01' }, { rowspan: 3, contents: '02' } ],
  353. [ '10' ],
  354. [ '20', '21' ]
  355. ] ) );
  356. selectNodes( [
  357. [ 0, 0, 0 ],
  358. [ 0, 1, 0 ],
  359. [ 0, 2, 0 ]
  360. ] );
  361. command.execute();
  362. assertEqualMarkup( getData( model ), modelTable( [
  363. [
  364. { rowspan: 2, contents: '<paragraph>[00</paragraph><paragraph>10</paragraph><paragraph>20]</paragraph>' },
  365. '01',
  366. { rowspan: 2, contents: '02' }
  367. ],
  368. [ '21' ]
  369. ] ) );
  370. } );
  371. it( 'should not decrease rowspan if cell from previous row does not overlaps removed row', () => {
  372. setData( model, modelTable( [
  373. [ '00', { rowspan: 2, contents: '01' } ],
  374. [ '10' ],
  375. [ '20', '21' ],
  376. [ '30', '31' ]
  377. ] ) );
  378. selectNodes( [
  379. [ 0, 2, 0 ], [ 0, 2, 1 ],
  380. [ 0, 3, 0 ], [ 0, 3, 1 ]
  381. ] );
  382. command.execute();
  383. assertEqualMarkup( getData( model ), modelTable( [
  384. [ '00', { rowspan: 2, contents: '01' } ],
  385. [ '10' ],
  386. [
  387. {
  388. colspan: 2,
  389. contents: '<paragraph>[20</paragraph><paragraph>21</paragraph>' +
  390. '<paragraph>30</paragraph><paragraph>31]</paragraph>'
  391. }
  392. ]
  393. ] ) );
  394. } );
  395. } );
  396. } );
  397. function selectNodes( paths ) {
  398. model.change( writer => {
  399. const ranges = paths.map( path => writer.createRangeOn( root.getNodeByPath( path ) ) );
  400. writer.setSelection( ranges );
  401. } );
  402. }
  403. } );