removerowcommand.js 8.0 KB

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