removecolumncommand.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 { defaultConversion, defaultSchema, modelTable } from '../_utils/utils';
  9. import TableUtils from '../../src/tableutils';
  10. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  11. describe( 'RemoveColumnCommand', () => {
  12. let editor, model, command;
  13. beforeEach( () => {
  14. return ModelTestEditor
  15. .create( {
  16. plugins: [ TableUtils ]
  17. } )
  18. .then( newEditor => {
  19. editor = newEditor;
  20. model = editor.model;
  21. command = new RemoveColumnCommand( editor );
  22. defaultSchema( model.schema );
  23. defaultConversion( editor.conversion );
  24. } );
  25. } );
  26. afterEach( () => {
  27. return editor.destroy();
  28. } );
  29. describe( 'isEnabled', () => {
  30. it( 'should be true if selection is inside table cell', () => {
  31. setData( model, modelTable( [
  32. [ '00[]', '01' ],
  33. [ '10', '11' ]
  34. ] ) );
  35. expect( command.isEnabled ).to.be.true;
  36. } );
  37. it( 'should be false if selection is inside table with one column only', () => {
  38. setData( model, modelTable( [
  39. [ '00' ],
  40. [ '10[]' ],
  41. [ '20[]' ]
  42. ] ) );
  43. expect( command.isEnabled ).to.be.false;
  44. } );
  45. it( 'should be false if selection is outside a table', () => {
  46. setData( model, '<paragraph>11[]</paragraph>' );
  47. expect( command.isEnabled ).to.be.false;
  48. } );
  49. } );
  50. describe( 'execute()', () => {
  51. it( 'should remove a given column', () => {
  52. setData( model, modelTable( [
  53. [ '00', '01', '02' ],
  54. [ '10', '[]11', '12' ],
  55. [ '20', '21', '22' ]
  56. ] ) );
  57. command.execute();
  58. assertEqualMarkup( getData( model ), modelTable( [
  59. [ '00', '02' ],
  60. [ '10', '[]12' ],
  61. [ '20', '22' ]
  62. ] ) );
  63. } );
  64. it( 'should remove a given column from a table start', () => {
  65. setData( model, modelTable( [
  66. [ '[]00', '01' ],
  67. [ '10', '11' ],
  68. [ '20', '21' ]
  69. ] ) );
  70. command.execute();
  71. assertEqualMarkup( getData( model ), modelTable( [
  72. [ '[]01' ],
  73. [ '11' ],
  74. [ '21' ]
  75. ] ) );
  76. } );
  77. it( 'should change heading columns if removing a heading column', () => {
  78. setData( model, modelTable( [
  79. [ '00', '01' ],
  80. [ '[]10', '11' ],
  81. [ '20', '21' ]
  82. ], { headingColumns: 2 } ) );
  83. command.execute();
  84. assertEqualMarkup( getData( model ), modelTable( [
  85. [ '01' ],
  86. [ '[]11' ],
  87. [ '21' ]
  88. ], { headingColumns: 1 } ) );
  89. } );
  90. it( 'should decrease colspan of table cells from previous column', () => {
  91. setData( model, modelTable( [
  92. [ { colspan: 4, contents: '00' }, '03' ],
  93. [ { colspan: 3, contents: '10' }, '13' ],
  94. [ { colspan: 2, contents: '20' }, '22[]', '23' ],
  95. [ '30', { colspan: 2, contents: '31' }, '33' ],
  96. [ '40', '41', '42', '43' ]
  97. ] ) );
  98. command.execute();
  99. assertEqualMarkup( getData( model ), modelTable( [
  100. [ { colspan: 3, contents: '00' }, '03' ],
  101. [ { colspan: 2, contents: '10' }, '13' ],
  102. [ { colspan: 2, contents: '20' }, '[]23' ],
  103. [ '30', '31', '33' ],
  104. [ '40', '41', '43' ]
  105. ] ) );
  106. } );
  107. it( 'should decrease colspan of cells that are on removed column', () => {
  108. setData( model, modelTable( [
  109. [ { colspan: 3, contents: '[]00' }, '03' ],
  110. [ { colspan: 2, contents: '10' }, '13' ],
  111. [ '20', '21', '22', '23' ]
  112. ] ) );
  113. command.execute();
  114. assertEqualMarkup( getData( model ), modelTable( [
  115. [ { colspan: 2, contents: '[]00' }, '03' ],
  116. [ '10', '13' ],
  117. [ '21', '22', '23' ]
  118. ] ) );
  119. } );
  120. it( 'should move focus to previous column of removed cell if in last column', () => {
  121. setData( model, modelTable( [
  122. [ '00', '01', '02' ],
  123. [ '10', '11', '12[]' ],
  124. [ '20', '21', '22' ]
  125. ] ) );
  126. command.execute();
  127. assertEqualMarkup( getData( model ), modelTable( [
  128. [ '00', '01' ],
  129. [ '10', '[]11' ],
  130. [ '20', '21' ]
  131. ] ) );
  132. } );
  133. } );
  134. } );