removerowcommand.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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._setCellSelection(
  42. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  43. modelRoot.getNodeByPath( [ 0, 0, 1 ] )
  44. );
  45. expect( command.isEnabled ).to.be.true;
  46. } );
  47. it( 'should be false if selection is inside table with one row only', () => {
  48. setData( model, modelTable( [
  49. [ '00[]', '01' ]
  50. ] ) );
  51. expect( command.isEnabled ).to.be.false;
  52. } );
  53. it( 'should be false if selection is outside a table', () => {
  54. setData( model, '<paragraph>11[]</paragraph>' );
  55. expect( command.isEnabled ).to.be.false;
  56. } );
  57. } );
  58. describe( 'execute()', () => {
  59. it( 'should remove a given row', () => {
  60. setData( model, modelTable( [
  61. [ '00', '01' ],
  62. [ '[]10', '11' ],
  63. [ '20', '21' ]
  64. ] ) );
  65. command.execute();
  66. assertEqualMarkup( getData( model ), modelTable( [
  67. [ '00', '01' ],
  68. [ '[]20', '21' ]
  69. ] ) );
  70. } );
  71. it( 'should remove a row if all its cells are selected', () => {
  72. setData( model, modelTable( [
  73. [ '00', '01' ],
  74. [ '10', '11' ],
  75. [ '20', '21' ]
  76. ] ) );
  77. const tableSelection = editor.plugins.get( TableSelection );
  78. const modelRoot = model.document.getRoot();
  79. tableSelection._setCellSelection(
  80. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  81. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  82. );
  83. command.execute();
  84. assertEqualMarkup( getData( model ), modelTable( [
  85. [ '00', '01' ],
  86. [ '[]20', '21' ]
  87. ] ) );
  88. } );
  89. describe( 'with multiple rows selected', () => {
  90. it( 'should properly remove middle rows', () => {
  91. setData( model, modelTable( [
  92. [ '00', '01' ],
  93. [ '10', '11' ],
  94. [ '20', '21' ],
  95. [ '30', '31' ]
  96. ] ) );
  97. const tableSelection = editor.plugins.get( TableSelection );
  98. const modelRoot = model.document.getRoot();
  99. tableSelection._setCellSelection(
  100. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  101. modelRoot.getNodeByPath( [ 0, 2, 0 ] )
  102. );
  103. command.execute();
  104. assertEqualMarkup( getData( model ), modelTable( [
  105. [ '00', '01' ],
  106. [ '[]30', '31' ]
  107. ] ) );
  108. } );
  109. // This test is blocked by (#6370).
  110. //
  111. // it( 'should properly remove tailing rows', () => {
  112. // setData( model, modelTable( [
  113. // [ '00', '01' ],
  114. // [ '10', '11' ],
  115. // [ '20', '21' ],
  116. // [ '30', '31' ]
  117. // ] ) );
  118. // const tableSelection = editor.plugins.get( TableSelection );
  119. // const modelRoot = model.document.getRoot();
  120. // tableSelection._setCellSelection(
  121. // modelRoot.getNodeByPath( [ 0, 2, 0 ] ),
  122. // modelRoot.getNodeByPath( [ 0, 36, 0 ] )
  123. // );
  124. // command.execute();
  125. // assertEqualMarkup( getData( model ), modelTable( [
  126. // [ '00', '01' ],
  127. // [ '10', '11[]' ]
  128. // ] ) );
  129. // } );
  130. it( 'should properly remove beginning rows', () => {
  131. setData( model, modelTable( [
  132. [ '00', '01' ],
  133. [ '10', '11' ],
  134. [ '20', '21' ],
  135. [ '30', '31' ]
  136. ] ) );
  137. const tableSelection = editor.plugins.get( TableSelection );
  138. const modelRoot = model.document.getRoot();
  139. tableSelection._setCellSelection(
  140. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  141. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  142. );
  143. command.execute();
  144. assertEqualMarkup( getData( model ), modelTable( [
  145. [ '[]20', '21' ],
  146. [ '30', '31' ]
  147. ] ) );
  148. } );
  149. it( 'should support removing multiple headings', () => {
  150. setData( model, modelTable( [
  151. [ '00', '01' ],
  152. [ '10', '11' ],
  153. [ '20', '21' ],
  154. [ '30', '31' ]
  155. ], { headingRows: 3 } ) );
  156. const tableSelection = editor.plugins.get( TableSelection );
  157. const modelRoot = model.document.getRoot();
  158. tableSelection._setCellSelection(
  159. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  160. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  161. );
  162. command.execute();
  163. assertEqualMarkup( getData( model ), modelTable( [
  164. [ '[]20', '21' ],
  165. [ '30', '31' ]
  166. ], { headingRows: 1 } ) );
  167. } );
  168. it( 'should support removing mixed heading and cell rows', () => {
  169. setData( model, modelTable( [
  170. [ '00', '01' ],
  171. [ '10', '11' ],
  172. [ '20', '21' ]
  173. ], { headingRows: 1 } ) );
  174. const tableSelection = editor.plugins.get( TableSelection );
  175. const modelRoot = model.document.getRoot();
  176. tableSelection._setCellSelection(
  177. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  178. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  179. );
  180. command.execute();
  181. assertEqualMarkup( getData( model ), modelTable( [
  182. [ '[]20', '21' ]
  183. ] ) );
  184. } );
  185. } );
  186. it( 'should remove a given row from a table start', () => {
  187. setData( model, modelTable( [
  188. [ '[]00', '01' ],
  189. [ '10', '11' ],
  190. [ '20', '21' ]
  191. ] ) );
  192. command.execute();
  193. assertEqualMarkup( getData( model ), modelTable( [
  194. [ '[]10', '11' ],
  195. [ '20', '21' ]
  196. ] ) );
  197. } );
  198. it( 'should change heading rows if removing a heading row', () => {
  199. setData( model, modelTable( [
  200. [ '00', '01' ],
  201. [ '[]10', '11' ],
  202. [ '20', '21' ]
  203. ], { headingRows: 2 } ) );
  204. command.execute();
  205. assertEqualMarkup( getData( model ), modelTable( [
  206. [ '00', '01' ],
  207. [ '[]20', '21' ]
  208. ], { headingRows: 1 } ) );
  209. } );
  210. it( 'should decrease rowspan of table cells from previous rows', () => {
  211. setData( model, modelTable( [
  212. [ { rowspan: 4, contents: '00' }, { rowspan: 3, contents: '01' }, { rowspan: 2, contents: '02' }, '03', '04' ],
  213. [ { rowspan: 2, contents: '13' }, '14' ],
  214. [ '22[]', '23', '24' ],
  215. [ '30', '31', '32', '33', '34' ]
  216. ] ) );
  217. command.execute();
  218. assertEqualMarkup( getData( model ), modelTable( [
  219. [ { rowspan: 3, contents: '00' }, { rowspan: 2, contents: '01' }, { rowspan: 2, contents: '02' }, '03', '04' ],
  220. [ '13', '14' ],
  221. [ '30', '31', '[]32', '33', '34' ]
  222. ] ) );
  223. } );
  224. it( 'should move rowspaned cells to row below removing it\'s row', () => {
  225. setData( model, modelTable( [
  226. [ { rowspan: 3, contents: '[]00' }, { rowspan: 2, contents: '01' }, '02' ],
  227. [ '12' ],
  228. [ '22' ],
  229. [ '30', '31', '32' ]
  230. ] ) );
  231. command.execute();
  232. assertEqualMarkup( getData( model ), modelTable( [
  233. [ { rowspan: 2, contents: '[]00' }, '01', '12' ],
  234. [ '22' ],
  235. [ '30', '31', '32' ]
  236. ] ) );
  237. } );
  238. } );
  239. } );