removecolumncommand.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 last columns', () => {
  211. // There's no handling for selection in case like that.
  212. setData( model, modelTable( [
  213. [ '00', '01', '02' ],
  214. [ '10', '11', '12' ],
  215. [ '20', '21', '22' ],
  216. [ '30', '31', '32' ]
  217. ] ) );
  218. const tableSelection = editor.plugins.get( TableSelection );
  219. const modelRoot = model.document.getRoot();
  220. tableSelection._setCellSelection(
  221. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  222. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  223. );
  224. command.execute();
  225. assertEqualMarkup( getData( model ), modelTable( [
  226. [ '00' ],
  227. [ '[]10' ],
  228. [ '20' ],
  229. [ '30' ]
  230. ] ) );
  231. } );
  232. it( 'should properly remove multiple heading columns', () => {
  233. // There's no handling for selection in case like that.
  234. setData( model, modelTable( [
  235. [ '00', '01', '02', '03', '04' ],
  236. [ '10', '11', '12', '13', '14' ]
  237. ], { headingColumns: 3 } ) );
  238. const tableSelection = editor.plugins.get( TableSelection );
  239. const modelRoot = model.document.getRoot();
  240. tableSelection._setCellSelection(
  241. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  242. modelRoot.getNodeByPath( [ 0, 1, 3 ] )
  243. );
  244. command.execute();
  245. assertEqualMarkup( getData( model ), modelTable( [
  246. [ '00', '04' ],
  247. [ '10', '[]14' ]
  248. ], { headingColumns: 1 } ) );
  249. } );
  250. } );
  251. it( 'should change heading columns if removing a heading column', () => {
  252. setData( model, modelTable( [
  253. [ '00', '01' ],
  254. [ '[]10', '11' ],
  255. [ '20', '21' ]
  256. ], { headingColumns: 2 } ) );
  257. command.execute();
  258. assertEqualMarkup( getData( model ), modelTable( [
  259. [ '01' ],
  260. [ '[]11' ],
  261. [ '21' ]
  262. ], { headingColumns: 1 } ) );
  263. } );
  264. it( 'should decrease colspan of table cells from previous column', () => {
  265. setData( model, modelTable( [
  266. [ { colspan: 4, contents: '00' }, '03' ],
  267. [ { colspan: 3, contents: '10' }, '13' ],
  268. [ { colspan: 2, contents: '20' }, '22[]', '23' ],
  269. [ '30', { colspan: 2, contents: '31' }, '33' ],
  270. [ '40', '41', '42', '43' ]
  271. ] ) );
  272. command.execute();
  273. assertEqualMarkup( getData( model ), modelTable( [
  274. [ { colspan: 3, contents: '00' }, '03' ],
  275. [ { colspan: 2, contents: '10' }, '13' ],
  276. [ { colspan: 2, contents: '20' }, '[]23' ],
  277. [ '30', '31', '33' ],
  278. [ '40', '41', '43' ]
  279. ] ) );
  280. } );
  281. it( 'should decrease colspan of cells that are on removed column', () => {
  282. setData( model, modelTable( [
  283. [ { colspan: 3, contents: '[]00' }, '03' ],
  284. [ { colspan: 2, contents: '10' }, '13' ],
  285. [ '20', '21', '22', '23' ]
  286. ] ) );
  287. command.execute();
  288. assertEqualMarkup( getData( model ), modelTable( [
  289. [ { colspan: 2, contents: '[]00' }, '03' ],
  290. [ '10', '13' ],
  291. [ '21', '22', '23' ]
  292. ] ) );
  293. } );
  294. it( 'should move focus to previous column of removed cell if in last column', () => {
  295. setData( model, modelTable( [
  296. [ '00', '01', '02' ],
  297. [ '10', '11', '12[]' ],
  298. [ '20', '21', '22' ]
  299. ] ) );
  300. command.execute();
  301. assertEqualMarkup( getData( model ), modelTable( [
  302. [ '00', '01' ],
  303. [ '10', '[]11' ],
  304. [ '20', '21' ]
  305. ] ) );
  306. } );
  307. } );
  308. } );