removecolumncommand.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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 RemoveColumnCommand from '../../src/commands/removecolumncommand';
  8. import TableSelection from '../../src/tableselection';
  9. import { defaultConversion, defaultSchema, modelTable } from '../_utils/utils';
  10. import TableUtils from '../../src/tableutils';
  11. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  12. describe( 'RemoveColumnCommand', () => {
  13. let editor, model, command;
  14. beforeEach( () => {
  15. return ModelTestEditor
  16. .create( {
  17. plugins: [ TableUtils, TableSelection ]
  18. } )
  19. .then( newEditor => {
  20. editor = newEditor;
  21. model = editor.model;
  22. command = new RemoveColumnCommand( editor );
  23. defaultSchema( model.schema );
  24. defaultConversion( editor.conversion );
  25. } );
  26. } );
  27. afterEach( () => {
  28. return editor.destroy();
  29. } );
  30. describe( 'isEnabled', () => {
  31. it( 'should be true if selection is inside table cell', () => {
  32. setData( model, modelTable( [
  33. [ '00[]', '01' ],
  34. [ '10', '11' ]
  35. ] ) );
  36. expect( command.isEnabled ).to.be.true;
  37. } );
  38. it( 'should be true if selection contains multiple cells', () => {
  39. setData( model, modelTable( [
  40. [ '00', '01', '02' ],
  41. [ '10', '11', '12' ]
  42. ] ) );
  43. const tableSelection = editor.plugins.get( TableSelection );
  44. const modelRoot = model.document.getRoot();
  45. tableSelection._setCellSelection(
  46. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  47. modelRoot.getNodeByPath( [ 0, 0, 1 ] )
  48. );
  49. expect( command.isEnabled ).to.be.true;
  50. } );
  51. it( 'should be false if selection is inside table with one column only', () => {
  52. setData( model, modelTable( [
  53. [ '00' ],
  54. [ '10[]' ],
  55. [ '20[]' ]
  56. ] ) );
  57. expect( command.isEnabled ).to.be.false;
  58. } );
  59. it( 'should be false if all columns are selected', () => {
  60. setData( model, modelTable( [
  61. [ '00', '01', '02' ],
  62. [ '10', '11', '12' ]
  63. ] ) );
  64. const tableSelection = editor.plugins.get( TableSelection );
  65. const modelRoot = model.document.getRoot();
  66. tableSelection._setCellSelection(
  67. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  68. modelRoot.getNodeByPath( [ 0, 0, 2 ] )
  69. );
  70. expect( command.isEnabled ).to.be.false;
  71. } );
  72. it( 'should be false if selection is outside a table', () => {
  73. setData( model, '<paragraph>11[]</paragraph>' );
  74. expect( command.isEnabled ).to.be.false;
  75. } );
  76. } );
  77. describe( 'execute()', () => {
  78. it( 'should remove a given column', () => {
  79. setData( model, modelTable( [
  80. [ '00', '01', '02' ],
  81. [ '10', '[]11', '12' ],
  82. [ '20', '21', '22' ]
  83. ] ) );
  84. command.execute();
  85. assertEqualMarkup( getData( model ), modelTable( [
  86. [ '00', '02' ],
  87. [ '10', '[]12' ],
  88. [ '20', '22' ]
  89. ] ) );
  90. } );
  91. it( 'should remove a given column from a table start', () => {
  92. setData( model, modelTable( [
  93. [ '[]00', '01' ],
  94. [ '10', '11' ],
  95. [ '20', '21' ]
  96. ] ) );
  97. command.execute();
  98. assertEqualMarkup( getData( model ), modelTable( [
  99. [ '[]01' ],
  100. [ '11' ],
  101. [ '21' ]
  102. ] ) );
  103. } );
  104. describe( 'with multiple cells selected', () => {
  105. it( 'should properly remove the first column', () => {
  106. setData( model, modelTable( [
  107. [ '00', '01' ],
  108. [ '10', '11' ],
  109. [ '20', '21' ],
  110. [ '30', '31' ]
  111. ] ) );
  112. const tableSelection = editor.plugins.get( TableSelection );
  113. const modelRoot = model.document.getRoot();
  114. tableSelection._setCellSelection(
  115. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  116. modelRoot.getNodeByPath( [ 0, 2, 0 ] )
  117. );
  118. command.execute();
  119. assertEqualMarkup( getData( model ), modelTable( [
  120. [ '01' ],
  121. [ '11' ],
  122. [ '[]21' ],
  123. [ '31' ]
  124. ] ) );
  125. } );
  126. it( 'should properly remove a middle column', () => {
  127. setData( model, modelTable( [
  128. [ '00', '01', '02' ],
  129. [ '10', '11', '12' ],
  130. [ '20', '21', '22' ],
  131. [ '30', '31', '32' ]
  132. ] ) );
  133. const tableSelection = editor.plugins.get( TableSelection );
  134. const modelRoot = model.document.getRoot();
  135. tableSelection._setCellSelection(
  136. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  137. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  138. );
  139. command.execute();
  140. assertEqualMarkup( getData( model ), modelTable( [
  141. [ '00', '02' ],
  142. [ '10', '12' ],
  143. [ '20', '[]22' ],
  144. [ '30', '32' ]
  145. ] ) );
  146. } );
  147. it( 'should properly remove the last column', () => {
  148. setData( model, modelTable( [
  149. [ '00', '01' ],
  150. [ '10', '11' ],
  151. [ '20', '21' ],
  152. [ '30', '31' ]
  153. ] ) );
  154. const tableSelection = editor.plugins.get( TableSelection );
  155. const modelRoot = model.document.getRoot();
  156. tableSelection._setCellSelection(
  157. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  158. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  159. );
  160. command.execute();
  161. assertEqualMarkup( getData( model ), modelTable( [
  162. [ '00' ],
  163. [ '[]10' ],
  164. [ '20' ],
  165. [ '30' ]
  166. ] ) );
  167. } );
  168. it( 'should properly remove two first columns', () => {
  169. setData( model, modelTable( [
  170. [ '00', '01', '02' ],
  171. [ '10', '11', '12' ],
  172. [ '20', '21', '22' ],
  173. [ '30', '31', '32' ]
  174. ] ) );
  175. const tableSelection = editor.plugins.get( TableSelection );
  176. const modelRoot = model.document.getRoot();
  177. tableSelection._setCellSelection(
  178. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  179. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  180. );
  181. command.execute();
  182. assertEqualMarkup( getData( model ), modelTable( [
  183. [ '02' ],
  184. [ '[]12' ],
  185. [ '22' ],
  186. [ '32' ]
  187. ] ) );
  188. } );
  189. it( 'should properly remove two middle columns', () => {
  190. setData( model, modelTable( [
  191. [ '00', '01', '02', '03' ],
  192. [ '10', '11', '12', '13' ],
  193. [ '20', '21', '22', '23' ],
  194. [ '30', '31', '32', '33' ]
  195. ] ) );
  196. const tableSelection = editor.plugins.get( TableSelection );
  197. const modelRoot = model.document.getRoot();
  198. tableSelection._setCellSelection(
  199. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  200. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  201. );
  202. command.execute();
  203. assertEqualMarkup( getData( model ), modelTable( [
  204. [ '00', '03' ],
  205. [ '10', '13' ],
  206. [ '20', '[]23' ],
  207. [ '30', '33' ]
  208. ] ) );
  209. } );
  210. it( 'should properly remove two middle columns with reversed selection', () => {
  211. setData( model, modelTable( [
  212. [ '00', '01', '02', '03' ],
  213. [ '10', '11', '12', '13' ],
  214. [ '20', '21', '22', '23' ],
  215. [ '30', '31', '32', '33' ]
  216. ] ) );
  217. const tableSelection = editor.plugins.get( TableSelection );
  218. const modelRoot = model.document.getRoot();
  219. tableSelection._setCellSelection(
  220. modelRoot.getNodeByPath( [ 0, 2, 2 ] ),
  221. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  222. );
  223. command.execute();
  224. assertEqualMarkup( getData( model ), modelTable( [
  225. [ '00', '03' ],
  226. [ '10', '13' ],
  227. [ '20', '[]23' ],
  228. [ '30', '33' ]
  229. ] ) );
  230. } );
  231. it( 'should properly remove two last columns', () => {
  232. // There's no handling for selection in case like that.
  233. setData( model, modelTable( [
  234. [ '00', '01', '02' ],
  235. [ '10', '11', '12' ],
  236. [ '20', '21', '22' ],
  237. [ '30', '31', '32' ]
  238. ] ) );
  239. const tableSelection = editor.plugins.get( TableSelection );
  240. const modelRoot = model.document.getRoot();
  241. tableSelection._setCellSelection(
  242. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  243. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  244. );
  245. command.execute();
  246. assertEqualMarkup( getData( model ), modelTable( [
  247. [ '00' ],
  248. [ '[]10' ],
  249. [ '20' ],
  250. [ '30' ]
  251. ] ) );
  252. } );
  253. it( 'should properly remove multiple heading columns', () => {
  254. // There's no handling for selection in case like that.
  255. setData( model, modelTable( [
  256. [ '00', '01', '02', '03', '04' ],
  257. [ '10', '11', '12', '13', '14' ]
  258. ], { headingColumns: 3 } ) );
  259. const tableSelection = editor.plugins.get( TableSelection );
  260. const modelRoot = model.document.getRoot();
  261. tableSelection._setCellSelection(
  262. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  263. modelRoot.getNodeByPath( [ 0, 1, 3 ] )
  264. );
  265. command.execute();
  266. assertEqualMarkup( getData( model ), modelTable( [
  267. [ '00', '04' ],
  268. [ '10', '[]14' ]
  269. ], { headingColumns: 1 } ) );
  270. } );
  271. } );
  272. it( 'should change heading columns if removing a heading column', () => {
  273. setData( model, modelTable( [
  274. [ '00', '01' ],
  275. [ '[]10', '11' ],
  276. [ '20', '21' ]
  277. ], { headingColumns: 2 } ) );
  278. command.execute();
  279. assertEqualMarkup( getData( model ), modelTable( [
  280. [ '01' ],
  281. [ '[]11' ],
  282. [ '21' ]
  283. ], { headingColumns: 1 } ) );
  284. } );
  285. it( 'should decrease colspan of table cells from previous column', () => {
  286. setData( model, modelTable( [
  287. [ { colspan: 4, contents: '00' }, '03' ],
  288. [ { colspan: 3, contents: '10' }, '13' ],
  289. [ { colspan: 2, contents: '20' }, '22[]', '23' ],
  290. [ '30', { colspan: 2, contents: '31' }, '33' ],
  291. [ '40', '41', '42', '43' ]
  292. ] ) );
  293. command.execute();
  294. assertEqualMarkup( getData( model ), modelTable( [
  295. [ { colspan: 3, contents: '00' }, '03' ],
  296. [ { colspan: 2, contents: '10' }, '13' ],
  297. [ { colspan: 2, contents: '20' }, '[]23' ],
  298. [ '30', '31', '33' ],
  299. [ '40', '41', '43' ]
  300. ] ) );
  301. } );
  302. it( 'should decrease colspan of cells that are on removed column', () => {
  303. setData( model, modelTable( [
  304. [ { colspan: 3, contents: '[]00' }, '03' ],
  305. [ { colspan: 2, contents: '10' }, '13' ],
  306. [ '20', '21', '22', '23' ]
  307. ] ) );
  308. command.execute();
  309. assertEqualMarkup( getData( model ), modelTable( [
  310. [ { colspan: 2, contents: '[]00' }, '03' ],
  311. [ '10', '13' ],
  312. [ '21', '22', '23' ]
  313. ] ) );
  314. } );
  315. it( 'should move focus to previous column of removed cell if in last column', () => {
  316. setData( model, modelTable( [
  317. [ '00', '01', '02' ],
  318. [ '10', '11', '12[]' ],
  319. [ '20', '21', '22' ]
  320. ] ) );
  321. command.execute();
  322. assertEqualMarkup( getData( model ), modelTable( [
  323. [ '00', '01' ],
  324. [ '10', '[]11' ],
  325. [ '20', '21' ]
  326. ] ) );
  327. } );
  328. it( 'should work property if the rowspan is in the first column (#1)', () => {
  329. setData( model, modelTable( [
  330. [ { rowspan: 2, contents: '00' }, '[]01' ],
  331. [ '10' ]
  332. ] ) );
  333. command.execute();
  334. assertEqualMarkup( getData( model ), modelTable( [
  335. [ { rowspan: 2, contents: '[]00' } ]
  336. ] ) );
  337. } );
  338. it( 'should work property if the rowspan is in the first column (#2)', () => {
  339. setData( model, modelTable( [
  340. [ { rowspan: 2, contents: '00' }, '01' ],
  341. [ '[]10' ]
  342. ] ) );
  343. command.execute();
  344. assertEqualMarkup( getData( model ), modelTable( [
  345. [ { rowspan: 2, contents: '[]00' } ]
  346. ] ) );
  347. } );
  348. it( 'should work property if the rowspan is in the first column (#3)', () => {
  349. setData( model, modelTable( [
  350. [ { rowspan: 2, contents: '00[]' }, '01' ],
  351. [ '10' ]
  352. ] ) );
  353. command.execute();
  354. assertEqualMarkup( getData( model ), modelTable( [
  355. [ '[]01' ],
  356. [ '10' ]
  357. ] ) );
  358. } );
  359. it( 'should work property if the rowspan is in the last column (#1)', () => {
  360. setData( model, modelTable( [
  361. [ '[]00', { rowspan: 2, contents: '01' } ],
  362. [ '10' ]
  363. ] ) );
  364. command.execute();
  365. assertEqualMarkup( getData( model ), modelTable( [
  366. [ { rowspan: 2, contents: '[]01' } ]
  367. ] ) );
  368. } );
  369. it( 'should work property if the rowspan is in the last column (#2)', () => {
  370. setData( model, modelTable( [
  371. [ '00', { rowspan: 2, contents: '01' } ],
  372. [ '[]10' ]
  373. ] ) );
  374. command.execute();
  375. assertEqualMarkup( getData( model ), modelTable( [
  376. [ { rowspan: 2, contents: '[]01' } ]
  377. ] ) );
  378. } );
  379. it( 'should work property if the rowspan is in the last column (#3)', () => {
  380. setData( model, modelTable( [
  381. [ '00', { rowspan: 2, contents: '[]01' } ],
  382. [ '10' ]
  383. ] ) );
  384. command.execute();
  385. assertEqualMarkup( getData( model ), modelTable( [
  386. [ '[]00' ],
  387. [ '10' ]
  388. ] ) );
  389. } );
  390. } );
  391. } );