removecolumncommand.js 8.5 KB

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