insertrowcommand.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  161. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
  162. command.execute();
  163. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  164. [ '11', '12' ],
  165. [ '21', '22' ],
  166. [ '', '' ],
  167. [ '31', '32' ]
  168. ] ) );
  169. assertSelectedCells( model, [
  170. [ 1, 1 ],
  171. [ 1, 1 ],
  172. [ 0, 0 ],
  173. [ 0, 0 ]
  174. ] );
  175. } );
  176. } );
  177. } );
  178. describe( 'order=above', () => {
  179. beforeEach( () => {
  180. command = new InsertRowCommand( editor, { order: 'above' } );
  181. } );
  182. describe( 'isEnabled', () => {
  183. it( 'should be false if wrong node', () => {
  184. setData( model, '<paragraph>foo[]</paragraph>' );
  185. expect( command.isEnabled ).to.be.false;
  186. } );
  187. it( 'should be true if in table', () => {
  188. setData( model, modelTable( [ [ '[]' ] ] ) );
  189. expect( command.isEnabled ).to.be.true;
  190. } );
  191. } );
  192. describe( 'execute()', () => {
  193. it( 'should insert row before current position (selection in block content)', () => {
  194. setData( model, modelTable( [
  195. [ '00' ],
  196. [ '<paragraph>[]10</paragraph>' ],
  197. [ '20' ]
  198. ] ) );
  199. command.execute();
  200. assertEqualMarkup( getData( model ), modelTable( [
  201. [ '00' ],
  202. [ '' ],
  203. [ '<paragraph>[]10</paragraph>' ],
  204. [ '20' ]
  205. ] ) );
  206. } );
  207. it( 'should insert row at the beginning of a table', () => {
  208. setData( model, modelTable( [
  209. [ '00[]', '01' ],
  210. [ '10', '11' ]
  211. ] ) );
  212. command.execute();
  213. assertEqualMarkup( getData( model ), modelTable( [
  214. [ '', '' ],
  215. [ '00[]', '01' ],
  216. [ '10', '11' ]
  217. ] ) );
  218. } );
  219. it( 'should insert row at the end of a table', () => {
  220. setData( model, modelTable( [
  221. [ '00', '01' ],
  222. [ '10', '11' ],
  223. [ '20[]', '21' ]
  224. ] ) );
  225. command.execute();
  226. assertEqualMarkup( getData( model ), modelTable( [
  227. [ '00', '01' ],
  228. [ '10', '11' ],
  229. [ '', '' ],
  230. [ '20[]', '21' ]
  231. ] ) );
  232. } );
  233. it( 'should update table heading rows attribute when inserting row in headings section', () => {
  234. setData( model, modelTable( [
  235. [ '00[]', '01' ],
  236. [ '10', '11' ],
  237. [ '20', '21' ]
  238. ], { headingRows: 2 } ) );
  239. command.execute();
  240. assertEqualMarkup( getData( model ), modelTable( [
  241. [ '', '' ],
  242. [ '00[]', '01' ],
  243. [ '10', '11' ],
  244. [ '20', '21' ]
  245. ], { headingRows: 3 } ) );
  246. } );
  247. it( 'should not update table heading rows attribute when inserting row after headings section', () => {
  248. setData( model, modelTable( [
  249. [ '00', '01' ],
  250. [ '10', '11' ],
  251. [ '20[]', '21' ]
  252. ], { headingRows: 2 } ) );
  253. command.execute();
  254. assertEqualMarkup( getData( model ), modelTable( [
  255. [ '00', '01' ],
  256. [ '10', '11' ],
  257. [ '', '' ],
  258. [ '20[]', '21' ]
  259. ], { headingRows: 2 } ) );
  260. } );
  261. it( 'should insert a row when multiple rows are selected', () => {
  262. setData( model, modelTable( [
  263. [ '11', '12' ],
  264. [ '21', '22' ],
  265. [ '31', '32' ]
  266. ] ) );
  267. const tableSelection = editor.plugins.get( TableSelection );
  268. const modelRoot = model.document.getRoot();
  269. tableSelection.startSelectingFrom( modelRoot.getNodeByPath( [ 0, 0, 0 ] ) );
  270. tableSelection.setSelectingTo( modelRoot.getNodeByPath( [ 0, 1, 1 ] ) );
  271. command.execute();
  272. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  273. [ '', '' ],
  274. [ '11', '12' ],
  275. [ '21', '22' ],
  276. [ '31', '32' ]
  277. ] ) );
  278. assertSelectedCells( model, [
  279. [ 0, 0 ],
  280. [ 1, 1 ],
  281. [ 1, 1 ],
  282. [ 0, 0 ]
  283. ] );
  284. } );
  285. } );
  286. } );
  287. } );