removerowcommand.js 12 KB

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