removerowcommand.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 RemoveRowCommand from '../../src/commands/removerowcommand';
  9. import { downcastInsertTable } from '../../src/converters/downcast';
  10. import upcastTable from '../../src/converters/upcasttable';
  11. import { formatTable, formattedModelTable, modelTable } from '../_utils/utils';
  12. describe( 'RemoveRowCommand', () => {
  13. let editor, model, command;
  14. beforeEach( () => {
  15. return ModelTestEditor.create()
  16. .then( newEditor => {
  17. editor = newEditor;
  18. model = editor.model;
  19. command = new RemoveRowCommand( editor );
  20. const conversion = editor.conversion;
  21. const schema = model.schema;
  22. schema.register( 'table', {
  23. allowWhere: '$block',
  24. allowAttributes: [ 'headingRows' ],
  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 row only', () => {
  66. setData( model, modelTable( [
  67. [ '00[]', '01' ]
  68. ] ) );
  69. expect( command.isEnabled ).to.be.false;
  70. } );
  71. it( 'should be false if selection is outside a table', () => {
  72. setData( model, '<p>11[]</p>' );
  73. expect( command.isEnabled ).to.be.false;
  74. } );
  75. } );
  76. describe( 'execute()', () => {
  77. it( 'should remove a given row', () => {
  78. setData( model, modelTable( [
  79. [ '00', '01' ],
  80. [ '[]10', '11' ],
  81. [ '20', '21' ]
  82. ] ) );
  83. command.execute();
  84. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  85. [ '00', '01[]' ],
  86. [ '20', '21' ]
  87. ] ) );
  88. } );
  89. it( 'should remove a given row from a table start', () => {
  90. setData( model, modelTable( [
  91. [ '[]00', '01' ],
  92. [ '10', '11' ],
  93. [ '20', '21' ]
  94. ] ) );
  95. command.execute();
  96. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  97. [ '[]10', '11' ],
  98. [ '20', '21' ]
  99. ] ) );
  100. } );
  101. it( 'should change heading rows if removing a heading row', () => {
  102. setData( model, modelTable( [
  103. [ '00', '01' ],
  104. [ '[]10', '11' ],
  105. [ '20', '21' ]
  106. ], { headingRows: 2 } ) );
  107. command.execute();
  108. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  109. [ '00', '01[]' ],
  110. [ '20', '21' ]
  111. ], { headingRows: 1 } ) );
  112. } );
  113. it( 'should decrease rowspan of table cells from previous rows', () => {
  114. setData( model, modelTable( [
  115. [ { rowspan: 4, contents: '00' }, { rowspan: 3, contents: '01' }, { rowspan: 2, contents: '02' }, '03', '04' ],
  116. [ { rowspan: 2, contents: '13' }, '14' ],
  117. [ '22[]', '23', '24' ],
  118. [ '30', '31', '32', '33', '34' ]
  119. ] ) );
  120. command.execute();
  121. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  122. [ { rowspan: 3, contents: '00' }, { rowspan: 2, contents: '01' }, { rowspan: 2, contents: '02' }, '03', '04' ],
  123. [ '13', '14[]' ],
  124. [ '30', '31', '32', '33', '34' ]
  125. ] ) );
  126. } );
  127. it( 'should move rowspaned cells to row below removing it\'s row', () => {
  128. setData( model, modelTable( [
  129. [ { rowspan: 3, contents: '[]00' }, { rowspan: 2, contents: '01' }, '02' ],
  130. [ '12' ],
  131. [ '22' ],
  132. [ '30', '31', '32' ]
  133. ] ) );
  134. command.execute();
  135. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  136. [ { rowspan: 2, contents: '[]00' }, '01', '12' ],
  137. [ '22' ],
  138. [ '30', '31', '32' ]
  139. ] ) );
  140. } );
  141. } );
  142. } );