removerowcommand.js 9.9 KB

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