mergecellcommand.js 14 KB

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