removecolumncommand.js 5.1 KB

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