removecolumncommand.js 14 KB

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