8
0

removecolumncommand.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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, 1, 1 ] ) );
  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, 2 ] ) );
  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. // There's no handling for selection in case like that.
  187. setData( model, modelTable( [
  188. [ '00', '01', '02' ],
  189. [ '10', '11', '12' ],
  190. [ '20', '21', '22' ],
  191. [ '30', '31', '32' ]
  192. ] ) );
  193. const tableSelection = editor.plugins.get( TableSelection );
  194. const modelRoot = model.document.getRoot();
  195. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
  196. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 2, 2 ] ) );
  197. command.execute();
  198. assertEqualMarkup( getData( model ), modelTable( [
  199. [ '00' ],
  200. [ '[]10' ],
  201. [ '20' ],
  202. [ '30' ]
  203. ] ) );
  204. } );
  205. } );
  206. it( 'should change heading columns if removing a heading column', () => {
  207. setData( model, modelTable( [
  208. [ '00', '01' ],
  209. [ '[]10', '11' ],
  210. [ '20', '21' ]
  211. ], { headingColumns: 2 } ) );
  212. command.execute();
  213. assertEqualMarkup( getData( model ), modelTable( [
  214. [ '01' ],
  215. [ '[]11' ],
  216. [ '21' ]
  217. ], { headingColumns: 1 } ) );
  218. } );
  219. it( 'should decrease colspan of table cells from previous column', () => {
  220. setData( model, modelTable( [
  221. [ { colspan: 4, contents: '00' }, '03' ],
  222. [ { colspan: 3, contents: '10' }, '13' ],
  223. [ { colspan: 2, contents: '20' }, '22[]', '23' ],
  224. [ '30', { colspan: 2, contents: '31' }, '33' ],
  225. [ '40', '41', '42', '43' ]
  226. ] ) );
  227. command.execute();
  228. assertEqualMarkup( getData( model ), modelTable( [
  229. [ { colspan: 3, contents: '00' }, '03' ],
  230. [ { colspan: 2, contents: '10' }, '13' ],
  231. [ { colspan: 2, contents: '20' }, '[]23' ],
  232. [ '30', '31', '33' ],
  233. [ '40', '41', '43' ]
  234. ] ) );
  235. } );
  236. it( 'should decrease colspan of cells that are on removed column', () => {
  237. setData( model, modelTable( [
  238. [ { colspan: 3, contents: '[]00' }, '03' ],
  239. [ { colspan: 2, contents: '10' }, '13' ],
  240. [ '20', '21', '22', '23' ]
  241. ] ) );
  242. command.execute();
  243. assertEqualMarkup( getData( model ), modelTable( [
  244. [ { colspan: 2, contents: '[]00' }, '03' ],
  245. [ '10', '13' ],
  246. [ '21', '22', '23' ]
  247. ] ) );
  248. } );
  249. it( 'should move focus to previous column of removed cell if in last column', () => {
  250. setData( model, modelTable( [
  251. [ '00', '01', '02' ],
  252. [ '10', '11', '12[]' ],
  253. [ '20', '21', '22' ]
  254. ] ) );
  255. command.execute();
  256. assertEqualMarkup( getData( model ), modelTable( [
  257. [ '00', '01' ],
  258. [ '10', '[]11' ],
  259. [ '20', '21' ]
  260. ] ) );
  261. } );
  262. } );
  263. } );