mergecellcommand.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  7. import { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
  8. import MergeCellCommand from '../../src/commands/mergecellcommand';
  9. import {
  10. downcastInsertCell,
  11. downcastInsertRow,
  12. downcastInsertTable,
  13. downcastRemoveRow,
  14. downcastTableHeadingColumnsChange,
  15. downcastTableHeadingRowsChange
  16. } from '../../src/converters/downcast';
  17. import upcastTable from '../../src/converters/upcasttable';
  18. import { formatTable, formattedModelTable, modelTable } from '../_utils/utils';
  19. describe( 'MergeCellCommand', () => {
  20. let editor, model, command, root;
  21. beforeEach( () => {
  22. return ModelTestEditor.create()
  23. .then( newEditor => {
  24. editor = newEditor;
  25. model = editor.model;
  26. root = model.document.getRoot( 'main' );
  27. const conversion = editor.conversion;
  28. const schema = model.schema;
  29. schema.register( 'table', {
  30. allowWhere: '$block',
  31. allowAttributes: [ 'headingRows' ],
  32. isObject: true
  33. } );
  34. schema.register( 'tableRow', { allowIn: 'table' } );
  35. schema.register( 'tableCell', {
  36. allowIn: 'tableRow',
  37. allowContentOf: '$block',
  38. allowAttributes: [ 'colspan', 'rowspan' ],
  39. isLimit: true
  40. } );
  41. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  42. // Table conversion.
  43. conversion.for( 'upcast' ).add( upcastTable() );
  44. conversion.for( 'downcast' ).add( downcastInsertTable() );
  45. // Insert row conversion.
  46. conversion.for( 'downcast' ).add( downcastInsertRow() );
  47. // Remove row conversion.
  48. conversion.for( 'downcast' ).add( downcastRemoveRow() );
  49. // Table cell conversion.
  50. conversion.for( 'downcast' ).add( downcastInsertCell() );
  51. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'td' } ) );
  52. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'th' } ) );
  53. // Table attributes conversion.
  54. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  55. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  56. conversion.for( 'downcast' ).add( downcastTableHeadingColumnsChange() );
  57. conversion.for( 'downcast' ).add( downcastTableHeadingRowsChange() );
  58. } );
  59. } );
  60. afterEach( () => {
  61. return editor.destroy();
  62. } );
  63. describe( 'direction=right', () => {
  64. beforeEach( () => {
  65. command = new MergeCellCommand( editor, { direction: 'right' } );
  66. } );
  67. describe( 'isEnabled', () => {
  68. it( 'should be true if in cell that has sibling on the right', () => {
  69. setData( model, modelTable( [
  70. [ '00[]', '01' ]
  71. ] ) );
  72. expect( command.isEnabled ).to.be.true;
  73. } );
  74. it( 'should be false if last cell of a row', () => {
  75. setData( model, modelTable( [
  76. [ '00', '01[]' ]
  77. ] ) );
  78. expect( command.isEnabled ).to.be.false;
  79. } );
  80. it( 'should be true if in a cell that has sibling on the right with the same rowspan', () => {
  81. setData( model, modelTable( [
  82. [ { rowspan: 2, contents: '00[]' }, { rowspan: 2, contents: '01' } ]
  83. ] ) );
  84. expect( command.isEnabled ).to.be.true;
  85. } );
  86. it( 'should be false if in a cell that has sibling but with different rowspan', () => {
  87. setData( model, modelTable( [
  88. [ { rowspan: 2, contents: '00[]' }, { rowspan: 3, contents: '01' } ]
  89. ] ) );
  90. expect( command.isEnabled ).to.be.false;
  91. } );
  92. it( 'should be false if not in a cell', () => {
  93. setData( model, '<p>11[]</p>' );
  94. expect( command.isEnabled ).to.be.false;
  95. } );
  96. } );
  97. describe( 'value', () => {
  98. it( 'should be set to mergeable sibling if in cell that has sibling on the right', () => {
  99. setData( model, modelTable( [
  100. [ '00[]', '01' ]
  101. ] ) );
  102. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 1 ] ) );
  103. } );
  104. it( 'should be undefined if last cell of a row', () => {
  105. setData( model, modelTable( [
  106. [ '00', '01[]' ]
  107. ] ) );
  108. expect( command.value ).to.be.undefined;
  109. } );
  110. it( 'should be set to mergeable sibling if in a cell that has sibling on the right with the same rowspan', () => {
  111. setData( model, modelTable( [
  112. [ { rowspan: 2, contents: '00[]' }, { rowspan: 2, contents: '01' } ]
  113. ] ) );
  114. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 1 ] ) );
  115. } );
  116. it( 'should be undefined if in a cell that has sibling but with different rowspan', () => {
  117. setData( model, modelTable( [
  118. [ { rowspan: 2, contents: '00[]' }, { rowspan: 3, contents: '01' } ]
  119. ] ) );
  120. expect( command.value ).to.be.undefined;
  121. } );
  122. it( 'should be undefined if not in a cell', () => {
  123. setData( model, '<p>11[]</p>' );
  124. expect( command.value ).to.be.undefined;
  125. } );
  126. } );
  127. describe( 'execute()', () => {
  128. it( 'should merge table cells ', () => {
  129. setData( model, modelTable( [
  130. [ '[]00', '01' ]
  131. ] ) );
  132. command.execute();
  133. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  134. [ { colspan: 2, contents: '[0001]' } ]
  135. ] ) );
  136. } );
  137. } );
  138. } );
  139. describe( 'direction=left', () => {
  140. beforeEach( () => {
  141. command = new MergeCellCommand( editor, { direction: 'left' } );
  142. } );
  143. describe( 'isEnabled', () => {
  144. it( 'should be true if in cell that has sibling on the left', () => {
  145. setData( model, modelTable( [
  146. [ '00', '01[]' ]
  147. ] ) );
  148. expect( command.isEnabled ).to.be.true;
  149. } );
  150. it( 'should be false if first cell of a row', () => {
  151. setData( model, modelTable( [
  152. [ '00[]', '01' ]
  153. ] ) );
  154. expect( command.isEnabled ).to.be.false;
  155. } );
  156. it( 'should be true if in a cell that has sibling on the left with the same rowspan', () => {
  157. setData( model, modelTable( [
  158. [ { rowspan: 2, contents: '00' }, { rowspan: 2, contents: '01[]' } ]
  159. ] ) );
  160. expect( command.isEnabled ).to.be.true;
  161. } );
  162. it( 'should be false if in a cell that has sibling but with different rowspan', () => {
  163. setData( model, modelTable( [
  164. [ { rowspan: 2, contents: '00' }, { rowspan: 3, contents: '01[]' } ]
  165. ] ) );
  166. expect( command.isEnabled ).to.be.false;
  167. } );
  168. it( 'should be false if not in a cell', () => {
  169. setData( model, '<p>11[]</p>' );
  170. expect( command.isEnabled ).to.be.false;
  171. } );
  172. } );
  173. describe( 'value', () => {
  174. it( 'should be set to mergeable sibling if in cell that has sibling on the left', () => {
  175. setData( model, modelTable( [
  176. [ '00', '01[]' ]
  177. ] ) );
  178. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 0 ] ) );
  179. } );
  180. it( 'should be undefined if first cell of a row', () => {
  181. setData( model, modelTable( [
  182. [ '00[]', '01' ]
  183. ] ) );
  184. expect( command.value ).to.be.undefined;
  185. } );
  186. it( 'should be set to mergeable sibling if in a cell that has sibling on the left with the same rowspan', () => {
  187. setData( model, modelTable( [
  188. [ { rowspan: 2, contents: '00' }, { rowspan: 2, contents: '01[]' } ]
  189. ] ) );
  190. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 0 ] ) );
  191. } );
  192. it( 'should be undefined if in a cell that has sibling but with different rowspan', () => {
  193. setData( model, modelTable( [
  194. [ { rowspan: 2, contents: '00' }, { rowspan: 3, contents: '01[]' } ]
  195. ] ) );
  196. expect( command.value ).to.be.undefined;
  197. } );
  198. it( 'should be undefined if not in a cell', () => {
  199. setData( model, '<p>11[]</p>' );
  200. expect( command.value ).to.be.undefined;
  201. } );
  202. } );
  203. describe( 'execute()', () => {
  204. it( 'should merge table cells ', () => {
  205. setData( model, modelTable( [
  206. [ '00', '[]01' ]
  207. ] ) );
  208. command.execute();
  209. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  210. [ { colspan: 2, contents: '[0001]' } ]
  211. ] ) );
  212. } );
  213. } );
  214. } );
  215. describe( 'direction=down', () => {
  216. beforeEach( () => {
  217. command = new MergeCellCommand( editor, { direction: 'down' } );
  218. } );
  219. describe( 'isEnabled', () => {
  220. it( 'should be true if in cell that has mergeable cell in next row', () => {
  221. setData( model, modelTable( [
  222. [ '00', '01[]' ],
  223. [ '10', '11' ]
  224. ] ) );
  225. expect( command.isEnabled ).to.be.true;
  226. } );
  227. it( 'should be false if in last row', () => {
  228. setData( model, modelTable( [
  229. [ '00', '01' ],
  230. [ '10[]', '11' ]
  231. ] ) );
  232. expect( command.isEnabled ).to.be.false;
  233. } );
  234. it( 'should be true if in a cell that has mergeable cell with the same colspan', () => {
  235. setData( model, modelTable( [
  236. [ { colspan: 2, contents: '00[]' }, '02' ],
  237. [ { colspan: 2, contents: '01' }, '12' ]
  238. ] ) );
  239. expect( command.isEnabled ).to.be.true;
  240. } );
  241. it( 'should be false if in a cell that potential mergeable cell has different colspan', () => {
  242. setData( model, modelTable( [
  243. [ { colspan: 2, contents: '00[]' }, '02' ],
  244. [ { colspan: 3, contents: '01' } ]
  245. ] ) );
  246. expect( command.isEnabled ).to.be.false;
  247. } );
  248. it( 'should be false if not in a cell', () => {
  249. setData( model, '<p>11[]</p>' );
  250. expect( command.isEnabled ).to.be.false;
  251. } );
  252. it( 'should be false if mergeable cell is in other table section then current cell', () => {
  253. setData( model, modelTable( [
  254. [ '00[]', '01' ],
  255. [ '10', '11' ]
  256. ], { headingRows: 1 } ) );
  257. expect( command.isEnabled ).to.be.false;
  258. } );
  259. } );
  260. describe( 'value', () => {
  261. it( 'should be set to mergeable cell', () => {
  262. setData( model, modelTable( [
  263. [ '00', '01[]' ],
  264. [ '10', '11' ]
  265. ] ) );
  266. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 1, 1 ] ) );
  267. } );
  268. it( 'should be undefined if in last row', () => {
  269. setData( model, modelTable( [
  270. [ '00', '01' ],
  271. [ '10[]', '11' ]
  272. ] ) );
  273. expect( command.value ).to.be.undefined;
  274. } );
  275. it( 'should be set to mergeable cell with the same rowspan', () => {
  276. setData( model, modelTable( [
  277. [ { colspan: 2, contents: '00[]' }, '02' ],
  278. [ { colspan: 2, contents: '01' }, '12' ]
  279. ] ) );
  280. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 1, 0 ] ) );
  281. } );
  282. it( 'should be undefined if in a cell that potential mergeable cell has different rowspan', () => {
  283. setData( model, modelTable( [
  284. [ { colspan: 2, contents: '00[]' }, '02' ],
  285. [ { colspan: 3, contents: '01' } ]
  286. ] ) );
  287. expect( command.value ).to.be.undefined;
  288. } );
  289. it( 'should be undefined if not in a cell', () => {
  290. setData( model, '<p>11[]</p>' );
  291. expect( command.value ).to.be.undefined;
  292. } );
  293. } );
  294. describe( 'execute()', () => {
  295. it( 'should merge table cells ', () => {
  296. setData( model, modelTable( [
  297. [ '00', '01[]' ],
  298. [ '10', '11' ]
  299. ] ) );
  300. command.execute();
  301. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  302. [ '00', { rowspan: 2, contents: '[0111]' } ],
  303. [ '10' ]
  304. ] ) );
  305. } );
  306. } );
  307. } );
  308. describe( 'direction=up', () => {
  309. beforeEach( () => {
  310. command = new MergeCellCommand( editor, { direction: 'up' } );
  311. } );
  312. describe( 'isEnabled', () => {
  313. it( 'should be true if in cell that has mergeable cell in previous row', () => {
  314. setData( model, modelTable( [
  315. [ '00', '01' ],
  316. [ '10', '11[]' ]
  317. ] ) );
  318. expect( command.isEnabled ).to.be.true;
  319. } );
  320. it( 'should be false if in first row', () => {
  321. setData( model, modelTable( [
  322. [ '00[]', '01' ],
  323. [ '10', '11' ]
  324. ] ) );
  325. expect( command.isEnabled ).to.be.false;
  326. } );
  327. it( 'should be true if in a cell that has mergeable cell with the same colspan', () => {
  328. setData( model, modelTable( [
  329. [ { colspan: 2, contents: '00' }, '02' ],
  330. [ { colspan: 2, contents: '01[]' }, '12' ]
  331. ] ) );
  332. expect( command.isEnabled ).to.be.true;
  333. } );
  334. it( 'should be false if in a cell that potential mergeable cell has different colspan', () => {
  335. setData( model, modelTable( [
  336. [ { colspan: 2, contents: '00' }, '02' ],
  337. [ { colspan: 3, contents: '01[]' } ]
  338. ] ) );
  339. expect( command.isEnabled ).to.be.false;
  340. } );
  341. it( 'should be false if not in a cell', () => {
  342. setData( model, '<p>11[]</p>' );
  343. expect( command.isEnabled ).to.be.false;
  344. } );
  345. it( 'should be false if mergeable cell is in other table section then current cell', () => {
  346. setData( model, modelTable( [
  347. [ '00', '01' ],
  348. [ '10[]', '11' ]
  349. ], { headingRows: 1 } ) );
  350. expect( command.isEnabled ).to.be.false;
  351. } );
  352. } );
  353. describe( 'value', () => {
  354. it( 'should be set to mergeable cell', () => {
  355. setData( model, modelTable( [
  356. [ '00', '01' ],
  357. [ '10', '11[]' ]
  358. ] ) );
  359. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 1 ] ) );
  360. } );
  361. it( 'should be undefined if in first row', () => {
  362. setData( model, modelTable( [
  363. [ '00[]', '01' ],
  364. [ '10', '11' ]
  365. ] ) );
  366. expect( command.value ).to.be.undefined;
  367. } );
  368. it( 'should be set to mergeable cell with the same rowspan', () => {
  369. setData( model, modelTable( [
  370. [ { colspan: 2, contents: '00' }, '02' ],
  371. [ { colspan: 2, contents: '01[]' }, '12' ]
  372. ] ) );
  373. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 0 ] ) );
  374. } );
  375. it( 'should be undefined if in a cell that potential mergeable cell has different rowspan', () => {
  376. setData( model, modelTable( [
  377. [ { colspan: 2, contents: '00' }, '02' ],
  378. [ { colspan: 3, contents: '01[]' } ]
  379. ] ) );
  380. expect( command.value ).to.be.undefined;
  381. } );
  382. it( 'should be undefined if not in a cell', () => {
  383. setData( model, '<p>11[]</p>' );
  384. expect( command.value ).to.be.undefined;
  385. } );
  386. } );
  387. describe( 'execute()', () => {
  388. it( 'should merge table cells ', () => {
  389. setData( model, modelTable( [
  390. [ '00', '01' ],
  391. [ '10', '11[]' ]
  392. ] ) );
  393. command.execute();
  394. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  395. [ '00', { rowspan: 2, contents: '[0111]' } ],
  396. [ '10' ]
  397. ] ) );
  398. } );
  399. } );
  400. } );
  401. } );