removerowcommand.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. it( 'should properly remove middle rows in reversed order', () => {
  124. setData( model, modelTable( [
  125. [ '00', '01' ],
  126. [ '10', '11' ],
  127. [ '20', '21' ],
  128. [ '30', '31' ]
  129. ] ) );
  130. const tableSelection = editor.plugins.get( TableSelection );
  131. const modelRoot = model.document.getRoot();
  132. tableSelection._setCellSelection(
  133. modelRoot.getNodeByPath( [ 0, 2, 0 ] ),
  134. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  135. );
  136. command.execute();
  137. assertEqualMarkup( getData( model ), modelTable( [
  138. [ '00', '01' ],
  139. [ '[]30', '31' ]
  140. ] ) );
  141. } );
  142. // This test is blocked by (#6370).
  143. //
  144. // it( 'should properly remove tailing 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, 2, 0 ] ),
  155. // modelRoot.getNodeByPath( [ 0, 36, 0 ] )
  156. // );
  157. // command.execute();
  158. // assertEqualMarkup( getData( model ), modelTable( [
  159. // [ '00', '01' ],
  160. // [ '10', '11[]' ]
  161. // ] ) );
  162. // } );
  163. it( 'should properly remove beginning rows', () => {
  164. setData( model, modelTable( [
  165. [ '00', '01' ],
  166. [ '10', '11' ],
  167. [ '20', '21' ],
  168. [ '30', '31' ]
  169. ] ) );
  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. ] ) );
  181. } );
  182. it( 'should support removing multiple headings', () => {
  183. setData( model, modelTable( [
  184. [ '00', '01' ],
  185. [ '10', '11' ],
  186. [ '20', '21' ],
  187. [ '30', '31' ]
  188. ], { headingRows: 3 } ) );
  189. const tableSelection = editor.plugins.get( TableSelection );
  190. const modelRoot = model.document.getRoot();
  191. tableSelection._setCellSelection(
  192. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  193. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  194. );
  195. command.execute();
  196. assertEqualMarkup( getData( model ), modelTable( [
  197. [ '[]20', '21' ],
  198. [ '30', '31' ]
  199. ], { headingRows: 1 } ) );
  200. } );
  201. it( 'should support removing mixed heading and cell rows', () => {
  202. setData( model, modelTable( [
  203. [ '00', '01' ],
  204. [ '10', '11' ],
  205. [ '20', '21' ]
  206. ], { headingRows: 1 } ) );
  207. const tableSelection = editor.plugins.get( TableSelection );
  208. const modelRoot = model.document.getRoot();
  209. tableSelection._setCellSelection(
  210. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  211. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  212. );
  213. command.execute();
  214. assertEqualMarkup( getData( model ), modelTable( [
  215. [ '[]20', '21' ]
  216. ] ) );
  217. } );
  218. } );
  219. describe( 'with entire row selected', () => {
  220. it( 'should properly remove row if reversed selection is made', () => {
  221. setData( model, modelTable( [
  222. [ '00', '01' ],
  223. [ '10', '11' ]
  224. ] ) );
  225. const tableSelection = editor.plugins.get( TableSelection );
  226. const modelRoot = model.document.getRoot();
  227. tableSelection._setCellSelection(
  228. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  229. modelRoot.getNodeByPath( [ 0, 0, 0 ] )
  230. );
  231. command.execute();
  232. assertEqualMarkup( getData( model ), modelTable( [
  233. [ '10', '[]11' ]
  234. ] ) );
  235. } );
  236. } );
  237. it( 'should remove a given row from a table start', () => {
  238. setData( model, modelTable( [
  239. [ '[]00', '01' ],
  240. [ '10', '11' ],
  241. [ '20', '21' ]
  242. ] ) );
  243. command.execute();
  244. assertEqualMarkup( getData( model ), modelTable( [
  245. [ '[]10', '11' ],
  246. [ '20', '21' ]
  247. ] ) );
  248. } );
  249. it( 'should change heading rows if removing a heading row', () => {
  250. setData( model, modelTable( [
  251. [ '00', '01' ],
  252. [ '[]10', '11' ],
  253. [ '20', '21' ]
  254. ], { headingRows: 2 } ) );
  255. command.execute();
  256. assertEqualMarkup( getData( model ), modelTable( [
  257. [ '00', '01' ],
  258. [ '[]20', '21' ]
  259. ], { headingRows: 1 } ) );
  260. } );
  261. it( 'should decrease rowspan of table cells from previous rows', () => {
  262. setData( model, modelTable( [
  263. [ { rowspan: 4, contents: '00' }, { rowspan: 3, contents: '01' }, { rowspan: 2, contents: '02' }, '03', '04' ],
  264. [ { rowspan: 2, contents: '13' }, '14' ],
  265. [ '22[]', '23', '24' ],
  266. [ '30', '31', '32', '33', '34' ]
  267. ] ) );
  268. command.execute();
  269. assertEqualMarkup( getData( model ), modelTable( [
  270. [ { rowspan: 3, contents: '00' }, { rowspan: 2, contents: '01' }, { rowspan: 2, contents: '02' }, '03', '04' ],
  271. [ '13', '14' ],
  272. [ '30', '31', '[]32', '33', '34' ]
  273. ] ) );
  274. } );
  275. it( 'should move rowspaned cells to row below removing it\'s row', () => {
  276. setData( model, modelTable( [
  277. [ { rowspan: 3, contents: '[]00' }, { rowspan: 2, contents: '01' }, '02' ],
  278. [ '12' ],
  279. [ '22' ],
  280. [ '30', '31', '32' ]
  281. ] ) );
  282. command.execute();
  283. assertEqualMarkup( getData( model ), modelTable( [
  284. [ { rowspan: 2, contents: '[]00' }, '01', '12' ],
  285. [ '22' ],
  286. [ '30', '31', '32' ]
  287. ] ) );
  288. } );
  289. } );
  290. } );