8
0

removerowcommand.js 5.2 KB

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