insertrowcommand.js 9.4 KB

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