removerowcommand.js 10 KB

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