removerowcommand.js 9.5 KB

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