8
0

mergecellscommand.js 10 KB

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