mergecellcommand.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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 mergable cell is in other table section', () => {
  322. setData( model, modelTable( [
  323. [ { rowspan: 2, contents: '00[]' }, '02' ],
  324. [ '12' ],
  325. [ '21', '22' ]
  326. ], { headingRows: 2 } ) );
  327. expect( command.value ).to.be.undefined;
  328. } );
  329. it( 'should be undefined if not in a cell', () => {
  330. setData( model, '<p>11[]</p>' );
  331. expect( command.value ).to.be.undefined;
  332. } );
  333. } );
  334. describe( 'execute()', () => {
  335. it( 'should merge table cells ', () => {
  336. setData( model, modelTable( [
  337. [ '00', '01[]' ],
  338. [ '10', '11' ]
  339. ] ) );
  340. command.execute();
  341. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  342. [ '00', { rowspan: 2, contents: '[0111]' } ],
  343. [ '10' ]
  344. ] ) );
  345. } );
  346. it( 'should remove empty row if merging table cells ', () => {
  347. setData( model, modelTable( [
  348. [ { rowspan: 2, contents: '00' }, '01[]', { rowspan: 3, contents: '02' } ],
  349. [ '11' ],
  350. [ '20', '21' ]
  351. ] ) );
  352. command.execute();
  353. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  354. [ '00', '[0111]', { rowspan: 2, contents: '02' } ],
  355. [ '20', '21' ]
  356. ] ) );
  357. } );
  358. it( 'should not reduce rowspan on cells above removed empty row when merging table cells ', () => {
  359. setData( model, modelTable( [
  360. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  361. [ '11', '12' ],
  362. [ { rowspan: 2, contents: '20' }, '21[]', { rowspan: 3, contents: '22' } ],
  363. [ '31' ],
  364. [ '40', '41' ]
  365. ] ) );
  366. command.execute();
  367. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  368. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  369. [ '11', '12' ],
  370. [ '20', '[2131]', { rowspan: 2, contents: '22' } ],
  371. [ '40', '41' ]
  372. ] ) );
  373. } );
  374. } );
  375. } );
  376. describe( 'direction=up', () => {
  377. beforeEach( () => {
  378. command = new MergeCellCommand( editor, { direction: 'up' } );
  379. } );
  380. describe( 'isEnabled', () => {
  381. it( 'should be true if in cell that has mergeable cell in previous row', () => {
  382. setData( model, modelTable( [
  383. [ '00', '01' ],
  384. [ '10', '11[]' ]
  385. ] ) );
  386. expect( command.isEnabled ).to.be.true;
  387. } );
  388. it( 'should be false if in first row', () => {
  389. setData( model, modelTable( [
  390. [ '00[]', '01' ],
  391. [ '10', '11' ]
  392. ] ) );
  393. expect( command.isEnabled ).to.be.false;
  394. } );
  395. it( 'should be true if in a cell that has mergeable cell with the same colspan', () => {
  396. setData( model, modelTable( [
  397. [ { colspan: 2, contents: '00' }, '02' ],
  398. [ { colspan: 2, contents: '01[]' }, '12' ]
  399. ] ) );
  400. expect( command.isEnabled ).to.be.true;
  401. } );
  402. it( 'should be false if in a cell that potential mergeable cell has different colspan', () => {
  403. setData( model, modelTable( [
  404. [ { colspan: 2, contents: '00' }, '02' ],
  405. [ { colspan: 3, contents: '01[]' } ]
  406. ] ) );
  407. expect( command.isEnabled ).to.be.false;
  408. } );
  409. it( 'should be false if not in a cell', () => {
  410. setData( model, '<p>11[]</p>' );
  411. expect( command.isEnabled ).to.be.false;
  412. } );
  413. it( 'should be false if mergeable cell is in other table section then current cell', () => {
  414. setData( model, modelTable( [
  415. [ '00', '01' ],
  416. [ '10[]', '11' ]
  417. ], { headingRows: 1 } ) );
  418. expect( command.isEnabled ).to.be.false;
  419. } );
  420. } );
  421. describe( 'value', () => {
  422. it( 'should be set to mergeable cell', () => {
  423. setData( model, modelTable( [
  424. [ '00', '01' ],
  425. [ '10', '11[]' ]
  426. ] ) );
  427. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 1 ] ) );
  428. } );
  429. it( 'should be undefined if in first row', () => {
  430. setData( model, modelTable( [
  431. [ '00[]', '01' ],
  432. [ '10', '11' ]
  433. ] ) );
  434. expect( command.value ).to.be.undefined;
  435. } );
  436. it( 'should be set to mergeable cell with the same rowspan', () => {
  437. setData( model, modelTable( [
  438. [ { colspan: 2, contents: '00' }, '02' ],
  439. [ { colspan: 2, contents: '01[]' }, '12' ]
  440. ] ) );
  441. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 0, 0 ] ) );
  442. } );
  443. it( 'should be set to mergeable cell in rows with spanned cells', () => {
  444. setData( model, modelTable( [
  445. [ { rowspan: 3, contents: '00' }, '11', '12', '13' ],
  446. [ { rowspan: 2, contents: '21' }, '22', '23' ],
  447. [ '32', { rowspan: 2, contents: '33[]' } ],
  448. [ { colspan: 2, contents: '40' }, '42' ]
  449. ] ) );
  450. expect( command.value ).to.equal( root.getNodeByPath( [ 0, 1, 2 ] ) );
  451. } );
  452. it( 'should be undefined if in a cell that potential mergeable cell has different rowspan', () => {
  453. setData( model, modelTable( [
  454. [ { colspan: 2, contents: '00' }, '02' ],
  455. [ { colspan: 3, contents: '01[]' } ]
  456. ] ) );
  457. expect( command.value ).to.be.undefined;
  458. } );
  459. it( 'should be undefined if not in a cell', () => {
  460. setData( model, '<p>11[]</p>' );
  461. expect( command.value ).to.be.undefined;
  462. } );
  463. } );
  464. describe( 'execute()', () => {
  465. it( 'should merge table cells', () => {
  466. setData( model, modelTable( [
  467. [ '00', '01' ],
  468. [ '10', '[]11' ]
  469. ] ) );
  470. command.execute();
  471. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  472. [ '00', { rowspan: 2, contents: '[0111]' } ],
  473. [ '10' ]
  474. ] ) );
  475. } );
  476. it( 'should properly merge cells in rows with spaned cells', () => {
  477. setData( model, modelTable( [
  478. [ { rowspan: 3, contents: '00' }, '11', '12', '13' ],
  479. [ { rowspan: 2, contents: '21' }, '22', '23' ],
  480. [ '32', { rowspan: 2, contents: '33[]' } ],
  481. [ { colspan: 2, contents: '40' }, '42' ]
  482. ] ) );
  483. command.execute();
  484. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  485. [ { rowspan: 3, contents: '00' }, '11', '12', '13' ],
  486. [ { rowspan: 2, contents: '21' }, '22', { rowspan: 3, contents: '[2333]' } ],
  487. [ '32' ],
  488. [ { colspan: 2, contents: '40' }, '42' ]
  489. ] ) );
  490. } );
  491. it( 'should remove empty row if merging table cells ', () => {
  492. setData( model, modelTable( [
  493. [ { rowspan: 2, contents: '00' }, '01', { rowspan: 3, contents: '02' } ],
  494. [ '11[]' ],
  495. [ '20', '21' ]
  496. ] ) );
  497. command.execute();
  498. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  499. [ '00', '[0111]', { rowspan: 2, contents: '02' } ],
  500. [ '20', '21' ]
  501. ] ) );
  502. } );
  503. it( 'should not reduce rowspan on cells above removed empty row when merging table cells ', () => {
  504. setData( model, modelTable( [
  505. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  506. [ '11', '12' ],
  507. [ { rowspan: 2, contents: '20' }, '21', { rowspan: 3, contents: '22' } ],
  508. [ '31[]' ],
  509. [ '40', '41' ]
  510. ] ) );
  511. command.execute();
  512. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  513. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  514. [ '11', '12' ],
  515. [ '20', '[2131]', { rowspan: 2, contents: '22' } ],
  516. [ '40', '41' ]
  517. ] ) );
  518. } );
  519. } );
  520. } );
  521. } );