removecolumncommand.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. it( 'should change heading columns if removing a heading column', () => {
  90. setData( model, modelTable( [
  91. [ '00', '01' ],
  92. [ '[]10', '11' ],
  93. [ '20', '21' ]
  94. ], { headingColumns: 2 } ) );
  95. command.execute();
  96. assertEqualMarkup( getData( model ), modelTable( [
  97. [ '01' ],
  98. [ '[]11' ],
  99. [ '21' ]
  100. ], { headingColumns: 1 } ) );
  101. } );
  102. it( 'should decrease colspan of table cells from previous column', () => {
  103. setData( model, modelTable( [
  104. [ { colspan: 4, contents: '00' }, '03' ],
  105. [ { colspan: 3, contents: '10' }, '13' ],
  106. [ { colspan: 2, contents: '20' }, '22[]', '23' ],
  107. [ '30', { colspan: 2, contents: '31' }, '33' ],
  108. [ '40', '41', '42', '43' ]
  109. ] ) );
  110. command.execute();
  111. assertEqualMarkup( getData( model ), modelTable( [
  112. [ { colspan: 3, contents: '00' }, '03' ],
  113. [ { colspan: 2, contents: '10' }, '13' ],
  114. [ { colspan: 2, contents: '20' }, '[]23' ],
  115. [ '30', '31', '33' ],
  116. [ '40', '41', '43' ]
  117. ] ) );
  118. } );
  119. it( 'should decrease colspan of cells that are on removed column', () => {
  120. setData( model, modelTable( [
  121. [ { colspan: 3, contents: '[]00' }, '03' ],
  122. [ { colspan: 2, contents: '10' }, '13' ],
  123. [ '20', '21', '22', '23' ]
  124. ] ) );
  125. command.execute();
  126. assertEqualMarkup( getData( model ), modelTable( [
  127. [ { colspan: 2, contents: '[]00' }, '03' ],
  128. [ '10', '13' ],
  129. [ '21', '22', '23' ]
  130. ] ) );
  131. } );
  132. it( 'should move focus to previous column of removed cell if in last column', () => {
  133. setData( model, modelTable( [
  134. [ '00', '01', '02' ],
  135. [ '10', '11', '12[]' ],
  136. [ '20', '21', '22' ]
  137. ] ) );
  138. command.execute();
  139. assertEqualMarkup( getData( model ), modelTable( [
  140. [ '00', '01' ],
  141. [ '10', '[]11' ],
  142. [ '20', '21' ]
  143. ] ) );
  144. } );
  145. } );
  146. } );