removecolumncommand.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  7. import { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
  8. import RemoveColumnCommand from '../../src/commands/removecolumncommand';
  9. import { downcastInsertTable } from '../../src/converters/downcast';
  10. import upcastTable from '../../src/converters/upcasttable';
  11. import { formatTable, formattedModelTable, modelTable } from '../_utils/utils';
  12. import TableUtils from '../../src/tableutils';
  13. describe( 'RemoveColumnCommand', () => {
  14. let editor, model, command;
  15. beforeEach( () => {
  16. return ModelTestEditor.create( {
  17. plugins: [ TableUtils ]
  18. } ).then( newEditor => {
  19. editor = newEditor;
  20. model = editor.model;
  21. command = new RemoveColumnCommand( editor );
  22. const conversion = editor.conversion;
  23. const schema = model.schema;
  24. schema.register( 'table', {
  25. allowWhere: '$block',
  26. allowAttributes: [ 'headingRows', 'headingColumns' ],
  27. isBlock: true,
  28. isObject: true
  29. } );
  30. schema.register( 'tableRow', {
  31. allowIn: 'table',
  32. allowAttributes: [],
  33. isBlock: true,
  34. isLimit: true
  35. } );
  36. schema.register( 'tableCell', {
  37. allowIn: 'tableRow',
  38. allowContentOf: '$block',
  39. allowAttributes: [ 'colspan', 'rowspan' ],
  40. isBlock: true,
  41. isLimit: true
  42. } );
  43. model.schema.register( 'p', { inheritAllFrom: '$block' } );
  44. // Table conversion.
  45. conversion.for( 'upcast' ).add( upcastTable() );
  46. conversion.for( 'downcast' ).add( downcastInsertTable() );
  47. // Table row upcast only since downcast conversion is done in `downcastTable()`.
  48. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableRow', view: 'tr' } ) );
  49. // Table cell conversion.
  50. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'td' } ) );
  51. conversion.for( 'upcast' ).add( upcastElementToElement( { model: 'tableCell', view: 'th' } ) );
  52. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  53. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  54. } );
  55. } );
  56. afterEach( () => {
  57. return editor.destroy();
  58. } );
  59. describe( 'isEnabled', () => {
  60. it( 'should be true if selection is inside table cell', () => {
  61. setData( model, modelTable( [
  62. [ '00[]', '01' ],
  63. [ '10', '11' ]
  64. ] ) );
  65. expect( command.isEnabled ).to.be.true;
  66. } );
  67. it( 'should be false if selection is inside table with one column only', () => {
  68. setData( model, modelTable( [
  69. [ '00' ],
  70. [ '10[]' ],
  71. [ '20[]' ]
  72. ] ) );
  73. expect( command.isEnabled ).to.be.false;
  74. } );
  75. it( 'should be false if selection is outside a table', () => {
  76. setData( model, '<p>11[]</p>' );
  77. expect( command.isEnabled ).to.be.false;
  78. } );
  79. } );
  80. describe( 'execute()', () => {
  81. it( 'should remove a given column', () => {
  82. setData( model, modelTable( [
  83. [ '00', '01', '02' ],
  84. [ '10', '[]11', '12' ],
  85. [ '20', '21', '22' ]
  86. ] ) );
  87. command.execute();
  88. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  89. [ '00', '02' ],
  90. [ '10[]', '12' ],
  91. [ '20', '22' ]
  92. ] ) );
  93. } );
  94. it( 'should remove a given column from a table start', () => {
  95. setData( model, modelTable( [
  96. [ '[]00', '01' ],
  97. [ '10', '11' ],
  98. [ '20', '21' ]
  99. ] ) );
  100. command.execute();
  101. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  102. [ '[]01' ],
  103. [ '11' ],
  104. [ '21' ]
  105. ] ) );
  106. } );
  107. it( 'should change heading columns if removing a heading column', () => {
  108. setData( model, modelTable( [
  109. [ '00', '01' ],
  110. [ '[]10', '11' ],
  111. [ '20', '21' ]
  112. ], { headingColumns: 2 } ) );
  113. command.execute();
  114. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  115. [ '01' ],
  116. [ '[]11' ],
  117. [ '21' ]
  118. ], { headingColumns: 1 } ) );
  119. } );
  120. it( 'should decrease colspan of table cells from previous column', () => {
  121. setData( model, modelTable( [
  122. [ { colspan: 4, contents: '00' }, '03' ],
  123. [ { colspan: 3, contents: '10' }, '13' ],
  124. [ { colspan: 2, contents: '20' }, '22[]', '23' ],
  125. [ '30', { colspan: 2, contents: '31' }, '33' ],
  126. [ '40', '41', '42', '43' ]
  127. ] ) );
  128. command.execute();
  129. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  130. [ { colspan: 3, contents: '00' }, '03' ],
  131. [ { colspan: 2, contents: '10' }, '13' ],
  132. [ { colspan: 2, contents: '20[]' }, '23' ],
  133. [ '30', '31', '33' ],
  134. [ '40', '41', '43' ]
  135. ] ) );
  136. } );
  137. it( 'should decrease colspan of cells that are on removed column', () => {
  138. setData( model, modelTable( [
  139. [ { colspan: 3, contents: '[]00' }, '03' ],
  140. [ { colspan: 2, contents: '10' }, '13' ],
  141. [ '20', '21', '22', '23' ]
  142. ] ) );
  143. command.execute();
  144. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  145. [ { colspan: 2, contents: '[]00' }, '03' ],
  146. [ '10', '13' ],
  147. [ '21', '22', '23' ]
  148. ] ) );
  149. } );
  150. } );
  151. } );