mergecellcommand.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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 { getData, setData } 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. import TableUtils from '../../src/tableutils';
  20. describe( 'MergeCellCommand', () => {
  21. let editor, model, command, root;
  22. beforeEach( () => {
  23. return ModelTestEditor
  24. .create( {
  25. plugins: [ TableUtils ]
  26. } )
  27. .then( newEditor => {
  28. editor = newEditor;
  29. model = editor.model;
  30. root = model.document.getRoot( 'main' );
  31. const conversion = editor.conversion;
  32. const schema = model.schema;
  33. schema.register( 'table', {
  34. allowWhere: '$block',
  35. allowAttributes: [ 'headingRows' ],
  36. isObject: true
  37. } );
  38. schema.register( 'tableRow', { allowIn: 'table' } );
  39. schema.register( 'tableCell', {
  40. allowIn: 'tableRow',
  41. allowContentOf: '$block',
  42. allowAttributes: [ 'colspan', 'rowspan' ],
  43. isLimit: true
  44. } );
  45. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  46. // Table conversion.
  47. conversion.for( 'upcast' ).add( upcastTable() );
  48. conversion.for( 'downcast' ).add( downcastInsertTable() );
  49. // Insert row conversion.
  50. conversion.for( 'downcast' ).add( downcastInsertRow() );
  51. // Remove row conversion.
  52. conversion.for( 'downcast' ).add( downcastRemoveRow() );
  53. // Table cell conversion.
  54. conversion.for( 'downcast' ).add( downcastInsertCell() );
  55. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'td' } ) );
  56. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'th' } ) );
  57. // Table attributes conversion.
  58. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  59. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  60. conversion.for( 'downcast' ).add( downcastTableHeadingColumnsChange() );
  61. conversion.for( 'downcast' ).add( downcastTableHeadingRowsChange() );
  62. } );
  63. } );
  64. afterEach( () => {
  65. return editor.destroy();
  66. } );
  67. describe( 'direction=right', () => {
  68. beforeEach( () => {
  69. command = new MergeCellCommand( editor, { direction: 'right' } );
  70. } );
  71. describe( 'isEnabled', () => {
  72. it( 'should be true if in cell that has sibling on the right', () => {
  73. setData( model, modelTable( [
  74. [ '00[]', '01' ]
  75. ] ) );
  76. expect( command.isEnabled ).to.be.true;
  77. } );
  78. it( 'should be false if last cell of a row', () => {
  79. setData( model, modelTable( [
  80. [ '00', '01[]' ]
  81. ] ) );
  82. expect( command.isEnabled ).to.be.false;
  83. } );
  84. it( 'should be true if in a cell that has sibling on the right with the same rowspan', () => {
  85. setData( model, modelTable( [
  86. [ { rowspan: 2, contents: '00[]' }, { rowspan: 2, contents: '01' } ]
  87. ] ) );
  88. expect( command.isEnabled ).to.be.true;
  89. } );
  90. it( 'should be false if in a cell that has sibling but with different rowspan', () => {
  91. setData( model, modelTable( [
  92. [ { rowspan: 2, contents: '00[]' }, { rowspan: 3, contents: '01' } ]
  93. ] ) );
  94. expect( command.isEnabled ).to.be.false;
  95. } );
  96. it( 'should be false when next cell is rowspanned', () => {
  97. setData( model, modelTable( [
  98. [ '00', { rowspan: 3, contents: '01' }, '02' ],
  99. [ '10[]', '12' ],
  100. [ '20', '22' ]
  101. ] ) );
  102. expect( command.isEnabled ).to.be.false;
  103. } );
  104. it( 'should be true when current cell is colspanned', () => {
  105. setData( model, modelTable( [
  106. [ { colspan: 2, contents: '00[]' }, '02' ]
  107. ] ) );
  108. expect( command.isEnabled ).to.be.true;
  109. } );
  110. it( 'should be false if not in a cell', () => {
  111. setData( model, '<p>11[]</p>' );
  112. expect( command.isEnabled ).to.be.false;
  113. } );
  114. } );
  115. describe( 'value', () => {
  116. it( 'should be set to mergeable sibling if in cell that has sibling on the right', () => {
  117. setData( model, modelTable( [
  118. [ '00[]', '01' ]
  119. ] ) );
  120. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 1 ] ) );
  121. } );
  122. it( 'should be undefined if last cell of a row', () => {
  123. setData( model, modelTable( [
  124. [ '00', '01[]' ]
  125. ] ) );
  126. expect( command.value ).to.be.undefined;
  127. } );
  128. it( 'should be set to mergeable sibling if in a cell that has sibling on the right with the same rowspan', () => {
  129. setData( model, modelTable( [
  130. [ { rowspan: 2, contents: '00[]' }, { rowspan: 2, contents: '01' } ]
  131. ] ) );
  132. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 1 ] ) );
  133. } );
  134. it( 'should be undefined if in a cell that has sibling but with different rowspan', () => {
  135. setData( model, modelTable( [
  136. [ { rowspan: 2, contents: '00[]' }, { rowspan: 3, contents: '01' } ]
  137. ] ) );
  138. expect( command.value ).to.be.undefined;
  139. } );
  140. it( 'should be undefined if not in a cell', () => {
  141. setData( model, '<p>11[]</p>' );
  142. expect( command.value ).to.be.undefined;
  143. } );
  144. } );
  145. describe( 'execute()', () => {
  146. it( 'should merge table cells ', () => {
  147. setData( model, modelTable( [
  148. [ '[]00', '01' ]
  149. ] ) );
  150. command.execute();
  151. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  152. [ { colspan: 2, contents: '[0001]' } ]
  153. ] ) );
  154. } );
  155. } );
  156. } );
  157. describe( 'direction=left', () => {
  158. beforeEach( () => {
  159. command = new MergeCellCommand( editor, { direction: 'left' } );
  160. } );
  161. describe( 'isEnabled', () => {
  162. it( 'should be true if in cell that has sibling on the left', () => {
  163. setData( model, modelTable( [
  164. [ '00', '01[]' ]
  165. ] ) );
  166. expect( command.isEnabled ).to.be.true;
  167. } );
  168. it( 'should be false if first cell of a row', () => {
  169. setData( model, modelTable( [
  170. [ '00[]', '01' ]
  171. ] ) );
  172. expect( command.isEnabled ).to.be.false;
  173. } );
  174. it( 'should be true if in a cell that has sibling on the left with the same rowspan', () => {
  175. setData( model, modelTable( [
  176. [ { rowspan: 2, contents: '00' }, { rowspan: 2, contents: '01[]' } ]
  177. ] ) );
  178. expect( command.isEnabled ).to.be.true;
  179. } );
  180. it( 'should be false if in a cell that has sibling but with different rowspan', () => {
  181. setData( model, modelTable( [
  182. [ { rowspan: 2, contents: '00' }, { rowspan: 3, contents: '01[]' } ]
  183. ] ) );
  184. expect( command.isEnabled ).to.be.false;
  185. } );
  186. it( 'should be false when next cell is rowspanned', () => {
  187. setData( model, modelTable( [
  188. [ '00', { rowspan: 3, contents: '01' }, '02' ],
  189. [ '10', '12[]' ],
  190. [ '20', '22' ]
  191. ] ) );
  192. expect( command.isEnabled ).to.be.false;
  193. } );
  194. it( 'should be true when mergeable cell is colspanned', () => {
  195. setData( model, modelTable( [
  196. [ { colspan: 2, contents: '00' }, '02[]' ]
  197. ] ) );
  198. expect( command.isEnabled ).to.be.true;
  199. } );
  200. it( 'should be false if not in a cell', () => {
  201. setData( model, '<p>11[]</p>' );
  202. expect( command.isEnabled ).to.be.false;
  203. } );
  204. } );
  205. describe( 'value', () => {
  206. it( 'should be set to mergeable sibling if in cell that has sibling on the left', () => {
  207. setData( model, modelTable( [
  208. [ '00', '01[]' ]
  209. ] ) );
  210. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 0 ] ) );
  211. } );
  212. it( 'should be undefined if first cell of a row', () => {
  213. setData( model, modelTable( [
  214. [ '00[]', '01' ]
  215. ] ) );
  216. expect( command.value ).to.be.undefined;
  217. } );
  218. it( 'should be set to mergeable sibling if in a cell that has sibling on the left with the same rowspan', () => {
  219. setData( model, modelTable( [
  220. [ { rowspan: 2, contents: '00' }, { rowspan: 2, contents: '01[]' } ]
  221. ] ) );
  222. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 0 ] ) );
  223. } );
  224. it( 'should be undefined if in a cell that has sibling but with different rowspan', () => {
  225. setData( model, modelTable( [
  226. [ { rowspan: 2, contents: '00' }, { rowspan: 3, contents: '01[]' } ]
  227. ] ) );
  228. expect( command.value ).to.be.undefined;
  229. } );
  230. it( 'should be undefined if not in a cell', () => {
  231. setData( model, '<p>11[]</p>' );
  232. expect( command.value ).to.be.undefined;
  233. } );
  234. } );
  235. describe( 'execute()', () => {
  236. it( 'should merge table cells ', () => {
  237. setData( model, modelTable( [
  238. [ '00', '[]01' ]
  239. ] ) );
  240. command.execute();
  241. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  242. [ { colspan: 2, contents: '[0001]' } ]
  243. ] ) );
  244. } );
  245. } );
  246. } );
  247. describe( 'direction=down', () => {
  248. beforeEach( () => {
  249. command = new MergeCellCommand( editor, { direction: 'down' } );
  250. } );
  251. describe( 'isEnabled', () => {
  252. it( 'should be true if in cell that has mergeable cell in next row', () => {
  253. setData( model, modelTable( [
  254. [ '00', '01[]' ],
  255. [ '10', '11' ]
  256. ] ) );
  257. expect( command.isEnabled ).to.be.true;
  258. } );
  259. it( 'should be false if in last row', () => {
  260. setData( model, modelTable( [
  261. [ '00', '01' ],
  262. [ '10[]', '11' ]
  263. ] ) );
  264. expect( command.isEnabled ).to.be.false;
  265. } );
  266. it( 'should be true if in a cell that has mergeable cell with the same colspan', () => {
  267. setData( model, modelTable( [
  268. [ { colspan: 2, contents: '00[]' }, '02' ],
  269. [ { colspan: 2, contents: '01' }, '12' ]
  270. ] ) );
  271. expect( command.isEnabled ).to.be.true;
  272. } );
  273. it( 'should be false if in a cell that potential mergeable cell has different colspan', () => {
  274. setData( model, modelTable( [
  275. [ { colspan: 2, contents: '00[]' }, '02' ],
  276. [ { colspan: 3, contents: '01' } ]
  277. ] ) );
  278. expect( command.isEnabled ).to.be.false;
  279. } );
  280. it( 'should be false if not in a cell', () => {
  281. setData( model, '<p>11[]</p>' );
  282. expect( command.isEnabled ).to.be.false;
  283. } );
  284. it( 'should be false if mergeable cell is in other table section then current cell', () => {
  285. setData( model, modelTable( [
  286. [ '00[]', '01' ],
  287. [ '10', '11' ]
  288. ], { headingRows: 1 } ) );
  289. expect( command.isEnabled ).to.be.false;
  290. } );
  291. } );
  292. describe( 'value', () => {
  293. it( 'should be set to mergeable cell', () => {
  294. setData( model, modelTable( [
  295. [ '00', '01[]' ],
  296. [ '10', '11' ]
  297. ] ) );
  298. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 1, 1 ] ) );
  299. } );
  300. it( 'should be undefined if in last row', () => {
  301. setData( model, modelTable( [
  302. [ '00', '01' ],
  303. [ '10[]', '11' ]
  304. ] ) );
  305. expect( command.value ).to.be.undefined;
  306. } );
  307. it( 'should be set to mergeable cell with the same rowspan', () => {
  308. setData( model, modelTable( [
  309. [ { colspan: 2, contents: '00[]' }, '02' ],
  310. [ { colspan: 2, contents: '01' }, '12' ]
  311. ] ) );
  312. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 1, 0 ] ) );
  313. } );
  314. it( 'should be undefined if in a cell that potential mergeable cell has different rowspan', () => {
  315. setData( model, modelTable( [
  316. [ { colspan: 2, contents: '00[]' }, '02' ],
  317. [ { colspan: 3, contents: '01' } ]
  318. ] ) );
  319. expect( command.value ).to.be.undefined;
  320. } );
  321. it( 'should be undefined if not in a cell', () => {
  322. setData( model, '<p>11[]</p>' );
  323. expect( command.value ).to.be.undefined;
  324. } );
  325. } );
  326. describe( 'execute()', () => {
  327. it( 'should merge table cells ', () => {
  328. setData( model, modelTable( [
  329. [ '00', '01[]' ],
  330. [ '10', '11' ]
  331. ] ) );
  332. command.execute();
  333. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  334. [ '00', { rowspan: 2, contents: '[0111]' } ],
  335. [ '10' ]
  336. ] ) );
  337. } );
  338. it( 'should remove empty row if merging table cells ', () => {
  339. setData( model, modelTable( [
  340. [ { rowspan: 2, contents: '00' }, '01[]', { rowspan: 3, contents: '02' } ],
  341. [ '11' ],
  342. [ '20', '21' ]
  343. ] ) );
  344. command.execute();
  345. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  346. [ '00', '[0111]', { rowspan: 2, contents: '02' } ],
  347. [ '20', '21' ]
  348. ] ) );
  349. } );
  350. it( 'should not reduce rowspan on cells above removed empty row when merging table cells ', () => {
  351. setData( model, modelTable( [
  352. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  353. [ '11', '12' ],
  354. [ { rowspan: 2, contents: '20' }, '21[]', { rowspan: 3, contents: '22' } ],
  355. [ '31' ],
  356. [ '40', '41' ]
  357. ] ) );
  358. command.execute();
  359. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  360. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  361. [ '11', '12' ],
  362. [ '20', '[2131]', { rowspan: 2, contents: '22' } ],
  363. [ '40', '41' ]
  364. ] ) );
  365. } );
  366. } );
  367. } );
  368. describe( 'direction=up', () => {
  369. beforeEach( () => {
  370. command = new MergeCellCommand( editor, { direction: 'up' } );
  371. } );
  372. describe( 'isEnabled', () => {
  373. it( 'should be true if in cell that has mergeable cell in previous row', () => {
  374. setData( model, modelTable( [
  375. [ '00', '01' ],
  376. [ '10', '11[]' ]
  377. ] ) );
  378. expect( command.isEnabled ).to.be.true;
  379. } );
  380. it( 'should be false if in first row', () => {
  381. setData( model, modelTable( [
  382. [ '00[]', '01' ],
  383. [ '10', '11' ]
  384. ] ) );
  385. expect( command.isEnabled ).to.be.false;
  386. } );
  387. it( 'should be true if in a cell that has mergeable cell with the same colspan', () => {
  388. setData( model, modelTable( [
  389. [ { colspan: 2, contents: '00' }, '02' ],
  390. [ { colspan: 2, contents: '01[]' }, '12' ]
  391. ] ) );
  392. expect( command.isEnabled ).to.be.true;
  393. } );
  394. it( 'should be false if in a cell that potential mergeable cell has different colspan', () => {
  395. setData( model, modelTable( [
  396. [ { colspan: 2, contents: '00' }, '02' ],
  397. [ { colspan: 3, contents: '01[]' } ]
  398. ] ) );
  399. expect( command.isEnabled ).to.be.false;
  400. } );
  401. it( 'should be false if not in a cell', () => {
  402. setData( model, '<p>11[]</p>' );
  403. expect( command.isEnabled ).to.be.false;
  404. } );
  405. it( 'should be false if mergeable cell is in other table section then current cell', () => {
  406. setData( model, modelTable( [
  407. [ '00', '01' ],
  408. [ '10[]', '11' ]
  409. ], { headingRows: 1 } ) );
  410. expect( command.isEnabled ).to.be.false;
  411. } );
  412. } );
  413. describe( 'value', () => {
  414. it( 'should be set to mergeable cell', () => {
  415. setData( model, modelTable( [
  416. [ '00', '01' ],
  417. [ '10', '11[]' ]
  418. ] ) );
  419. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 1 ] ) );
  420. } );
  421. it( 'should be undefined if in first row', () => {
  422. setData( model, modelTable( [
  423. [ '00[]', '01' ],
  424. [ '10', '11' ]
  425. ] ) );
  426. expect( command.value ).to.be.undefined;
  427. } );
  428. it( 'should be set to mergeable cell with the same rowspan', () => {
  429. setData( model, modelTable( [
  430. [ { colspan: 2, contents: '00' }, '02' ],
  431. [ { colspan: 2, contents: '01[]' }, '12' ]
  432. ] ) );
  433. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 0 ] ) );
  434. } );
  435. it( 'should be set to mergeable cell in rows with spanned cells', () => {
  436. setData( model, modelTable( [
  437. [ { rowspan: 3, contents: '00' }, '11', '12', '13' ],
  438. [ { rowspan: 2, contents: '21' }, '22', '23' ],
  439. [ '32', { rowspan: 2, contents: '33[]' } ],
  440. [ { colspan: 2, contents: '40' }, '42' ]
  441. ] ) );
  442. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 1, 2 ] ) );
  443. } );
  444. it( 'should be undefined if in a cell that potential mergeable cell has different rowspan', () => {
  445. setData( model, modelTable( [
  446. [ { colspan: 2, contents: '00' }, '02' ],
  447. [ { colspan: 3, contents: '01[]' } ]
  448. ] ) );
  449. expect( command.value ).to.be.undefined;
  450. } );
  451. it( 'should be undefined if not in a cell', () => {
  452. setData( model, '<p>11[]</p>' );
  453. expect( command.value ).to.be.undefined;
  454. } );
  455. } );
  456. describe( 'execute()', () => {
  457. it( 'should merge table cells', () => {
  458. setData( model, modelTable( [
  459. [ '00', '01' ],
  460. [ '10', '[]11' ]
  461. ] ) );
  462. command.execute();
  463. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  464. [ '00', { rowspan: 2, contents: '[0111]' } ],
  465. [ '10' ]
  466. ] ) );
  467. } );
  468. it( 'should properly merge cells in rows with spaned cells', () => {
  469. setData( model, modelTable( [
  470. [ { rowspan: 3, contents: '00' }, '11', '12', '13' ],
  471. [ { rowspan: 2, contents: '21' }, '22', '23' ],
  472. [ '32', { rowspan: 2, contents: '33[]' } ],
  473. [ { colspan: 2, contents: '40' }, '42' ]
  474. ] ) );
  475. command.execute();
  476. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  477. [ { rowspan: 3, contents: '00' }, '11', '12', '13' ],
  478. [ { rowspan: 2, contents: '21' }, '22', { rowspan: 3, contents: '[2333]' } ],
  479. [ '32' ],
  480. [ { colspan: 2, contents: '40' }, '42' ]
  481. ] ) );
  482. } );
  483. it( 'should remove empty row if merging table cells ', () => {
  484. setData( model, modelTable( [
  485. [ { rowspan: 2, contents: '00' }, '01', { rowspan: 3, contents: '02' } ],
  486. [ '11[]' ],
  487. [ '20', '21' ]
  488. ] ) );
  489. command.execute();
  490. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  491. [ '00', '[0111]', { rowspan: 2, contents: '02' } ],
  492. [ '20', '21' ]
  493. ] ) );
  494. } );
  495. it( 'should not reduce rowspan on cells above removed empty row when merging table cells ', () => {
  496. setData( model, modelTable( [
  497. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  498. [ '11', '12' ],
  499. [ { rowspan: 2, contents: '20' }, '21', { rowspan: 3, contents: '22' } ],
  500. [ '31[]' ],
  501. [ '40', '41' ]
  502. ] ) );
  503. command.execute();
  504. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  505. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  506. [ '11', '12' ],
  507. [ '20', '[2131]', { rowspan: 2, contents: '22' } ],
  508. [ '40', '41' ]
  509. ] ) );
  510. } );
  511. } );
  512. } );
  513. } );