removerowcommand.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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( 'with multiple rows selected', () => {
  86. it( 'should properly remove middle rows', () => {
  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. // This test is blocked by (#6370).
  104. //
  105. // it( 'should properly remove tailing rows', () => {
  106. // setData( model, modelTable( [
  107. // [ '00', '01' ],
  108. // [ '10', '11' ],
  109. // [ '20', '21' ],
  110. // [ '30', '31' ]
  111. // ] ) );
  112. // const tableSelection = editor.plugins.get( TableSelection );
  113. // const modelRoot = model.document.getRoot();
  114. // tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 2, 0 ] ) );
  115. // tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 36, 0 ] ) );
  116. // command.execute();
  117. // assertEqualMarkup( getData( model ), modelTable( [
  118. // [ '00', '01' ],
  119. // [ '10', '11[]' ]
  120. // ] ) );
  121. // } );
  122. it( 'should properly remove beginning rows', () => {
  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, 0, 0 ] ) );
  132. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 0 ] ) );
  133. command.execute();
  134. assertEqualMarkup( getData( model ), modelTable( [
  135. [ '[]20', '21' ],
  136. [ '30', '31' ]
  137. ] ) );
  138. } );
  139. it( 'should support removing multiple headings', () => {
  140. setData( model, modelTable( [
  141. [ '00', '01' ],
  142. [ '10', '11' ],
  143. [ '20', '21' ],
  144. [ '30', '31' ]
  145. ], { headingRows: 3 } ) );
  146. const tableSelection = editor.plugins.get( TableSelection );
  147. const modelRoot = model.document.getRoot();
  148. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  149. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 0 ] ) );
  150. command.execute();
  151. assertEqualMarkup( getData( model ), modelTable( [
  152. [ '[]20', '21' ],
  153. [ '30', '31' ]
  154. ], { headingRows: 1 } ) );
  155. } );
  156. it( 'should support removing mixed heading and cell rows', () => {
  157. setData( model, modelTable( [
  158. [ '00', '01' ],
  159. [ '10', '11' ],
  160. [ '20', '21' ]
  161. ], { headingRows: 1 } ) );
  162. const tableSelection = editor.plugins.get( TableSelection );
  163. const modelRoot = model.document.getRoot();
  164. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  165. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 0 ] ) );
  166. command.execute();
  167. assertEqualMarkup( getData( model ), modelTable( [
  168. [ '[]20', '21' ]
  169. ] ) );
  170. } );
  171. } );
  172. it( 'should remove a given row from a table start', () => {
  173. setData( model, modelTable( [
  174. [ '[]00', '01' ],
  175. [ '10', '11' ],
  176. [ '20', '21' ]
  177. ] ) );
  178. command.execute();
  179. assertEqualMarkup( getData( model ), modelTable( [
  180. [ '[]10', '11' ],
  181. [ '20', '21' ]
  182. ] ) );
  183. } );
  184. it( 'should change heading rows if removing a heading row', () => {
  185. setData( model, modelTable( [
  186. [ '00', '01' ],
  187. [ '[]10', '11' ],
  188. [ '20', '21' ]
  189. ], { headingRows: 2 } ) );
  190. command.execute();
  191. assertEqualMarkup( getData( model ), modelTable( [
  192. [ '00', '01' ],
  193. [ '[]20', '21' ]
  194. ], { headingRows: 1 } ) );
  195. } );
  196. it( 'should decrease rowspan of table cells from previous rows', () => {
  197. setData( model, modelTable( [
  198. [ { rowspan: 4, contents: '00' }, { rowspan: 3, contents: '01' }, { rowspan: 2, contents: '02' }, '03', '04' ],
  199. [ { rowspan: 2, contents: '13' }, '14' ],
  200. [ '22[]', '23', '24' ],
  201. [ '30', '31', '32', '33', '34' ]
  202. ] ) );
  203. command.execute();
  204. assertEqualMarkup( getData( model ), modelTable( [
  205. [ { rowspan: 3, contents: '00' }, { rowspan: 2, contents: '01' }, { rowspan: 2, contents: '02' }, '03', '04' ],
  206. [ '13', '14' ],
  207. [ '30', '31', '[]32', '33', '34' ]
  208. ] ) );
  209. } );
  210. it( 'should move rowspaned cells to row below removing it\'s row', () => {
  211. setData( model, modelTable( [
  212. [ { rowspan: 3, contents: '[]00' }, { rowspan: 2, contents: '01' }, '02' ],
  213. [ '12' ],
  214. [ '22' ],
  215. [ '30', '31', '32' ]
  216. ] ) );
  217. command.execute();
  218. assertEqualMarkup( getData( model ), modelTable( [
  219. [ { rowspan: 2, contents: '[]00' }, '01', '12' ],
  220. [ '22' ],
  221. [ '30', '31', '32' ]
  222. ] ) );
  223. } );
  224. } );
  225. } );