insertrowcommand.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  8. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  9. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  10. import TableEditing from '../../src/tableediting';
  11. import TableSelection from '../../src/tableselection';
  12. import { assertSelectedCells, modelTable } from '../_utils/utils';
  13. import InsertRowCommand from '../../src/commands/insertrowcommand';
  14. describe( 'InsertRowCommand', () => {
  15. let editor, model, command;
  16. beforeEach( () => {
  17. return ModelTestEditor
  18. .create( {
  19. plugins: [ Paragraph, TableEditing, TableSelection, HorizontalLineEditing ]
  20. } )
  21. .then( newEditor => {
  22. editor = newEditor;
  23. model = editor.model;
  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. // +----+----+----+----+
  100. // | 00 | 02 | 03 |
  101. // +----+----+----+----+ <-- heading rows
  102. // | 10 | 12 | 13 |
  103. // + +----+----+
  104. // | | 22 | 23 |
  105. // +----+----+----+----+
  106. // ^-- heading columns
  107. setData( model, modelTable( [
  108. [ { contents: '00', colspan: 2 }, '02', '03' ],
  109. [ { contents: '10[]', colspan: 2, rowspan: 2 }, '12', '13' ],
  110. [ '22', '23' ]
  111. ], { headingColumns: 3, headingRows: 1 } ) );
  112. command.execute();
  113. // +----+----+----+----+
  114. // | 00 | 02 | 03 |
  115. // +----+----+----+----+ <-- heading rows
  116. // | 10 | 12 | 13 |
  117. // + +----+----+
  118. // | | | |
  119. // + +----+----+
  120. // | | 22 | 23 |
  121. // +----+----+----+----+
  122. // ^-- heading columns
  123. assertEqualMarkup( getData( model ), modelTable( [
  124. [ { contents: '00', colspan: 2 }, '02', '03' ],
  125. [ { contents: '10[]', colspan: 2, rowspan: 3 }, '12', '13' ],
  126. [ '', '' ],
  127. [ '22', '23' ]
  128. ], { headingColumns: 3, headingRows: 1 } ) );
  129. } );
  130. it( 'should not expand rowspan of a cell that does not overlaps inserted rows', () => {
  131. // +----+----+----+
  132. // | 00 | 01 | 02 |
  133. // + +----+----+
  134. // | | 11 | 12 |
  135. // +----+----+----+ <-- heading rows
  136. // | 20 | 21 | 22 |
  137. // +----+----+----+
  138. setData( model, modelTable( [
  139. [ { contents: '00', rowspan: 2 }, '01', '02' ],
  140. [ '11[]', '12' ],
  141. [ '20', '21', '22' ]
  142. ], { headingRows: 2 } ) );
  143. command.execute();
  144. // +----+----+----+
  145. // | 00 | 01 | 02 |
  146. // + +----+----+
  147. // | | 11 | 12 |
  148. // +----+----+----+ <-- heading rows
  149. // | | | |
  150. // +----+----+----+
  151. // | 20 | 21 | 22 |
  152. // +----+----+----+
  153. assertEqualMarkup( getData( model ), modelTable( [
  154. [ { contents: '00', rowspan: 2 }, '01', '02' ],
  155. [ '11[]', '12' ],
  156. [ '', '', '' ],
  157. [ '20', '21', '22' ]
  158. ], { headingRows: 2 } ) );
  159. } );
  160. it( 'should properly calculate columns if next row has colspans', () => {
  161. // +----+----+----+
  162. // | 00 | 01 | 02 |
  163. // + +----+----+
  164. // | | 11 | 12 |
  165. // +----+----+----+ <-- heading rows
  166. // | 20 |
  167. // +----+----+----+
  168. setData( model, modelTable( [
  169. [ { contents: '00', rowspan: 2 }, '01', '02' ],
  170. [ '11[]', '12' ],
  171. [ { contents: '20', colspan: 3 } ]
  172. ], { headingRows: 2 } ) );
  173. command.execute();
  174. // +----+----+----+
  175. // | 00 | 01 | 02 |
  176. // + +----+----+
  177. // | | 11 | 12 |
  178. // +----+----+----+ <-- heading rows
  179. // | | | |
  180. // +----+----+----+
  181. // | 20 |
  182. // +----+----+----+
  183. assertEqualMarkup( getData( model ), modelTable( [
  184. [ { contents: '00', rowspan: 2 }, '01', '02' ],
  185. [ '11[]', '12' ],
  186. [ '', '', '' ],
  187. [ { contents: '20', colspan: 3 } ]
  188. ], { headingRows: 2 } ) );
  189. } );
  190. it( 'should insert rows at the end of a table', () => {
  191. setData( model, modelTable( [
  192. [ '00', '01' ],
  193. [ '10[]', '11' ]
  194. ] ) );
  195. command.execute();
  196. assertEqualMarkup( getData( model ), modelTable( [
  197. [ '00', '01' ],
  198. [ '10[]', '11' ],
  199. [ '', '' ]
  200. ] ) );
  201. } );
  202. it( 'should insert a row when multiple rows are selected', () => {
  203. setData( model, modelTable( [
  204. [ '11', '12' ],
  205. [ '21', '22' ],
  206. [ '31', '32' ]
  207. ] ) );
  208. const tableSelection = editor.plugins.get( TableSelection );
  209. const modelRoot = model.document.getRoot();
  210. tableSelection.setCellSelection(
  211. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  212. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  213. );
  214. command.execute();
  215. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  216. [ '11', '12' ],
  217. [ '21', '22' ],
  218. [ '', '' ],
  219. [ '31', '32' ]
  220. ] ) );
  221. assertSelectedCells( model, [
  222. [ 1, 1 ],
  223. [ 1, 1 ],
  224. [ 0, 0 ],
  225. [ 0, 0 ]
  226. ] );
  227. } );
  228. it( 'should insert a row when a widget in the table cell is selected', () => {
  229. setData( model, modelTable( [
  230. [ '11', '12' ],
  231. [ '21', '22' ],
  232. [ '31', '[<horizontalLine></horizontalLine>]' ]
  233. ] ) );
  234. command.execute();
  235. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  236. [ '11', '12' ],
  237. [ '21', '22' ],
  238. [ '31', '<horizontalLine></horizontalLine>' ],
  239. [ '', '' ]
  240. ] ) );
  241. } );
  242. it( 'should copy the row structure from the selected row', () => {
  243. // +----+----+----+
  244. // | 00 | 01 |
  245. // +----+----+----+
  246. // | 10 | 11 | 12 |
  247. // +----+----+----+
  248. setData( model, modelTable( [
  249. [ '[]00', { contents: '01', colspan: 2 } ],
  250. [ '10', '11', '12' ]
  251. ] ) );
  252. command.execute();
  253. // +----+----+----+
  254. // | 00 | 01 |
  255. // +----+----+----+
  256. // | | |
  257. // +----+----+----+
  258. // | 10 | 11 | 12 |
  259. // +----+----+----+
  260. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  261. [ '00', { contents: '01', colspan: 2 } ],
  262. [ '', { contents: '', colspan: 2 } ],
  263. [ '10', '11', '12' ]
  264. ] ) );
  265. } );
  266. } );
  267. } );
  268. describe( 'order=above', () => {
  269. beforeEach( () => {
  270. command = new InsertRowCommand( editor, { order: 'above' } );
  271. } );
  272. describe( 'isEnabled', () => {
  273. it( 'should be false if wrong node', () => {
  274. setData( model, '<paragraph>foo[]</paragraph>' );
  275. expect( command.isEnabled ).to.be.false;
  276. } );
  277. it( 'should be true if in table', () => {
  278. setData( model, modelTable( [ [ '[]' ] ] ) );
  279. expect( command.isEnabled ).to.be.true;
  280. } );
  281. } );
  282. describe( 'execute()', () => {
  283. it( 'should insert row before current position (selection in block content)', () => {
  284. setData( model, modelTable( [
  285. [ '00' ],
  286. [ '<paragraph>[]10</paragraph>' ],
  287. [ '20' ]
  288. ] ) );
  289. command.execute();
  290. assertEqualMarkup( getData( model ), modelTable( [
  291. [ '00' ],
  292. [ '' ],
  293. [ '<paragraph>[]10</paragraph>' ],
  294. [ '20' ]
  295. ] ) );
  296. } );
  297. it( 'should insert row at the beginning of a table', () => {
  298. setData( model, modelTable( [
  299. [ '00[]', '01' ],
  300. [ '10', '11' ]
  301. ] ) );
  302. command.execute();
  303. assertEqualMarkup( getData( model ), modelTable( [
  304. [ '', '' ],
  305. [ '00[]', '01' ],
  306. [ '10', '11' ]
  307. ] ) );
  308. } );
  309. it( 'should insert row at the end of a table', () => {
  310. setData( model, modelTable( [
  311. [ '00', '01' ],
  312. [ '10', '11' ],
  313. [ '20[]', '21' ]
  314. ] ) );
  315. command.execute();
  316. assertEqualMarkup( getData( model ), modelTable( [
  317. [ '00', '01' ],
  318. [ '10', '11' ],
  319. [ '', '' ],
  320. [ '20[]', '21' ]
  321. ] ) );
  322. } );
  323. it( 'should update table heading rows attribute when inserting row in headings section', () => {
  324. setData( model, modelTable( [
  325. [ '00[]', '01' ],
  326. [ '10', '11' ],
  327. [ '20', '21' ]
  328. ], { headingRows: 2 } ) );
  329. command.execute();
  330. assertEqualMarkup( getData( model ), modelTable( [
  331. [ '', '' ],
  332. [ '00[]', '01' ],
  333. [ '10', '11' ],
  334. [ '20', '21' ]
  335. ], { headingRows: 3 } ) );
  336. } );
  337. it( 'should not update table heading rows attribute when inserting row after headings section', () => {
  338. setData( model, modelTable( [
  339. [ '00', '01' ],
  340. [ '10', '11' ],
  341. [ '20[]', '21' ]
  342. ], { headingRows: 2 } ) );
  343. command.execute();
  344. assertEqualMarkup( getData( model ), modelTable( [
  345. [ '00', '01' ],
  346. [ '10', '11' ],
  347. [ '', '' ],
  348. [ '20[]', '21' ]
  349. ], { headingRows: 2 } ) );
  350. } );
  351. it( 'should insert a row when multiple rows are selected', () => {
  352. setData( model, modelTable( [
  353. [ '11', '12' ],
  354. [ '21', '22' ],
  355. [ '31', '32' ]
  356. ] ) );
  357. const tableSelection = editor.plugins.get( TableSelection );
  358. const modelRoot = model.document.getRoot();
  359. tableSelection.setCellSelection(
  360. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  361. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  362. );
  363. command.execute();
  364. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  365. [ '', '' ],
  366. [ '11', '12' ],
  367. [ '21', '22' ],
  368. [ '31', '32' ]
  369. ] ) );
  370. assertSelectedCells( model, [
  371. [ 0, 0 ],
  372. [ 1, 1 ],
  373. [ 1, 1 ],
  374. [ 0, 0 ]
  375. ] );
  376. } );
  377. it( 'should copy the row structure from the selected row', () => {
  378. // +----+----+----+
  379. // | 00 | 01 |
  380. // +----+----+----+
  381. // | 10 | 11 | 12 |
  382. // +----+----+----+
  383. setData( model, modelTable( [
  384. [ '[]00', { contents: '01', colspan: 2 } ],
  385. [ '10', '11', '12' ]
  386. ] ) );
  387. command.execute();
  388. // +----+----+----+
  389. // | | |
  390. // +----+----+----+
  391. // | 00 | 01 |
  392. // +----+----+----+
  393. // | 10 | 11 | 12 |
  394. // +----+----+----+
  395. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  396. [ '', { contents: '', colspan: 2 } ],
  397. [ '00', { contents: '01', colspan: 2 } ],
  398. [ '10', '11', '12' ]
  399. ] ) );
  400. } );
  401. } );
  402. } );
  403. } );