insertrowcommand.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 InsertRowCommand from '../../src/commands/insertrowcommand';
  8. import TableSelection from '../../src/tableselection';
  9. import { assertSelectedCells, defaultConversion, defaultSchema, modelTable } from '../_utils/utils';
  10. import TableUtils from '../../src/tableutils';
  11. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  12. describe( 'InsertRowCommand', () => {
  13. let editor, model, command;
  14. beforeEach( () => {
  15. return ModelTestEditor
  16. .create( {
  17. plugins: [ TableUtils, TableSelection ]
  18. } )
  19. .then( newEditor => {
  20. editor = newEditor;
  21. model = editor.model;
  22. defaultSchema( model.schema );
  23. defaultConversion( editor.conversion );
  24. } );
  25. } );
  26. afterEach( () => {
  27. return editor.destroy();
  28. } );
  29. describe( 'order=below', () => {
  30. beforeEach( () => {
  31. command = new InsertRowCommand( editor );
  32. } );
  33. describe( 'isEnabled', () => {
  34. it( 'should be false if wrong node', () => {
  35. setData( model, '<paragraph>foo[]</paragraph>' );
  36. expect( command.isEnabled ).to.be.false;
  37. } );
  38. it( 'should be true if in table', () => {
  39. setData( model, modelTable( [ [ '[]' ] ] ) );
  40. expect( command.isEnabled ).to.be.true;
  41. } );
  42. } );
  43. describe( 'execute()', () => {
  44. it( 'should insert row after current position', () => {
  45. setData( model, modelTable( [
  46. [ '00[]', '01' ],
  47. [ '10', '11' ]
  48. ] ) );
  49. command.execute();
  50. assertEqualMarkup( getData( model ), modelTable( [
  51. [ '00[]', '01' ],
  52. [ '', '' ],
  53. [ '10', '11' ]
  54. ] ) );
  55. } );
  56. it( 'should insert row after current position (selection in block content)', () => {
  57. setData( model, modelTable( [
  58. [ '00' ],
  59. [ '<paragraph>[]10</paragraph>' ],
  60. [ '20' ]
  61. ] ) );
  62. command.execute();
  63. assertEqualMarkup( getData( model ), modelTable( [
  64. [ '00' ],
  65. [ '<paragraph>[]10</paragraph>' ],
  66. [ '' ],
  67. [ '20' ]
  68. ] ) );
  69. } );
  70. it( 'should update table heading rows attribute when inserting row in headings section', () => {
  71. setData( model, modelTable( [
  72. [ '00[]', '01' ],
  73. [ '10', '11' ],
  74. [ '20', '21' ]
  75. ], { headingRows: 2 } ) );
  76. command.execute();
  77. assertEqualMarkup( getData( model ), modelTable( [
  78. [ '00[]', '01' ],
  79. [ '', '' ],
  80. [ '10', '11' ],
  81. [ '20', '21' ]
  82. ], { headingRows: 3 } ) );
  83. } );
  84. it( 'should not update table heading rows attribute when inserting row after headings section', () => {
  85. setData( model, modelTable( [
  86. [ '00', '01' ],
  87. [ '10[]', '11' ],
  88. [ '20', '21' ]
  89. ], { headingRows: 2 } ) );
  90. command.execute();
  91. assertEqualMarkup( getData( model ), modelTable( [
  92. [ '00', '01' ],
  93. [ '10[]', '11' ],
  94. [ '', '' ],
  95. [ '20', '21' ]
  96. ], { headingRows: 2 } ) );
  97. } );
  98. it( 'should expand rowspan of a cell that overlaps inserted rows', () => {
  99. setData( model, modelTable( [
  100. [ { colspan: 2, contents: '00' }, '02', '03' ],
  101. [ { colspan: 2, rowspan: 4, contents: '10[]' }, '12', '13' ],
  102. [ '22', '23' ]
  103. ], { headingColumns: 3, headingRows: 1 } ) );
  104. command.execute();
  105. assertEqualMarkup( getData( model ), modelTable( [
  106. [ { colspan: 2, contents: '00' }, '02', '03' ],
  107. [ { colspan: 2, rowspan: 5, contents: '10[]' }, '12', '13' ],
  108. [ '', '' ],
  109. [ '22', '23' ]
  110. ], { headingColumns: 3, headingRows: 1 } ) );
  111. } );
  112. it( 'should not expand rowspan of a cell that does not overlaps inserted rows', () => {
  113. setData( model, modelTable( [
  114. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  115. [ '11[]', '12' ],
  116. [ '20', '21', '22' ]
  117. ], { headingColumns: 3, headingRows: 1 } ) );
  118. command.execute();
  119. assertEqualMarkup( getData( model ), modelTable( [
  120. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  121. [ '11[]', '12' ],
  122. [ '', '', '' ],
  123. [ '20', '21', '22' ]
  124. ], { headingColumns: 3, headingRows: 1 } ) );
  125. } );
  126. it( 'should properly calculate columns if next row has colspans', () => {
  127. setData( model, modelTable( [
  128. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  129. [ '11[]', '12' ],
  130. [ { colspan: 3, contents: '20' } ]
  131. ], { headingColumns: 3, headingRows: 1 } ) );
  132. command.execute();
  133. assertEqualMarkup( getData( model ), modelTable( [
  134. [ { rowspan: 2, contents: '00' }, '01', '02' ],
  135. [ '11[]', '12' ],
  136. [ '', '', '' ],
  137. [ { colspan: 3, contents: '20' } ]
  138. ], { headingColumns: 3, headingRows: 1 } ) );
  139. } );
  140. it( 'should insert rows at the end of a table', () => {
  141. setData( model, modelTable( [
  142. [ '00', '01' ],
  143. [ '10[]', '11' ]
  144. ] ) );
  145. command.execute();
  146. assertEqualMarkup( getData( model ), modelTable( [
  147. [ '00', '01' ],
  148. [ '10[]', '11' ],
  149. [ '', '' ]
  150. ] ) );
  151. } );
  152. it( 'should insert a row when multiple rows are selected', () => {
  153. setData( model, modelTable( [
  154. [ '11', '12' ],
  155. [ '21', '22' ],
  156. [ '31', '32' ]
  157. ] ) );
  158. const tableSelection = editor.plugins.get( TableSelection );
  159. const modelRoot = model.document.getRoot();
  160. tableSelection._setCellSelection(
  161. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  162. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  163. );
  164. command.execute();
  165. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  166. [ '11', '12' ],
  167. [ '21', '22' ],
  168. [ '', '' ],
  169. [ '31', '32' ]
  170. ] ) );
  171. assertSelectedCells( model, [
  172. [ 1, 1 ],
  173. [ 1, 1 ],
  174. [ 0, 0 ],
  175. [ 0, 0 ]
  176. ] );
  177. } );
  178. } );
  179. } );
  180. describe( 'order=above', () => {
  181. beforeEach( () => {
  182. command = new InsertRowCommand( editor, { order: 'above' } );
  183. } );
  184. describe( 'isEnabled', () => {
  185. it( 'should be false if wrong node', () => {
  186. setData( model, '<paragraph>foo[]</paragraph>' );
  187. expect( command.isEnabled ).to.be.false;
  188. } );
  189. it( 'should be true if in table', () => {
  190. setData( model, modelTable( [ [ '[]' ] ] ) );
  191. expect( command.isEnabled ).to.be.true;
  192. } );
  193. } );
  194. describe( 'execute()', () => {
  195. it( 'should insert row before current position (selection in block content)', () => {
  196. setData( model, modelTable( [
  197. [ '00' ],
  198. [ '<paragraph>[]10</paragraph>' ],
  199. [ '20' ]
  200. ] ) );
  201. command.execute();
  202. assertEqualMarkup( getData( model ), modelTable( [
  203. [ '00' ],
  204. [ '' ],
  205. [ '<paragraph>[]10</paragraph>' ],
  206. [ '20' ]
  207. ] ) );
  208. } );
  209. it( 'should insert row at the beginning of a table', () => {
  210. setData( model, modelTable( [
  211. [ '00[]', '01' ],
  212. [ '10', '11' ]
  213. ] ) );
  214. command.execute();
  215. assertEqualMarkup( getData( model ), modelTable( [
  216. [ '', '' ],
  217. [ '00[]', '01' ],
  218. [ '10', '11' ]
  219. ] ) );
  220. } );
  221. it( 'should insert row at the end of a table', () => {
  222. setData( model, modelTable( [
  223. [ '00', '01' ],
  224. [ '10', '11' ],
  225. [ '20[]', '21' ]
  226. ] ) );
  227. command.execute();
  228. assertEqualMarkup( getData( model ), modelTable( [
  229. [ '00', '01' ],
  230. [ '10', '11' ],
  231. [ '', '' ],
  232. [ '20[]', '21' ]
  233. ] ) );
  234. } );
  235. it( 'should update table heading rows attribute when inserting row in headings section', () => {
  236. setData( model, modelTable( [
  237. [ '00[]', '01' ],
  238. [ '10', '11' ],
  239. [ '20', '21' ]
  240. ], { headingRows: 2 } ) );
  241. command.execute();
  242. assertEqualMarkup( getData( model ), modelTable( [
  243. [ '', '' ],
  244. [ '00[]', '01' ],
  245. [ '10', '11' ],
  246. [ '20', '21' ]
  247. ], { headingRows: 3 } ) );
  248. } );
  249. it( 'should not update table heading rows attribute when inserting row after headings section', () => {
  250. setData( model, modelTable( [
  251. [ '00', '01' ],
  252. [ '10', '11' ],
  253. [ '20[]', '21' ]
  254. ], { headingRows: 2 } ) );
  255. command.execute();
  256. assertEqualMarkup( getData( model ), modelTable( [
  257. [ '00', '01' ],
  258. [ '10', '11' ],
  259. [ '', '' ],
  260. [ '20[]', '21' ]
  261. ], { headingRows: 2 } ) );
  262. } );
  263. it( 'should insert a row when multiple rows are selected', () => {
  264. setData( model, modelTable( [
  265. [ '11', '12' ],
  266. [ '21', '22' ],
  267. [ '31', '32' ]
  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, 1 ] )
  274. );
  275. command.execute();
  276. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  277. [ '', '' ],
  278. [ '11', '12' ],
  279. [ '21', '22' ],
  280. [ '31', '32' ]
  281. ] ) );
  282. assertSelectedCells( model, [
  283. [ 0, 0 ],
  284. [ 1, 1 ],
  285. [ 1, 1 ],
  286. [ 0, 0 ]
  287. ] );
  288. } );
  289. } );
  290. } );
  291. } );