8
0

mergecellscommand.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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;
  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. command = new MergeCellsCommand( editor );
  22. } );
  23. afterEach( () => {
  24. return editor.destroy();
  25. } );
  26. describe( 'isEnabled', () => {
  27. it( 'should be false if collapsed selection in table cell', () => {
  28. setData( model, modelTable( [
  29. [ '00[]', '01' ]
  30. ] ) );
  31. expect( command.isEnabled ).to.be.false;
  32. } );
  33. it( 'should be false if only one table cell is selected', () => {
  34. setData( model, modelTable( [
  35. [ '00', '01' ]
  36. ] ) );
  37. selectNodes( [ [ 0, 0, 0 ] ] );
  38. expect( command.isEnabled ).to.be.false;
  39. } );
  40. it( 'should be true if at least two adjacent table cells are selected', () => {
  41. setData( model, modelTable( [
  42. [ '00', '01' ]
  43. ] ) );
  44. selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 1 ] ] );
  45. expect( command.isEnabled ).to.be.true;
  46. } );
  47. it( 'should be true if many table cells are selected', () => {
  48. setData( model, modelTable( [
  49. [ '00', '01', '02', '03' ],
  50. [ '10', '11', '12', '13' ],
  51. [ '20', '21', '22', '23' ],
  52. [ '30', '31', '32', '33' ]
  53. ] ) );
  54. selectNodes( [
  55. [ 0, 0, 1 ], [ 0, 0, 2 ],
  56. [ 0, 1, 1 ], [ 0, 1, 2 ],
  57. [ 0, 2, 1 ], [ 0, 2, 2 ],
  58. [ 0, 3, 1 ], [ 0, 3, 2 ]
  59. ] );
  60. expect( command.isEnabled ).to.be.true;
  61. } );
  62. it( 'should be false if at least one table cell is not selected from an area', () => {
  63. setData( model, modelTable( [
  64. [ '00', '01', '02', '03' ],
  65. [ '10', '11', '12', '13' ],
  66. [ '20', '21', '22', '23' ],
  67. [ '30', '31', '32', '33' ]
  68. ] ) );
  69. selectNodes( [
  70. [ 0, 0, 1 ], [ 0, 0, 2 ],
  71. [ 0, 1, 2 ], // one table cell not selected from this row
  72. [ 0, 2, 1 ], [ 0, 2, 2 ],
  73. [ 0, 3, 1 ], [ 0, 3, 2 ]
  74. ] );
  75. expect( command.isEnabled ).to.be.false;
  76. } );
  77. it( 'should be false if table cells are not in adjacent rows', () => {
  78. setData( model, modelTable( [
  79. [ '00', '01' ],
  80. [ '10', '11' ]
  81. ] ) );
  82. selectNodes( [
  83. [ 0, 1, 0 ],
  84. [ 0, 0, 1 ]
  85. ] );
  86. expect( command.isEnabled ).to.be.false;
  87. } );
  88. it( 'should be false if table cells are not in adjacent columns', () => {
  89. setData( model, modelTable( [
  90. [ '00', '01', '02' ]
  91. ] ) );
  92. selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 2 ] ] );
  93. expect( command.isEnabled ).to.be.false;
  94. } );
  95. it( 'should be false if any range is collapsed in selection', () => {
  96. setData( model, modelTable( [
  97. [ '00', '01', '02' ]
  98. ] ) );
  99. selectNodes( [
  100. [ 0, 0, 0, 0, 0 ], // The "00" text node
  101. [ 0, 0, 1 ]
  102. ] );
  103. expect( command.isEnabled ).to.be.false;
  104. } );
  105. it( 'should be false if any ranges are on different tables', () => {
  106. setData( model,
  107. modelTable( [ [ '00', '01' ] ] ) +
  108. modelTable( [ [ 'aa', 'ab' ] ] )
  109. );
  110. selectNodes( [
  111. [ 0, 0, 0 ], // first table
  112. [ 1, 0, 1 ] // second table
  113. ] );
  114. expect( command.isEnabled ).to.be.false;
  115. } );
  116. it( 'should be false if any table cell with colspan attribute extends over selection area', () => {
  117. setData( model, modelTable( [
  118. [ '00', { colspan: 2, contents: '01' } ],
  119. [ '10', '11', '12' ]
  120. ] ) );
  121. selectNodes( [
  122. [ 0, 0, 0 ], [ 0, 0, 1 ],
  123. [ 0, 1, 0 ], [ 0, 1, 1 ]
  124. ] );
  125. expect( command.isEnabled ).to.be.false;
  126. } );
  127. it( 'should be true if none table cell with colspan attribute extends over selection area', () => {
  128. setData( model, modelTable( [
  129. [ '00', { colspan: 2, contents: '01' } ],
  130. [ '10', '11', '12' ]
  131. ] ) );
  132. selectNodes( [
  133. [ 0, 0, 0 ], [ 0, 0, 1 ],
  134. [ 0, 1, 0 ], [ 0, 1, 1 ],
  135. [ 0, 1, 2 ]
  136. ] );
  137. expect( command.isEnabled ).to.be.true;
  138. } );
  139. it( 'should be true if first table cell is inside selection area', () => {
  140. setData( model, modelTable( [
  141. [ { colspan: 2, rowspan: 2, contents: '00' }, '02', '03' ],
  142. [ '12', '13' ]
  143. ] ) );
  144. selectNodes( [
  145. [ 0, 0, 0 ], [ 0, 0, 1 ],
  146. [ 0, 1, 0 ]
  147. ] );
  148. expect( command.isEnabled ).to.be.true;
  149. } );
  150. it( 'should be false if any table cell with rowspan attribute extends over selection area', () => {
  151. setData( model, modelTable( [
  152. [ '00', { rowspan: 2, contents: '01' } ],
  153. [ '10' ]
  154. ] ) );
  155. selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 1 ] ] );
  156. expect( command.isEnabled ).to.be.false;
  157. } );
  158. it( 'should be true if none table cell with rowspan attribute extends over selection area', () => {
  159. setData( model, modelTable( [
  160. [ '00', { rowspan: 2, contents: '01' } ],
  161. [ '10' ]
  162. ] ) );
  163. selectNodes( [
  164. [ 0, 0, 0 ], [ 0, 0, 1 ],
  165. [ 0, 1, 0 ]
  166. ] );
  167. expect( command.isEnabled ).to.be.true;
  168. } );
  169. it( 'should be false if not in a cell', () => {
  170. setData( model, '<paragraph>11[]</paragraph>' );
  171. expect( command.isEnabled ).to.be.false;
  172. } );
  173. it( 'should be false if selection has cells from header and body sections', () => {
  174. setData( model, modelTable( [
  175. [ '00[]', '01' ],
  176. [ '10', '11' ]
  177. ], { headingRows: 1 } ) );
  178. const tableSelection = editor.plugins.get( TableSelection );
  179. const modelRoot = model.document.getRoot();
  180. tableSelection._setCellSelection(
  181. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  182. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  183. );
  184. expect( command.isEnabled ).to.be.false;
  185. } );
  186. } );
  187. describe( 'execute()', () => {
  188. it( 'should merge simple table cell selection', () => {
  189. setData( model, modelTable( [
  190. [ '[]00', '01' ]
  191. ] ) );
  192. selectNodes( [
  193. [ 0, 0, 0 ], [ 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 table cells - extend colspan attribute', () => {
  201. setData( model, modelTable( [
  202. [ { colspan: 2, contents: '00' }, '02', '03' ],
  203. [ '10', '11', '12', '13' ]
  204. ] ) );
  205. selectNodes( [
  206. [ 0, 0, 0 ], [ 0, 0, 1 ],
  207. [ 0, 1, 0 ], [ 0, 1, 1 ], [ 0, 1, 2 ]
  208. ] );
  209. command.execute();
  210. assertEqualMarkup( getData( model ), modelTable( [
  211. [ {
  212. colspan: 3,
  213. rowspan: 2,
  214. contents: '<paragraph>[00</paragraph>' +
  215. '<paragraph>02</paragraph>' +
  216. '<paragraph>10</paragraph>' +
  217. '<paragraph>11</paragraph>' +
  218. '<paragraph>12]</paragraph>'
  219. }, '03' ],
  220. [ '13' ]
  221. ] ) );
  222. } );
  223. it( 'should merge to a single paragraph - every cell is empty', () => {
  224. setData( model, modelTable( [
  225. [ '[]', '' ]
  226. ] ) );
  227. selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 1 ] ] );
  228. command.execute();
  229. assertEqualMarkup( getData( model ), modelTable( [
  230. [ { colspan: 2, contents: '<paragraph>[]</paragraph>' } ]
  231. ] ) );
  232. } );
  233. it( 'should merge to a single paragraph - merged cell is empty', () => {
  234. setData( model, modelTable( [
  235. [ 'foo', '' ]
  236. ] ) );
  237. selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 1 ] ] );
  238. command.execute();
  239. assertEqualMarkup( getData( model ), modelTable( [
  240. [ { colspan: 2, contents: '<paragraph>[foo]</paragraph>' } ]
  241. ] ) );
  242. } );
  243. it( 'should merge to a single paragraph - cell to which others are merged is empty', () => {
  244. setData( model, modelTable( [
  245. [ '', 'foo' ]
  246. ] ) );
  247. selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 1 ] ] );
  248. command.execute();
  249. assertEqualMarkup( getData( model ), modelTable( [
  250. [ { colspan: 2, contents: '<paragraph>[foo]</paragraph>' } ]
  251. ] ) );
  252. } );
  253. it( 'should not merge empty blocks other then <paragraph> to a single block', () => {
  254. model.schema.register( 'block', {
  255. allowWhere: '$block',
  256. allowContentOf: '$block',
  257. isBlock: true
  258. } );
  259. setData( model, modelTable( [
  260. [ '<block>[]</block>', '<block></block>' ]
  261. ] ) );
  262. selectNodes( [ [ 0, 0, 0 ], [ 0, 0, 1 ] ] );
  263. command.execute();
  264. assertEqualMarkup( getData( model ), modelTable( [
  265. [ { colspan: 2, contents: '<block>[</block><block>]</block>' } ]
  266. ] ) );
  267. } );
  268. describe( 'removing empty row', () => {
  269. it( 'should remove empty row if merging all table cells from that row', () => {
  270. setData( model, modelTable( [
  271. [ '00' ],
  272. [ '10' ],
  273. [ '20' ]
  274. ] ) );
  275. selectNodes( [
  276. [ 0, 0, 0 ],
  277. [ 0, 1, 0 ],
  278. [ 0, 2, 0 ]
  279. ] );
  280. command.execute();
  281. assertEqualMarkup( getData( model ), modelTable( [
  282. [
  283. '<paragraph>[00</paragraph><paragraph>10</paragraph><paragraph>20]</paragraph>'
  284. ]
  285. ] ) );
  286. } );
  287. it( 'should decrease rowspan if cell overlaps removed row', () => {
  288. setData( model, modelTable( [
  289. [ '00', { rowspan: 2, contents: '01' }, { rowspan: 3, contents: '02' } ],
  290. [ '10' ],
  291. [ '20', '21' ]
  292. ] ) );
  293. selectNodes( [
  294. [ 0, 0, 0 ],
  295. [ 0, 1, 0 ],
  296. [ 0, 2, 0 ]
  297. ] );
  298. command.execute();
  299. assertEqualMarkup( getData( model ), modelTable( [
  300. [
  301. { rowspan: 2, contents: '<paragraph>[00</paragraph><paragraph>10</paragraph><paragraph>20]</paragraph>' },
  302. '01',
  303. { rowspan: 2, contents: '02' }
  304. ],
  305. [ '21' ]
  306. ] ) );
  307. } );
  308. it( 'should not decrease rowspan if cell from previous row does not overlaps removed row', () => {
  309. setData( model, modelTable( [
  310. [ '00', { rowspan: 2, contents: '01' } ],
  311. [ '10' ],
  312. [ '20', '21' ],
  313. [ '30', '31' ]
  314. ] ) );
  315. selectNodes( [
  316. [ 0, 2, 0 ], [ 0, 2, 1 ],
  317. [ 0, 3, 0 ], [ 0, 3, 1 ]
  318. ] );
  319. command.execute();
  320. assertEqualMarkup( getData( model ), modelTable( [
  321. [ '00', { rowspan: 2, contents: '01' } ],
  322. [ '10' ],
  323. [
  324. {
  325. colspan: 2,
  326. contents: '<paragraph>[20</paragraph><paragraph>21</paragraph>' +
  327. '<paragraph>30</paragraph><paragraph>31]</paragraph>'
  328. }
  329. ]
  330. ] ) );
  331. } );
  332. } );
  333. } );
  334. function selectNodes( paths ) {
  335. model.change( writer => {
  336. const ranges = paths.map( path => writer.createRangeOn( root.getNodeByPath( path ) ) );
  337. writer.setSelection( ranges );
  338. } );
  339. }
  340. } );