removerowcommand.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. 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._setCellSelection(
  96. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  97. modelRoot.getNodeByPath( [ 0, 2, 0 ] )
  98. );
  99. command.execute();
  100. assertEqualMarkup( getData( model ), modelTable( [
  101. [ '00', '01' ],
  102. [ '[]30', '31' ]
  103. ] ) );
  104. } );
  105. it( 'should properly remove middle rows in reversed order', () => {
  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._setCellSelection(
  115. modelRoot.getNodeByPath( [ 0, 2, 0 ] ),
  116. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  117. );
  118. command.execute();
  119. assertEqualMarkup( getData( model ), modelTable( [
  120. [ '00', '01' ],
  121. [ '[]30', '31' ]
  122. ] ) );
  123. } );
  124. it( 'should properly remove tailing rows', () => {
  125. setData( model, modelTable( [
  126. [ '00', '01' ],
  127. [ '10', '11' ],
  128. [ '20', '21' ],
  129. [ '30', '31' ]
  130. ] ) );
  131. const tableSelection = editor.plugins.get( TableSelection );
  132. const modelRoot = model.document.getRoot();
  133. tableSelection._setCellSelection(
  134. modelRoot.getNodeByPath( [ 0, 2, 0 ] ),
  135. modelRoot.getNodeByPath( [ 0, 3, 0 ] )
  136. );
  137. command.execute();
  138. assertEqualMarkup( getData( model ), modelTable( [
  139. [ '00', '01' ],
  140. [ '[]10', '11' ]
  141. ] ) );
  142. } );
  143. it( 'should properly remove beginning rows', () => {
  144. setData( model, modelTable( [
  145. [ '00', '01' ],
  146. [ '10', '11' ],
  147. [ '20', '21' ],
  148. [ '30', '31' ]
  149. ] ) );
  150. const tableSelection = editor.plugins.get( TableSelection );
  151. const modelRoot = model.document.getRoot();
  152. tableSelection._setCellSelection(
  153. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  154. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  155. );
  156. command.execute();
  157. assertEqualMarkup( getData( model ), modelTable( [
  158. [ '[]20', '21' ],
  159. [ '30', '31' ]
  160. ] ) );
  161. } );
  162. it( 'should support removing multiple headings', () => {
  163. setData( model, modelTable( [
  164. [ '00', '01' ],
  165. [ '10', '11' ],
  166. [ '20', '21' ],
  167. [ '30', '31' ]
  168. ], { headingRows: 3 } ) );
  169. const tableSelection = editor.plugins.get( TableSelection );
  170. const modelRoot = model.document.getRoot();
  171. tableSelection._setCellSelection(
  172. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  173. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  174. );
  175. command.execute();
  176. assertEqualMarkup( getData( model ), modelTable( [
  177. [ '[]20', '21' ],
  178. [ '30', '31' ]
  179. ], { headingRows: 1 } ) );
  180. } );
  181. it( 'should support removing mixed heading and cell rows', () => {
  182. setData( model, modelTable( [
  183. [ '00', '01' ],
  184. [ '10', '11' ],
  185. [ '20', '21' ]
  186. ], { headingRows: 1 } ) );
  187. const tableSelection = editor.plugins.get( TableSelection );
  188. const modelRoot = model.document.getRoot();
  189. tableSelection._setCellSelection(
  190. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  191. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  192. );
  193. command.execute();
  194. assertEqualMarkup( getData( model ), modelTable( [
  195. [ '[]20', '21' ]
  196. ] ) );
  197. } );
  198. } );
  199. describe( 'with entire row selected', () => {
  200. it( 'should remove a row if all its cells are selected', () => {
  201. setData( model, modelTable( [
  202. [ '00', '01' ],
  203. [ '10', '11' ],
  204. [ '20', '21' ]
  205. ] ) );
  206. const tableSelection = editor.plugins.get( TableSelection );
  207. const modelRoot = model.document.getRoot();
  208. tableSelection._setCellSelection(
  209. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  210. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  211. );
  212. command.execute();
  213. assertEqualMarkup( getData( model ), modelTable( [
  214. [ '00', '01' ],
  215. [ '[]20', '21' ]
  216. ] ) );
  217. } );
  218. it( 'should properly remove row if reversed selection is made', () => {
  219. setData( model, modelTable( [
  220. [ '00', '01' ],
  221. [ '10', '11' ]
  222. ] ) );
  223. const tableSelection = editor.plugins.get( TableSelection );
  224. const modelRoot = model.document.getRoot();
  225. tableSelection._setCellSelection(
  226. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  227. modelRoot.getNodeByPath( [ 0, 0, 0 ] )
  228. );
  229. command.execute();
  230. assertEqualMarkup( getData( model ), modelTable( [
  231. [ '[]10', '11' ]
  232. ] ) );
  233. } );
  234. } );
  235. it( 'should remove a given row from a table start', () => {
  236. setData( model, modelTable( [
  237. [ '[]00', '01' ],
  238. [ '10', '11' ],
  239. [ '20', '21' ]
  240. ] ) );
  241. command.execute();
  242. assertEqualMarkup( getData( model ), modelTable( [
  243. [ '[]10', '11' ],
  244. [ '20', '21' ]
  245. ] ) );
  246. } );
  247. it( 'should remove a given row from a table start when selection is at the end', () => {
  248. setData( model, modelTable( [
  249. [ '00', '01[]' ],
  250. [ '10', '11' ],
  251. [ '20', '21' ]
  252. ] ) );
  253. command.execute();
  254. assertEqualMarkup( getData( model ), modelTable( [
  255. [ '10', '[]11' ],
  256. [ '20', '21' ]
  257. ] ) );
  258. } );
  259. it( 'should remove last row', () => {
  260. setData( model, modelTable( [
  261. [ '00', '01' ],
  262. [ '[]10', '11' ]
  263. ] ) );
  264. command.execute();
  265. assertEqualMarkup( getData( model ), modelTable( [
  266. [ '[]00', '01' ]
  267. ] ) );
  268. } );
  269. it( 'should change heading rows if removing a heading row', () => {
  270. setData( model, modelTable( [
  271. [ '00', '01' ],
  272. [ '[]10', '11' ],
  273. [ '20', '21' ]
  274. ], { headingRows: 2 } ) );
  275. command.execute();
  276. assertEqualMarkup( getData( model ), modelTable( [
  277. [ '00', '01' ],
  278. [ '[]20', '21' ]
  279. ], { headingRows: 1 } ) );
  280. } );
  281. it( 'should decrease rowspan of table cells from previous rows', () => {
  282. setData( model, modelTable( [
  283. [ { rowspan: 4, contents: '00' }, { rowspan: 3, contents: '01' }, { rowspan: 2, contents: '02' }, '03', '04' ],
  284. [ { rowspan: 2, contents: '13' }, '14' ],
  285. [ '22[]', '23', '24' ],
  286. [ '30', '31', '32', '33', '34' ]
  287. ] ) );
  288. command.execute();
  289. assertEqualMarkup( getData( model ), modelTable( [
  290. [ { rowspan: 3, contents: '00' }, { rowspan: 2, contents: '01' }, { rowspan: 2, contents: '02' }, '03', '04' ],
  291. [ '13', '14' ],
  292. [ '30', '31', '[]32', '33', '34' ]
  293. ] ) );
  294. } );
  295. it( 'should move rowspaned cells to row below removing it\'s row', () => {
  296. setData( model, modelTable( [
  297. [ { rowspan: 3, contents: '[]00' }, { rowspan: 2, contents: '01' }, '02' ],
  298. [ '12' ],
  299. [ '22' ],
  300. [ '30', '31', '32' ]
  301. ] ) );
  302. command.execute();
  303. assertEqualMarkup( getData( model ), modelTable( [
  304. [ { rowspan: 2, contents: '[]00' }, '01', '12' ],
  305. [ '22' ],
  306. [ '30', '31', '32' ]
  307. ] ) );
  308. } );
  309. } );
  310. } );