removerowcommand.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /**
  2. * @license Copyright (c) 2003-2020, 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 RemoveRowCommand from '../../src/commands/removerowcommand';
  8. import TableSelection from '../../src/tableselection';
  9. import { defaultConversion, defaultSchema, modelTable } from '../_utils/utils';
  10. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  11. describe( 'RemoveRowCommand', () => {
  12. let editor, model, command;
  13. beforeEach( () => {
  14. return ModelTestEditor.create( { plugins: [ TableSelection ] } )
  15. .then( newEditor => {
  16. editor = newEditor;
  17. model = editor.model;
  18. command = new RemoveRowCommand( editor );
  19. defaultSchema( model.schema );
  20. defaultConversion( editor.conversion );
  21. } );
  22. } );
  23. afterEach( () => {
  24. return editor.destroy();
  25. } );
  26. describe( 'isEnabled', () => {
  27. it( 'should be true if selection is inside table cell', () => {
  28. setData( model, modelTable( [
  29. [ '00[]', '01' ],
  30. [ '10', '11' ]
  31. ] ) );
  32. expect( command.isEnabled ).to.be.true;
  33. } );
  34. it( 'should be true if selection contains multiple cells', () => {
  35. setData( model, modelTable( [
  36. [ '00', '01' ],
  37. [ '10', '11' ]
  38. ] ) );
  39. const tableSelection = editor.plugins.get( TableSelection );
  40. const modelRoot = model.document.getRoot();
  41. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  42. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 0, 1 ] ) );
  43. expect( command.isEnabled ).to.be.true;
  44. } );
  45. it( 'should be false if selection is inside table with one row only', () => {
  46. setData( model, modelTable( [
  47. [ '00[]', '01' ]
  48. ] ) );
  49. expect( command.isEnabled ).to.be.false;
  50. } );
  51. it( 'should be false if selection is outside a table', () => {
  52. setData( model, '<paragraph>11[]</paragraph>' );
  53. expect( command.isEnabled ).to.be.false;
  54. } );
  55. } );
  56. describe( 'execute()', () => {
  57. it( 'should remove a given row', () => {
  58. setData( model, modelTable( [
  59. [ '00', '01' ],
  60. [ '[]10', '11' ],
  61. [ '20', '21' ]
  62. ] ) );
  63. command.execute();
  64. assertEqualMarkup( getData( model ), modelTable( [
  65. [ '00', '01' ],
  66. [ '[]20', '21' ]
  67. ] ) );
  68. } );
  69. it( 'should remove a row if all its cells are selected', () => {
  70. setData( model, modelTable( [
  71. [ '00', '01' ],
  72. [ '10', '11' ],
  73. [ '20', '21' ]
  74. ] ) );
  75. const tableSelection = editor.plugins.get( TableSelection );
  76. const modelRoot = model.document.getRoot();
  77. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 1, 0 ] ) );
  78. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
  79. command.execute();
  80. assertEqualMarkup( getData( model ), modelTable( [
  81. [ '00', '01' ],
  82. [ '[]20', '21' ]
  83. ] ) );
  84. } );
  85. describe( 'multiple rows selection', () => {
  86. it( 'should properly remove when middle rows are selected', () => {
  87. setData( model, modelTable( [
  88. [ '00', '01' ],
  89. [ '10', '11' ],
  90. [ '20', '21' ],
  91. [ '30', '31' ]
  92. ] ) );
  93. const tableSelection = editor.plugins.get( TableSelection );
  94. const modelRoot = model.document.getRoot();
  95. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 1, 0 ] ) );
  96. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 2, 0 ] ) );
  97. command.execute();
  98. assertEqualMarkup( getData( model ), modelTable( [
  99. [ '00', '01' ],
  100. [ '[]30', '31' ]
  101. ] ) );
  102. } );
  103. it( 'should properly remove when beginning rows are selected', () => {
  104. setData( model, modelTable( [
  105. [ '00', '01' ],
  106. [ '10', '11' ],
  107. [ '20', '21' ],
  108. [ '30', '31' ]
  109. ] ) );
  110. const tableSelection = editor.plugins.get( TableSelection );
  111. const modelRoot = model.document.getRoot();
  112. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  113. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 0 ] ) );
  114. command.execute();
  115. assertEqualMarkup( getData( model ), modelTable( [
  116. [ '[]20', '21' ],
  117. [ '30', '31' ]
  118. ] ) );
  119. } );
  120. // This test is blocked by (#6370).
  121. //
  122. // it( 'should properly remove when tailing rows are selected', () => {
  123. // setData( model, modelTable( [
  124. // [ '00', '01' ],
  125. // [ '10', '11' ],
  126. // [ '20', '21' ],
  127. // [ '30', '31' ]
  128. // ] ) );
  129. // const tableSelection = editor.plugins.get( TableSelection );
  130. // const modelRoot = model.document.getRoot();
  131. // tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 2, 0 ] ) );
  132. // tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 36, 0 ] ) );
  133. // command.execute();
  134. // assertEqualMarkup( getData( model ), modelTable( [
  135. // [ '00', '01' ],
  136. // [ '10', '11[]' ]
  137. // ] ) );
  138. // } );
  139. } );
  140. it( 'should remove a given row from a table start', () => {
  141. setData( model, modelTable( [
  142. [ '[]00', '01' ],
  143. [ '10', '11' ],
  144. [ '20', '21' ]
  145. ] ) );
  146. command.execute();
  147. assertEqualMarkup( getData( model ), modelTable( [
  148. [ '[]10', '11' ],
  149. [ '20', '21' ]
  150. ] ) );
  151. } );
  152. it( 'should change heading rows if removing a heading row', () => {
  153. setData( model, modelTable( [
  154. [ '00', '01' ],
  155. [ '[]10', '11' ],
  156. [ '20', '21' ]
  157. ], { headingRows: 2 } ) );
  158. command.execute();
  159. assertEqualMarkup( getData( model ), modelTable( [
  160. [ '00', '01' ],
  161. [ '[]20', '21' ]
  162. ], { headingRows: 1 } ) );
  163. } );
  164. it( 'should decrease rowspan of table cells from previous rows', () => {
  165. setData( model, modelTable( [
  166. [ { rowspan: 4, contents: '00' }, { rowspan: 3, contents: '01' }, { rowspan: 2, contents: '02' }, '03', '04' ],
  167. [ { rowspan: 2, contents: '13' }, '14' ],
  168. [ '22[]', '23', '24' ],
  169. [ '30', '31', '32', '33', '34' ]
  170. ] ) );
  171. command.execute();
  172. assertEqualMarkup( getData( model ), modelTable( [
  173. [ { rowspan: 3, contents: '00' }, { rowspan: 2, contents: '01' }, { rowspan: 2, contents: '02' }, '03', '04' ],
  174. [ '13', '14' ],
  175. [ '30', '31', '[]32', '33', '34' ]
  176. ] ) );
  177. } );
  178. it( 'should move rowspaned cells to row below removing it\'s row', () => {
  179. setData( model, modelTable( [
  180. [ { rowspan: 3, contents: '[]00' }, { rowspan: 2, contents: '01' }, '02' ],
  181. [ '12' ],
  182. [ '22' ],
  183. [ '30', '31', '32' ]
  184. ] ) );
  185. command.execute();
  186. assertEqualMarkup( getData( model ), modelTable( [
  187. [ { rowspan: 2, contents: '[]00' }, '01', '12' ],
  188. [ '22' ],
  189. [ '30', '31', '32' ]
  190. ] ) );
  191. } );
  192. } );
  193. } );