removecolumncommand.js 3.9 KB

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