removerowcommand.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. it( 'should be false if all the rows are selected - table with more than 10 rows (array sort bug)', () => {
  88. setData( model, modelTable( [
  89. [ '0' ],
  90. [ '1' ],
  91. [ '2' ],
  92. [ '3' ],
  93. [ '4' ],
  94. [ '5' ],
  95. [ '6' ],
  96. [ '7' ],
  97. [ '8' ],
  98. [ '9' ],
  99. [ '10' ],
  100. [ '11' ],
  101. [ '12' ]
  102. ] ) );
  103. const tableSelection = editor.plugins.get( TableSelection );
  104. const modelRoot = model.document.getRoot();
  105. tableSelection._setCellSelection(
  106. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  107. modelRoot.getNodeByPath( [ 0, 12, 0 ] )
  108. );
  109. expect( command.isEnabled ).to.be.false;
  110. } );
  111. } );
  112. describe( 'execute()', () => {
  113. it( 'should remove a given row', () => {
  114. setData( model, modelTable( [
  115. [ '00', '01' ],
  116. [ '[]10', '11' ],
  117. [ '20', '21' ]
  118. ] ) );
  119. command.execute();
  120. assertEqualMarkup( getData( model ), modelTable( [
  121. [ '00', '01' ],
  122. [ '[]20', '21' ]
  123. ] ) );
  124. } );
  125. describe( 'with multiple rows selected', () => {
  126. it( 'should properly remove middle rows', () => {
  127. setData( model, modelTable( [
  128. [ '00', '01' ],
  129. [ '10', '11' ],
  130. [ '20', '21' ],
  131. [ '30', '31' ]
  132. ] ) );
  133. const tableSelection = editor.plugins.get( TableSelection );
  134. const modelRoot = model.document.getRoot();
  135. tableSelection._setCellSelection(
  136. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  137. modelRoot.getNodeByPath( [ 0, 2, 0 ] )
  138. );
  139. command.execute();
  140. assertEqualMarkup( getData( model ), modelTable( [
  141. [ '00', '01' ],
  142. [ '[]30', '31' ]
  143. ] ) );
  144. } );
  145. it( 'should properly remove middle rows in reversed order', () => {
  146. setData( model, modelTable( [
  147. [ '00', '01' ],
  148. [ '10', '11' ],
  149. [ '20', '21' ],
  150. [ '30', '31' ]
  151. ] ) );
  152. const tableSelection = editor.plugins.get( TableSelection );
  153. const modelRoot = model.document.getRoot();
  154. tableSelection._setCellSelection(
  155. modelRoot.getNodeByPath( [ 0, 2, 0 ] ),
  156. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  157. );
  158. command.execute();
  159. assertEqualMarkup( getData( model ), modelTable( [
  160. [ '00', '01' ],
  161. [ '[]30', '31' ]
  162. ] ) );
  163. } );
  164. it( 'should properly remove tailing rows', () => {
  165. setData( model, modelTable( [
  166. [ '00', '01' ],
  167. [ '10', '11' ],
  168. [ '20', '21' ],
  169. [ '30', '31' ]
  170. ] ) );
  171. const tableSelection = editor.plugins.get( TableSelection );
  172. const modelRoot = model.document.getRoot();
  173. tableSelection._setCellSelection(
  174. modelRoot.getNodeByPath( [ 0, 2, 0 ] ),
  175. modelRoot.getNodeByPath( [ 0, 3, 0 ] )
  176. );
  177. command.execute();
  178. assertEqualMarkup( getData( model ), modelTable( [
  179. [ '00', '01' ],
  180. [ '[]10', '11' ]
  181. ] ) );
  182. } );
  183. it( 'should properly remove beginning rows', () => {
  184. setData( model, modelTable( [
  185. [ '00', '01' ],
  186. [ '10', '11' ],
  187. [ '20', '21' ],
  188. [ '30', '31' ]
  189. ] ) );
  190. const tableSelection = editor.plugins.get( TableSelection );
  191. const modelRoot = model.document.getRoot();
  192. tableSelection._setCellSelection(
  193. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  194. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  195. );
  196. command.execute();
  197. assertEqualMarkup( getData( model ), modelTable( [
  198. [ '[]20', '21' ],
  199. [ '30', '31' ]
  200. ] ) );
  201. } );
  202. it( 'should support removing multiple headings (removed rows in heading section)', () => {
  203. setData( model, modelTable( [
  204. [ '00', '01' ],
  205. [ '10', '11' ],
  206. [ '20', '21' ],
  207. [ '30', '31' ]
  208. ], { headingRows: 3 } ) );
  209. const tableSelection = editor.plugins.get( TableSelection );
  210. const modelRoot = model.document.getRoot();
  211. tableSelection._setCellSelection(
  212. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  213. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  214. );
  215. command.execute();
  216. assertEqualMarkup( getData( model ), modelTable( [
  217. [ '[]20', '21' ],
  218. [ '30', '31' ]
  219. ], { headingRows: 1 } ) );
  220. } );
  221. it( 'should support removing multiple headings (removed rows in heading and body section)', () => {
  222. setData( model, modelTable( [
  223. [ '00', '01' ],
  224. [ '10', '11' ],
  225. [ '20', '21' ],
  226. [ '30', '31' ],
  227. [ '40', '41' ]
  228. ], { headingRows: 3 } ) );
  229. const tableSelection = editor.plugins.get( TableSelection );
  230. const modelRoot = model.document.getRoot();
  231. tableSelection._setCellSelection(
  232. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  233. modelRoot.getNodeByPath( [ 0, 3, 0 ] )
  234. );
  235. command.execute();
  236. assertEqualMarkup( getData( model ), modelTable( [
  237. [ '00', '01' ],
  238. [ '[]40', '41' ]
  239. ], { headingRows: 1 } ) );
  240. // The view should also be properly downcasted.
  241. assertEqualMarkup( getViewData( editor.editing.view, { withoutSelection: true } ), viewTable( [
  242. [ '00', '01' ],
  243. [ '40', '41' ]
  244. ], { headingRows: 1 } ) );
  245. } );
  246. it( 'should support removing mixed heading and cell rows', () => {
  247. setData( model, modelTable( [
  248. [ '00', '01' ],
  249. [ '10', '11' ],
  250. [ '20', '21' ]
  251. ], { headingRows: 1 } ) );
  252. const tableSelection = editor.plugins.get( TableSelection );
  253. const modelRoot = model.document.getRoot();
  254. tableSelection._setCellSelection(
  255. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  256. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  257. );
  258. command.execute();
  259. assertEqualMarkup( getData( model ), modelTable( [
  260. [ '[]20', '21' ]
  261. ] ) );
  262. } );
  263. it( 'should properly calculate truncated rowspans', () => {
  264. setData( model, modelTable( [
  265. [ '00', { contents: '01', rowspan: 3 } ],
  266. [ '10' ],
  267. [ '20' ]
  268. ] ) );
  269. const tableSelection = editor.plugins.get( TableSelection );
  270. const modelRoot = model.document.getRoot();
  271. tableSelection._setCellSelection(
  272. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  273. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  274. );
  275. command.execute();
  276. assertEqualMarkup( getData( model ), modelTable( [
  277. [ '[]20', '01' ]
  278. ] ) );
  279. } );
  280. it( 'should create one undo step (1 batch)', () => {
  281. setData( model, modelTable( [
  282. [ '00', '01' ],
  283. [ '10', '11' ],
  284. [ '20', '21' ],
  285. [ '30', '31' ]
  286. ], { headingRows: 3 } ) );
  287. const createdBatches = new Set();
  288. model.on( 'applyOperation', ( evt, args ) => {
  289. const operation = args[ 0 ];
  290. createdBatches.add( operation.batch );
  291. } );
  292. const tableSelection = editor.plugins.get( TableSelection );
  293. const modelRoot = model.document.getRoot();
  294. tableSelection._setCellSelection(
  295. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  296. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  297. );
  298. command.execute();
  299. expect( createdBatches.size ).to.equal( 1 );
  300. } );
  301. it( 'should properly remove more than 10 rows selected (array sort bug)', () => {
  302. setData( model, modelTable( [
  303. [ '0' ],
  304. [ '1' ],
  305. [ '2' ],
  306. [ '3' ],
  307. [ '4' ],
  308. [ '5' ],
  309. [ '6' ],
  310. [ '7' ],
  311. [ '8' ],
  312. [ '9' ],
  313. [ '10' ],
  314. [ '11' ],
  315. [ '12' ],
  316. [ '13' ],
  317. [ '14' ]
  318. ] ) );
  319. const tableSelection = editor.plugins.get( TableSelection );
  320. const modelRoot = model.document.getRoot();
  321. tableSelection._setCellSelection(
  322. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  323. modelRoot.getNodeByPath( [ 0, 12, 0 ] )
  324. );
  325. command.execute();
  326. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  327. [ '0' ],
  328. [ '13' ],
  329. [ '14' ]
  330. ] ) );
  331. } );
  332. } );
  333. describe( 'with entire row selected', () => {
  334. it( 'should remove a row if all its cells are selected', () => {
  335. setData( model, modelTable( [
  336. [ '00', '01' ],
  337. [ '10', '11' ],
  338. [ '20', '21' ]
  339. ] ) );
  340. const tableSelection = editor.plugins.get( TableSelection );
  341. const modelRoot = model.document.getRoot();
  342. tableSelection._setCellSelection(
  343. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  344. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  345. );
  346. command.execute();
  347. assertEqualMarkup( getData( model ), modelTable( [
  348. [ '00', '01' ],
  349. [ '[]20', '21' ]
  350. ] ) );
  351. } );
  352. it( 'should properly remove row if reversed selection is made', () => {
  353. setData( model, modelTable( [
  354. [ '00', '01' ],
  355. [ '10', '11' ]
  356. ] ) );
  357. const tableSelection = editor.plugins.get( TableSelection );
  358. const modelRoot = model.document.getRoot();
  359. tableSelection._setCellSelection(
  360. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  361. modelRoot.getNodeByPath( [ 0, 0, 0 ] )
  362. );
  363. command.execute();
  364. assertEqualMarkup( getData( model ), modelTable( [
  365. [ '[]10', '11' ]
  366. ] ) );
  367. } );
  368. } );
  369. it( 'should remove a given row from a table start', () => {
  370. setData( model, modelTable( [
  371. [ '[]00', '01' ],
  372. [ '10', '11' ],
  373. [ '20', '21' ]
  374. ] ) );
  375. command.execute();
  376. assertEqualMarkup( getData( model ), modelTable( [
  377. [ '[]10', '11' ],
  378. [ '20', '21' ]
  379. ] ) );
  380. } );
  381. it( 'should remove a given row from a table start when selection is at the end', () => {
  382. setData( model, modelTable( [
  383. [ '00', '01[]' ],
  384. [ '10', '11' ],
  385. [ '20', '21' ]
  386. ] ) );
  387. command.execute();
  388. assertEqualMarkup( getData( model ), modelTable( [
  389. [ '10', '[]11' ],
  390. [ '20', '21' ]
  391. ] ) );
  392. } );
  393. it( 'should remove last row', () => {
  394. setData( model, modelTable( [
  395. [ '00', '01' ],
  396. [ '[]10', '11' ]
  397. ] ) );
  398. command.execute();
  399. assertEqualMarkup( getData( model ), modelTable( [
  400. [ '[]00', '01' ]
  401. ] ) );
  402. } );
  403. it( 'should change heading rows if removing a heading row', () => {
  404. setData( model, modelTable( [
  405. [ '00', '01' ],
  406. [ '[]10', '11' ],
  407. [ '20', '21' ]
  408. ], { headingRows: 2 } ) );
  409. command.execute();
  410. assertEqualMarkup( getData( model ), modelTable( [
  411. [ '00', '01' ],
  412. [ '[]20', '21' ]
  413. ], { headingRows: 1 } ) );
  414. } );
  415. it( 'should decrease rowspan of table cells from previous rows', () => {
  416. setData( model, modelTable( [
  417. [ { rowspan: 4, contents: '00' }, { rowspan: 3, contents: '01' }, { rowspan: 2, contents: '02' }, '03', '04' ],
  418. [ { rowspan: 2, contents: '13' }, '14' ],
  419. [ '22[]', '23', '24' ],
  420. [ '30', '31', '32', '33', '34' ]
  421. ] ) );
  422. command.execute();
  423. assertEqualMarkup( getData( model ), modelTable( [
  424. [ { rowspan: 3, contents: '00' }, { rowspan: 2, contents: '01' }, { rowspan: 2, contents: '02' }, '03', '04' ],
  425. [ '13', '14' ],
  426. [ '30', '31', '[]32', '33', '34' ]
  427. ] ) );
  428. } );
  429. it( 'should move rowspaned cells to row below removing it\'s row', () => {
  430. setData( model, modelTable( [
  431. [ { rowspan: 3, contents: '[]00' }, { rowspan: 2, contents: '01' }, '02' ],
  432. [ '12' ],
  433. [ '21', '22' ],
  434. [ '30', '31', '32' ]
  435. ] ) );
  436. command.execute();
  437. assertEqualMarkup( getData( model ), modelTable( [
  438. [ { rowspan: 2, contents: '[]00' }, '01', '12' ],
  439. [ '21', '22' ],
  440. [ '30', '31', '32' ]
  441. ] ) );
  442. } );
  443. } );
  444. } );