removerowcommand.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  6. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  7. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  8. import RemoveRowCommand from '../../src/commands/removerowcommand';
  9. import TableSelection from '../../src/tableselection';
  10. import { modelTable, viewTable } from '../_utils/utils';
  11. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  12. import TableEditing from '../../src/tableediting';
  13. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  14. describe( 'RemoveRowCommand', () => {
  15. let editor, model, command;
  16. beforeEach( async () => {
  17. editor = await VirtualTestEditor.create( { plugins: [ Paragraph, TableEditing, TableSelection ] } );
  18. model = editor.model;
  19. command = new RemoveRowCommand( editor );
  20. } );
  21. afterEach( () => {
  22. return editor.destroy();
  23. } );
  24. describe( 'isEnabled', () => {
  25. it( 'should be true if selection is inside table cell', () => {
  26. setData( model, modelTable( [
  27. [ '00[]', '01' ],
  28. [ '10', '11' ]
  29. ] ) );
  30. expect( command.isEnabled ).to.be.true;
  31. } );
  32. it( 'should be true if selection contains multiple cells', () => {
  33. setData( model, modelTable( [
  34. [ '00', '01' ],
  35. [ '10', '11' ],
  36. [ '20', '21' ]
  37. ] ) );
  38. const tableSelection = editor.plugins.get( TableSelection );
  39. const modelRoot = model.document.getRoot();
  40. tableSelection._setCellSelection(
  41. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  42. modelRoot.getNodeByPath( [ 0, 0, 1 ] )
  43. );
  44. expect( command.isEnabled ).to.be.true;
  45. } );
  46. it( 'should be false if selection is inside table with one row only', () => {
  47. setData( model, modelTable( [
  48. [ '00[]', '01' ]
  49. ] ) );
  50. expect( command.isEnabled ).to.be.false;
  51. } );
  52. it( 'should be false if all the rows are selected', () => {
  53. setData( model, modelTable( [
  54. [ '00', '01' ],
  55. [ '10', '11' ]
  56. ] ) );
  57. const tableSelection = editor.plugins.get( TableSelection );
  58. const modelRoot = model.document.getRoot();
  59. tableSelection._setCellSelection(
  60. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  61. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  62. );
  63. expect( command.isEnabled ).to.be.false;
  64. } );
  65. it( 'should be false if selection is outside a table', () => {
  66. setData( model, '<paragraph>11[]</paragraph>' );
  67. expect( command.isEnabled ).to.be.false;
  68. } );
  69. it( 'should be false when the first column with rowspan is selected', () => {
  70. // (#6427)
  71. setData( model, modelTable( [
  72. [ { rowspan: 2, contents: '00' }, '01' ],
  73. [ '11' ],
  74. [ '20', '21' ]
  75. ] ) );
  76. const tableSelection = editor.plugins.get( TableSelection );
  77. const modelRoot = model.document.getRoot();
  78. tableSelection._setCellSelection(
  79. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  80. modelRoot.getNodeByPath( [ 0, 2, 0 ] )
  81. );
  82. expect( command.isEnabled ).to.be.false;
  83. } );
  84. } );
  85. describe( 'execute()', () => {
  86. it( 'should remove a given row', () => {
  87. setData( model, modelTable( [
  88. [ '00', '01' ],
  89. [ '[]10', '11' ],
  90. [ '20', '21' ]
  91. ] ) );
  92. command.execute();
  93. assertEqualMarkup( getData( model ), modelTable( [
  94. [ '00', '01' ],
  95. [ '[]20', '21' ]
  96. ] ) );
  97. } );
  98. describe( 'with multiple rows selected', () => {
  99. it( 'should properly remove middle rows', () => {
  100. setData( model, modelTable( [
  101. [ '00', '01' ],
  102. [ '10', '11' ],
  103. [ '20', '21' ],
  104. [ '30', '31' ]
  105. ] ) );
  106. const tableSelection = editor.plugins.get( TableSelection );
  107. const modelRoot = model.document.getRoot();
  108. tableSelection._setCellSelection(
  109. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  110. modelRoot.getNodeByPath( [ 0, 2, 0 ] )
  111. );
  112. command.execute();
  113. assertEqualMarkup( getData( model ), modelTable( [
  114. [ '00', '01' ],
  115. [ '[]30', '31' ]
  116. ] ) );
  117. } );
  118. it( 'should properly remove middle rows in reversed order', () => {
  119. setData( model, modelTable( [
  120. [ '00', '01' ],
  121. [ '10', '11' ],
  122. [ '20', '21' ],
  123. [ '30', '31' ]
  124. ] ) );
  125. const tableSelection = editor.plugins.get( TableSelection );
  126. const modelRoot = model.document.getRoot();
  127. tableSelection._setCellSelection(
  128. modelRoot.getNodeByPath( [ 0, 2, 0 ] ),
  129. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  130. );
  131. command.execute();
  132. assertEqualMarkup( getData( model ), modelTable( [
  133. [ '00', '01' ],
  134. [ '[]30', '31' ]
  135. ] ) );
  136. } );
  137. it( 'should properly remove tailing rows', () => {
  138. setData( model, modelTable( [
  139. [ '00', '01' ],
  140. [ '10', '11' ],
  141. [ '20', '21' ],
  142. [ '30', '31' ]
  143. ] ) );
  144. const tableSelection = editor.plugins.get( TableSelection );
  145. const modelRoot = model.document.getRoot();
  146. tableSelection._setCellSelection(
  147. modelRoot.getNodeByPath( [ 0, 2, 0 ] ),
  148. modelRoot.getNodeByPath( [ 0, 3, 0 ] )
  149. );
  150. command.execute();
  151. assertEqualMarkup( getData( model ), modelTable( [
  152. [ '00', '01' ],
  153. [ '[]10', '11' ]
  154. ] ) );
  155. } );
  156. it( 'should properly remove beginning rows', () => {
  157. setData( model, modelTable( [
  158. [ '00', '01' ],
  159. [ '10', '11' ],
  160. [ '20', '21' ],
  161. [ '30', '31' ]
  162. ] ) );
  163. const tableSelection = editor.plugins.get( TableSelection );
  164. const modelRoot = model.document.getRoot();
  165. tableSelection._setCellSelection(
  166. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  167. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  168. );
  169. command.execute();
  170. assertEqualMarkup( getData( model ), modelTable( [
  171. [ '[]20', '21' ],
  172. [ '30', '31' ]
  173. ] ) );
  174. } );
  175. it( 'should support removing multiple headings (removed rows in heading section)', () => {
  176. setData( model, modelTable( [
  177. [ '00', '01' ],
  178. [ '10', '11' ],
  179. [ '20', '21' ],
  180. [ '30', '31' ]
  181. ], { headingRows: 3 } ) );
  182. const tableSelection = editor.plugins.get( TableSelection );
  183. const modelRoot = model.document.getRoot();
  184. tableSelection._setCellSelection(
  185. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  186. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  187. );
  188. command.execute();
  189. assertEqualMarkup( getData( model ), modelTable( [
  190. [ '[]20', '21' ],
  191. [ '30', '31' ]
  192. ], { headingRows: 1 } ) );
  193. } );
  194. it( 'should support removing multiple headings (removed rows in heading and body section)', () => {
  195. setData( model, modelTable( [
  196. [ '00', '01' ],
  197. [ '10', '11' ],
  198. [ '20', '21' ],
  199. [ '30', '31' ],
  200. [ '40', '41' ]
  201. ], { headingRows: 3 } ) );
  202. const tableSelection = editor.plugins.get( TableSelection );
  203. const modelRoot = model.document.getRoot();
  204. tableSelection._setCellSelection(
  205. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  206. modelRoot.getNodeByPath( [ 0, 3, 0 ] )
  207. );
  208. command.execute();
  209. assertEqualMarkup( getData( model ), modelTable( [
  210. [ '00', '01' ],
  211. [ '[]40', '41' ]
  212. ], { headingRows: 1 } ) );
  213. // The editing view should also be properly downcasted.
  214. assertEqualMarkup( getViewData( editor.editing.view, { withoutSelection: true } ), viewTable( [
  215. [ '00', '01' ],
  216. [ '40', '41' ]
  217. ], { headingRows: 1, asWidget: true } ) );
  218. } );
  219. it( 'should support removing mixed heading and cell rows', () => {
  220. setData( model, modelTable( [
  221. [ '00', '01' ],
  222. [ '10', '11' ],
  223. [ '20', '21' ]
  224. ], { headingRows: 1 } ) );
  225. const tableSelection = editor.plugins.get( TableSelection );
  226. const modelRoot = model.document.getRoot();
  227. tableSelection._setCellSelection(
  228. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  229. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  230. );
  231. command.execute();
  232. assertEqualMarkup( getData( model ), modelTable( [
  233. [ '[]20', '21' ]
  234. ] ) );
  235. } );
  236. it( 'should properly calculate truncated rowspans', () => {
  237. setData( model, modelTable( [
  238. [ '00', { contents: '01', rowspan: 3 } ],
  239. [ '10' ],
  240. [ '20' ]
  241. ] ) );
  242. const tableSelection = editor.plugins.get( TableSelection );
  243. const modelRoot = model.document.getRoot();
  244. tableSelection._setCellSelection(
  245. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  246. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  247. );
  248. command.execute();
  249. assertEqualMarkup( getData( model ), modelTable( [
  250. [ '[]20', '01' ]
  251. ] ) );
  252. } );
  253. it( 'should create one undo step (1 batch)', () => {
  254. setData( model, modelTable( [
  255. [ '00', '01' ],
  256. [ '10', '11' ],
  257. [ '20', '21' ],
  258. [ '30', '31' ]
  259. ], { headingRows: 3 } ) );
  260. const createdBatches = new Set();
  261. model.on( 'applyOperation', ( evt, args ) => {
  262. const operation = args[ 0 ];
  263. createdBatches.add( operation.batch );
  264. } );
  265. const tableSelection = editor.plugins.get( TableSelection );
  266. const modelRoot = model.document.getRoot();
  267. tableSelection._setCellSelection(
  268. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  269. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  270. );
  271. command.execute();
  272. expect( createdBatches.size ).to.equal( 1 );
  273. } );
  274. } );
  275. describe( 'with entire row selected', () => {
  276. it( 'should remove a row if all its cells are selected', () => {
  277. setData( model, modelTable( [
  278. [ '00', '01' ],
  279. [ '10', '11' ],
  280. [ '20', '21' ]
  281. ] ) );
  282. const tableSelection = editor.plugins.get( TableSelection );
  283. const modelRoot = model.document.getRoot();
  284. tableSelection._setCellSelection(
  285. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  286. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  287. );
  288. command.execute();
  289. assertEqualMarkup( getData( model ), modelTable( [
  290. [ '00', '01' ],
  291. [ '[]20', '21' ]
  292. ] ) );
  293. } );
  294. it( 'should properly remove row if reversed selection is made', () => {
  295. setData( model, modelTable( [
  296. [ '00', '01' ],
  297. [ '10', '11' ]
  298. ] ) );
  299. const tableSelection = editor.plugins.get( TableSelection );
  300. const modelRoot = model.document.getRoot();
  301. tableSelection._setCellSelection(
  302. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  303. modelRoot.getNodeByPath( [ 0, 0, 0 ] )
  304. );
  305. command.execute();
  306. assertEqualMarkup( getData( model ), modelTable( [
  307. [ '[]10', '11' ]
  308. ] ) );
  309. } );
  310. } );
  311. it( 'should remove a given row from a table start', () => {
  312. setData( model, modelTable( [
  313. [ '[]00', '01' ],
  314. [ '10', '11' ],
  315. [ '20', '21' ]
  316. ] ) );
  317. command.execute();
  318. assertEqualMarkup( getData( model ), modelTable( [
  319. [ '[]10', '11' ],
  320. [ '20', '21' ]
  321. ] ) );
  322. } );
  323. it( 'should remove a given row from a table start when selection is at the end', () => {
  324. setData( model, modelTable( [
  325. [ '00', '01[]' ],
  326. [ '10', '11' ],
  327. [ '20', '21' ]
  328. ] ) );
  329. command.execute();
  330. assertEqualMarkup( getData( model ), modelTable( [
  331. [ '10', '[]11' ],
  332. [ '20', '21' ]
  333. ] ) );
  334. } );
  335. it( 'should remove last row', () => {
  336. setData( model, modelTable( [
  337. [ '00', '01' ],
  338. [ '[]10', '11' ]
  339. ] ) );
  340. command.execute();
  341. assertEqualMarkup( getData( model ), modelTable( [
  342. [ '[]00', '01' ]
  343. ] ) );
  344. } );
  345. it( 'should change heading rows if removing a heading row', () => {
  346. setData( model, modelTable( [
  347. [ '00', '01' ],
  348. [ '[]10', '11' ],
  349. [ '20', '21' ]
  350. ], { headingRows: 2 } ) );
  351. command.execute();
  352. assertEqualMarkup( getData( model ), modelTable( [
  353. [ '00', '01' ],
  354. [ '[]20', '21' ]
  355. ], { headingRows: 1 } ) );
  356. } );
  357. it( 'should decrease rowspan of table cells from previous rows', () => {
  358. setData( model, modelTable( [
  359. [ { rowspan: 4, contents: '00' }, { rowspan: 3, contents: '01' }, { rowspan: 2, contents: '02' }, '03', '04' ],
  360. [ { rowspan: 2, contents: '13' }, '14' ],
  361. [ '22[]', '24' ],
  362. [ '31', '32', '33', '34' ]
  363. ] ) );
  364. command.execute();
  365. assertEqualMarkup( getData( model ), modelTable( [
  366. [ { rowspan: 3, contents: '00' }, { rowspan: 2, contents: '01' }, { rowspan: 2, contents: '02' }, '03', '04' ],
  367. [ '13', '14' ],
  368. [ '31', '32', '[]33', '34' ]
  369. ] ) );
  370. } );
  371. it( 'should move rowspaned cells to row below removing it\'s row', () => {
  372. setData( model, modelTable( [
  373. [ { rowspan: 3, contents: '[]00' }, { rowspan: 2, contents: '01' }, '02' ],
  374. [ '12' ],
  375. [ '21', '22' ],
  376. [ '30', '31', '32' ]
  377. ] ) );
  378. command.execute();
  379. assertEqualMarkup( getData( model ), modelTable( [
  380. [ { rowspan: 2, contents: '[]00' }, '01', '12' ],
  381. [ '21', '22' ],
  382. [ '30', '31', '32' ]
  383. ] ) );
  384. } );
  385. } );
  386. } );