removerowcommand.js 10 KB

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