removecolumncommand.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  46. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  47. expect( command.isEnabled ).to.be.true;
  48. } );
  49. it( 'should be false if selection is inside table with one column only', () => {
  50. setData( model, modelTable( [
  51. [ '00' ],
  52. [ '10[]' ],
  53. [ '20[]' ]
  54. ] ) );
  55. expect( command.isEnabled ).to.be.false;
  56. } );
  57. it( 'should be false if all columns are selected', () => {
  58. setData( model, modelTable( [
  59. [ '00', '01', '02' ],
  60. [ '10', '11', '12' ]
  61. ] ) );
  62. const tableSelection = editor.plugins.get( TableSelection );
  63. const modelRoot = model.document.getRoot();
  64. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  65. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 2 ] ) );
  66. expect( command.isEnabled ).to.be.false;
  67. } );
  68. it( 'should be false if selection is outside a table', () => {
  69. setData( model, '<paragraph>11[]</paragraph>' );
  70. expect( command.isEnabled ).to.be.false;
  71. } );
  72. } );
  73. describe( 'execute()', () => {
  74. it( 'should remove a given column', () => {
  75. setData( model, modelTable( [
  76. [ '00', '01', '02' ],
  77. [ '10', '[]11', '12' ],
  78. [ '20', '21', '22' ]
  79. ] ) );
  80. command.execute();
  81. assertEqualMarkup( getData( model ), modelTable( [
  82. [ '00', '02' ],
  83. [ '10', '[]12' ],
  84. [ '20', '22' ]
  85. ] ) );
  86. } );
  87. it( 'should remove a given column from a table start', () => {
  88. setData( model, modelTable( [
  89. [ '[]00', '01' ],
  90. [ '10', '11' ],
  91. [ '20', '21' ]
  92. ] ) );
  93. command.execute();
  94. assertEqualMarkup( getData( model ), modelTable( [
  95. [ '[]01' ],
  96. [ '11' ],
  97. [ '21' ]
  98. ] ) );
  99. } );
  100. describe( 'with multiple cells selected', () => {
  101. it( 'should properly remove the first column', () => {
  102. setData( model, modelTable( [
  103. [ '00', '01' ],
  104. [ '10', '11' ],
  105. [ '20', '21' ],
  106. [ '30', '31' ]
  107. ] ) );
  108. const tableSelection = editor.plugins.get( TableSelection );
  109. const modelRoot = model.document.getRoot();
  110. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 1, 0 ] ) );
  111. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 2, 0 ] ) );
  112. command.execute();
  113. assertEqualMarkup( getData( model ), modelTable( [
  114. [ '01' ],
  115. [ '11' ],
  116. [ '[]21' ],
  117. [ '31' ]
  118. ] ) );
  119. } );
  120. it( 'should properly remove a middle column', () => {
  121. setData( model, modelTable( [
  122. [ '00', '01', '02' ],
  123. [ '10', '11', '12' ],
  124. [ '20', '21', '22' ],
  125. [ '30', '31', '32' ]
  126. ] ) );
  127. const tableSelection = editor.plugins.get( TableSelection );
  128. const modelRoot = model.document.getRoot();
  129. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
  130. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 2, 1 ] ) );
  131. command.execute();
  132. assertEqualMarkup( getData( model ), modelTable( [
  133. [ '00', '02' ],
  134. [ '10', '12' ],
  135. [ '20', '[]22' ],
  136. [ '30', '32' ]
  137. ] ) );
  138. } );
  139. it( 'should properly remove the last column', () => {
  140. setData( model, modelTable( [
  141. [ '00', '01' ],
  142. [ '10', '11' ],
  143. [ '20', '21' ],
  144. [ '30', '31' ]
  145. ] ) );
  146. const tableSelection = editor.plugins.get( TableSelection );
  147. const modelRoot = model.document.getRoot();
  148. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
  149. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 2, 1 ] ) );
  150. command.execute();
  151. assertEqualMarkup( getData( model ), modelTable( [
  152. [ '00' ],
  153. [ '[]10' ],
  154. [ '20' ],
  155. [ '30' ]
  156. ] ) );
  157. } );
  158. it( 'should properly remove two first columns', () => {
  159. setData( model, modelTable( [
  160. [ '00', '01', '02' ],
  161. [ '10', '11', '12' ],
  162. [ '20', '21', '22' ],
  163. [ '30', '31', '32' ]
  164. ] ) );
  165. const tableSelection = editor.plugins.get( TableSelection );
  166. const modelRoot = model.document.getRoot();
  167. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 1, 0 ] ) );
  168. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
  169. command.execute();
  170. assertEqualMarkup( getData( model ), modelTable( [
  171. [ '02' ],
  172. [ '[]12' ],
  173. [ '22' ],
  174. [ '32' ]
  175. ] ) );
  176. } );
  177. it( 'should properly remove two middle columns', () => {
  178. setData( model, modelTable( [
  179. [ '00', '01', '02', '03' ],
  180. [ '10', '11', '12', '13' ],
  181. [ '20', '21', '22', '23' ],
  182. [ '30', '31', '32', '33' ]
  183. ] ) );
  184. const tableSelection = editor.plugins.get( TableSelection );
  185. const modelRoot = model.document.getRoot();
  186. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
  187. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 2, 2 ] ) );
  188. command.execute();
  189. assertEqualMarkup( getData( model ), modelTable( [
  190. [ '00', '03' ],
  191. [ '10', '13' ],
  192. [ '20', '[]23' ],
  193. [ '30', '33' ]
  194. ] ) );
  195. } );
  196. it( 'should properly remove two last columns', () => {
  197. // There's no handling for selection in case like that.
  198. setData( model, modelTable( [
  199. [ '00', '01', '02' ],
  200. [ '10', '11', '12' ],
  201. [ '20', '21', '22' ],
  202. [ '30', '31', '32' ]
  203. ] ) );
  204. const tableSelection = editor.plugins.get( TableSelection );
  205. const modelRoot = model.document.getRoot();
  206. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
  207. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 2, 2 ] ) );
  208. command.execute();
  209. assertEqualMarkup( getData( model ), modelTable( [
  210. [ '00' ],
  211. [ '[]10' ],
  212. [ '20' ],
  213. [ '30' ]
  214. ] ) );
  215. } );
  216. it( 'should properly remove multiple heading columns', () => {
  217. // There's no handling for selection in case like that.
  218. setData( model, modelTable( [
  219. [ '00', '01', '02', '03', '04' ],
  220. [ '10', '11', '12', '13', '14' ]
  221. ], { headingColumns: 3 } ) );
  222. const tableSelection = editor.plugins.get( TableSelection );
  223. const modelRoot = model.document.getRoot();
  224. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  225. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 3 ] ) );
  226. command.execute();
  227. assertEqualMarkup( getData( model ), modelTable( [
  228. [ '00', '04' ],
  229. [ '10', '[]14' ]
  230. ], { headingColumns: 1 } ) );
  231. } );
  232. } );
  233. it( 'should change heading columns if removing a heading column', () => {
  234. setData( model, modelTable( [
  235. [ '00', '01' ],
  236. [ '[]10', '11' ],
  237. [ '20', '21' ]
  238. ], { headingColumns: 2 } ) );
  239. command.execute();
  240. assertEqualMarkup( getData( model ), modelTable( [
  241. [ '01' ],
  242. [ '[]11' ],
  243. [ '21' ]
  244. ], { headingColumns: 1 } ) );
  245. } );
  246. it( 'should decrease colspan of table cells from previous column', () => {
  247. setData( model, modelTable( [
  248. [ { colspan: 4, contents: '00' }, '03' ],
  249. [ { colspan: 3, contents: '10' }, '13' ],
  250. [ { colspan: 2, contents: '20' }, '22[]', '23' ],
  251. [ '30', { colspan: 2, contents: '31' }, '33' ],
  252. [ '40', '41', '42', '43' ]
  253. ] ) );
  254. command.execute();
  255. assertEqualMarkup( getData( model ), modelTable( [
  256. [ { colspan: 3, contents: '00' }, '03' ],
  257. [ { colspan: 2, contents: '10' }, '13' ],
  258. [ { colspan: 2, contents: '20' }, '[]23' ],
  259. [ '30', '31', '33' ],
  260. [ '40', '41', '43' ]
  261. ] ) );
  262. } );
  263. it( 'should decrease colspan of cells that are on removed column', () => {
  264. setData( model, modelTable( [
  265. [ { colspan: 3, contents: '[]00' }, '03' ],
  266. [ { colspan: 2, contents: '10' }, '13' ],
  267. [ '20', '21', '22', '23' ]
  268. ] ) );
  269. command.execute();
  270. assertEqualMarkup( getData( model ), modelTable( [
  271. [ { colspan: 2, contents: '[]00' }, '03' ],
  272. [ '10', '13' ],
  273. [ '21', '22', '23' ]
  274. ] ) );
  275. } );
  276. it( 'should move focus to previous column of removed cell if in last column', () => {
  277. setData( model, modelTable( [
  278. [ '00', '01', '02' ],
  279. [ '10', '11', '12[]' ],
  280. [ '20', '21', '22' ]
  281. ] ) );
  282. command.execute();
  283. assertEqualMarkup( getData( model ), modelTable( [
  284. [ '00', '01' ],
  285. [ '10', '[]11' ],
  286. [ '20', '21' ]
  287. ] ) );
  288. } );
  289. } );
  290. } );