mergecellscommand.js 13 KB

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