mergecellcommand.js 13 KB

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