setheaderrowcommand.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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 Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  7. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  9. import TableEditing from '../../src/tableediting';
  10. import TableSelection from '../../src/tableselection';
  11. import { assertSelectedCells, modelTable } from '../_utils/utils';
  12. import SetHeaderRowCommand from '../../src/commands/setheaderrowcommand';
  13. describe( 'SetHeaderRowCommand', () => {
  14. let editor, model, command;
  15. beforeEach( () => {
  16. return ModelTestEditor
  17. .create( {
  18. plugins: [ Paragraph, TableEditing, TableSelection ]
  19. } )
  20. .then( newEditor => {
  21. editor = newEditor;
  22. model = editor.model;
  23. command = new SetHeaderRowCommand( editor );
  24. } );
  25. } );
  26. afterEach( () => {
  27. return editor.destroy();
  28. } );
  29. describe( 'isEnabled', () => {
  30. it( 'should be false if selection is not in a table', () => {
  31. setData( model, '<paragraph>foo[]</paragraph>' );
  32. expect( command.isEnabled ).to.be.false;
  33. } );
  34. it( 'should be true if selection is in table', () => {
  35. setData( model, '<table><tableRow><tableCell><paragraph>foo[]</paragraph></tableCell></tableRow></table>' );
  36. expect( command.isEnabled ).to.be.true;
  37. } );
  38. it( 'should be true if multiple cells are selected', () => {
  39. setData( model, modelTable( [
  40. [ '01', '02', '03' ]
  41. ] ) );
  42. const tableSelection = editor.plugins.get( TableSelection );
  43. const modelRoot = model.document.getRoot();
  44. tableSelection.setCellSelection(
  45. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  46. modelRoot.getNodeByPath( [ 0, 0, 1 ] )
  47. );
  48. expect( command.isEnabled ).to.be.true;
  49. } );
  50. it( 'should be true if multiple cells in a header row are selected', () => {
  51. setData( model, modelTable( [
  52. [ '01', '02', '03' ]
  53. ], { headingRows: 1 } ) );
  54. const tableSelection = editor.plugins.get( TableSelection );
  55. const modelRoot = model.document.getRoot();
  56. tableSelection.setCellSelection(
  57. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  58. modelRoot.getNodeByPath( [ 0, 0, 1 ] )
  59. );
  60. expect( command.isEnabled ).to.be.true;
  61. } );
  62. } );
  63. describe( 'value', () => {
  64. it( 'should be false if selection is not in a table without heading row', () => {
  65. setData( model, modelTable( [
  66. [ '01[]', '02' ],
  67. [ '11', '12' ]
  68. ] ) );
  69. expect( command.value ).to.be.false;
  70. } );
  71. it( 'should be false if selection is not in a heading row', () => {
  72. setData( model, modelTable( [
  73. [ '01', '02' ],
  74. [ '11', '12[]' ]
  75. ], { headingRows: 1 } ) );
  76. expect( command.value ).to.be.false;
  77. } );
  78. it( 'should be true if selection is in a heading row', () => {
  79. setData( model, modelTable( [
  80. [ '01[]', '02' ],
  81. [ '11', '12' ]
  82. ], { headingRows: 1 } ) );
  83. expect( command.value ).to.be.true;
  84. } );
  85. it( 'should be false if selection is in a heading column', () => {
  86. setData( model, modelTable( [
  87. [ '01', '02' ],
  88. [ '11[]', '12' ]
  89. ], { headingRows: 1, headingColumns: 1 } ) );
  90. expect( command.value ).to.be.false;
  91. } );
  92. it( 'should be true if multiple header rows are selected', () => {
  93. setData( model, modelTable( [
  94. [ '01', '02' ],
  95. [ '11', '12' ]
  96. ], { headingRows: 2 } ) );
  97. const tableSelection = editor.plugins.get( TableSelection );
  98. const modelRoot = model.document.getRoot();
  99. tableSelection.setCellSelection(
  100. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  101. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  102. );
  103. expect( command.value ).to.be.true;
  104. } );
  105. it( 'should be true if multiple header columns are selected in reversed order', () => {
  106. setData( model, modelTable( [
  107. [ '01', '02' ],
  108. [ '11', '12' ]
  109. ], { headingRows: 2 } ) );
  110. const tableSelection = editor.plugins.get( TableSelection );
  111. const modelRoot = model.document.getRoot();
  112. tableSelection.setCellSelection(
  113. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  114. modelRoot.getNodeByPath( [ 0, 0, 1 ] )
  115. );
  116. expect( command.value ).to.be.true;
  117. } );
  118. it( 'should be false if only part of selected columns are headers', () => {
  119. setData( model, modelTable( [
  120. [ '01', '02' ],
  121. [ '11', '12' ]
  122. ], { headingRows: 1 } ) );
  123. const tableSelection = editor.plugins.get( TableSelection );
  124. const modelRoot = model.document.getRoot();
  125. tableSelection.setCellSelection(
  126. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  127. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  128. );
  129. expect( command.value ).to.be.false;
  130. } );
  131. } );
  132. describe( 'execute()', () => {
  133. it( 'should set heading rows attribute that cover row in which is selection', () => {
  134. setData( model, modelTable( [
  135. [ '00' ],
  136. [ '[]10' ],
  137. [ '20' ],
  138. [ '30' ]
  139. ] ) );
  140. command.execute();
  141. assertEqualMarkup( getData( model ), modelTable( [
  142. [ '00' ],
  143. [ '[]10' ],
  144. [ '20' ],
  145. [ '30' ]
  146. ], { headingRows: 2 } ) );
  147. } );
  148. it( 'should toggle heading rows attribute', () => {
  149. setData( model, modelTable( [
  150. [ '00' ],
  151. [ '[]10' ],
  152. [ '20' ],
  153. [ '30' ]
  154. ], { headingRows: 2 } ) );
  155. command.execute();
  156. assertEqualMarkup( getData( model ), modelTable( [
  157. [ '00' ],
  158. [ '[]10' ],
  159. [ '20' ],
  160. [ '30' ]
  161. ], { headingRows: 1 } ) );
  162. command.execute();
  163. setData( model, modelTable( [
  164. [ '00' ],
  165. [ '[]10' ],
  166. [ '20' ],
  167. [ '30' ]
  168. ], { headingRows: 2 } ) );
  169. } );
  170. it( 'should set heading rows attribute if currently selected row is a heading so the heading section is below this row', () => {
  171. setData( model, modelTable( [
  172. [ '00' ],
  173. [ '[]10' ],
  174. [ '20' ],
  175. [ '30' ]
  176. ], { headingRows: 3 } ) );
  177. command.execute();
  178. assertEqualMarkup( getData( model ), modelTable( [
  179. [ '00' ],
  180. [ '[]10' ],
  181. [ '20' ],
  182. [ '30' ]
  183. ], { headingRows: 1 } ) );
  184. } );
  185. it( 'should remove "headingRows" attribute from table if no value was given', () => {
  186. setData( model, modelTable( [
  187. [ '[]00' ],
  188. [ '10' ],
  189. [ '20' ],
  190. [ '30' ]
  191. ], { headingRows: 3 } ) );
  192. command.execute();
  193. assertEqualMarkup( getData( model ), modelTable( [
  194. [ '[]00' ],
  195. [ '10' ],
  196. [ '20' ],
  197. [ '30' ]
  198. ] ) );
  199. } );
  200. describe( 'multi-cell selection', () => {
  201. it( 'should set it correctly in a middle of multi-row table', () => {
  202. setData( model, modelTable( [
  203. [ '00' ],
  204. [ '10' ],
  205. [ '20' ],
  206. [ '30' ]
  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, 2, 0 ] )
  213. );
  214. command.execute();
  215. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  216. [ '00' ],
  217. [ '10' ],
  218. [ '20' ],
  219. [ '30' ]
  220. ], {
  221. headingRows: 3
  222. } ) );
  223. assertSelectedCells( model, [
  224. [ 0 ],
  225. [ 1 ],
  226. [ 1 ],
  227. [ 0 ]
  228. ] );
  229. } );
  230. it( 'should set it correctly in a middle of multi-row table - reversed selection', () => {
  231. setData( model, modelTable( [
  232. [ '00' ],
  233. [ '10' ],
  234. [ '20' ],
  235. [ '30' ]
  236. ] ) );
  237. const tableSelection = editor.plugins.get( TableSelection );
  238. const modelRoot = model.document.getRoot();
  239. tableSelection.setCellSelection(
  240. modelRoot.getNodeByPath( [ 0, 2, 0 ] ),
  241. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  242. );
  243. command.execute();
  244. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  245. [ '00' ],
  246. [ '10' ],
  247. [ '20' ],
  248. [ '30' ]
  249. ], {
  250. headingRows: 3
  251. } ) );
  252. assertSelectedCells( model, [
  253. [ 0 ],
  254. [ 1 ],
  255. [ 1 ],
  256. [ 0 ]
  257. ] );
  258. } );
  259. it( 'should set it correctly in a middle of multi-row, multiple cell selection', () => {
  260. setData( model, modelTable( [
  261. [ '00', '01', '02', '03' ],
  262. [ '10', '11', '12', '13' ],
  263. [ '20', '21', '22', '23' ],
  264. [ '30', '31', '32', '33' ]
  265. ] ) );
  266. const tableSelection = editor.plugins.get( TableSelection );
  267. const modelRoot = model.document.getRoot();
  268. tableSelection.setCellSelection(
  269. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  270. modelRoot.getNodeByPath( [ 0, 2, 2 ] )
  271. );
  272. command.execute();
  273. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  274. [ '00', '01', '02', '03' ],
  275. [ '10', '11', '12', '13' ],
  276. [ '20', '21', '22', '23' ],
  277. [ '30', '31', '32', '33' ]
  278. ], {
  279. headingRows: 3
  280. } ) );
  281. assertSelectedCells( model, [
  282. [ 0, 0, 0, 0 ],
  283. [ 0, 1, 1, 0 ],
  284. [ 0, 1, 1, 0 ],
  285. [ 0, 0, 0, 0 ]
  286. ] );
  287. } );
  288. it( 'should set it correctly in table with more than 10 columns (array sort bug)', () => {
  289. setData( model, modelTable( [
  290. [ '0', 'x' ],
  291. [ '1', 'x' ],
  292. [ '2', 'x' ],
  293. [ '3', 'x' ],
  294. [ '4', 'x' ],
  295. [ '5', 'x' ],
  296. [ '6', 'x' ],
  297. [ '7', 'x' ],
  298. [ '8', 'x' ],
  299. [ '9', 'x' ],
  300. [ '10', 'x' ],
  301. [ '11', 'x' ],
  302. [ '12', 'x' ],
  303. [ '13', 'x' ],
  304. [ '14', 'x' ]
  305. ] ) );
  306. const tableSelection = editor.plugins.get( TableSelection );
  307. const modelRoot = model.document.getRoot();
  308. tableSelection.setCellSelection(
  309. modelRoot.getNodeByPath( [ 0, 2, 0 ] ),
  310. modelRoot.getNodeByPath( [ 0, 13, 0 ] )
  311. );
  312. command.execute();
  313. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  314. [ '0', 'x' ],
  315. [ '1', 'x' ],
  316. [ '2', 'x' ],
  317. [ '3', 'x' ],
  318. [ '4', 'x' ],
  319. [ '5', 'x' ],
  320. [ '6', 'x' ],
  321. [ '7', 'x' ],
  322. [ '8', 'x' ],
  323. [ '9', 'x' ],
  324. [ '10', 'x' ],
  325. [ '11', 'x' ],
  326. [ '12', 'x' ],
  327. [ '13', 'x' ],
  328. [ '14', 'x' ]
  329. ], { headingRows: 14 } ) );
  330. } );
  331. it( 'should set it correctly in table with more than 10 columns (array sort bug, reversed selection)', () => {
  332. setData( model, modelTable( [
  333. [ '0', 'x' ],
  334. [ '1', 'x' ],
  335. [ '2', 'x' ],
  336. [ '3', 'x' ],
  337. [ '4', 'x' ],
  338. [ '5', 'x' ],
  339. [ '6', 'x' ],
  340. [ '7', 'x' ],
  341. [ '8', 'x' ],
  342. [ '9', 'x' ],
  343. [ '10', 'x' ],
  344. [ '11', 'x' ],
  345. [ '12', 'x' ],
  346. [ '13', 'x' ],
  347. [ '14', 'x' ]
  348. ] ) );
  349. const tableSelection = editor.plugins.get( TableSelection );
  350. const modelRoot = model.document.getRoot();
  351. tableSelection.setCellSelection(
  352. modelRoot.getNodeByPath( [ 0, 13, 0 ] ),
  353. modelRoot.getNodeByPath( [ 0, 2, 1 ] )
  354. );
  355. command.execute();
  356. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  357. [ '0', 'x' ],
  358. [ '1', 'x' ],
  359. [ '2', 'x' ],
  360. [ '3', 'x' ],
  361. [ '4', 'x' ],
  362. [ '5', 'x' ],
  363. [ '6', 'x' ],
  364. [ '7', 'x' ],
  365. [ '8', 'x' ],
  366. [ '9', 'x' ],
  367. [ '10', 'x' ],
  368. [ '11', 'x' ],
  369. [ '12', 'x' ],
  370. [ '13', 'x' ],
  371. [ '14', 'x' ]
  372. ], { headingRows: 14 } ) );
  373. } );
  374. it( 'should remove header rows in case of multiple cell selection', () => {
  375. setData( model, modelTable( [
  376. [ '00' ],
  377. [ '10' ],
  378. [ '20' ],
  379. [ '30' ]
  380. ], { headingRows: 4 } ) );
  381. const tableSelection = editor.plugins.get( TableSelection );
  382. const modelRoot = model.document.getRoot();
  383. tableSelection.setCellSelection(
  384. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  385. modelRoot.getNodeByPath( [ 0, 2, 0 ] )
  386. );
  387. command.execute();
  388. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  389. [ '00' ],
  390. [ '10' ],
  391. [ '20' ],
  392. [ '30' ]
  393. ], {
  394. headingRows: 1
  395. } ) );
  396. assertSelectedCells( model, [
  397. [ 0 ],
  398. [ 1 ],
  399. [ 1 ],
  400. [ 0 ]
  401. ] );
  402. } );
  403. it( 'should respect forceValue=true in case of multiple row selection', () => {
  404. setData( model, modelTable( [
  405. [ '00' ],
  406. [ '10' ],
  407. [ '20' ],
  408. [ '30' ]
  409. ], {
  410. headingRows: 3
  411. } ) );
  412. const tableSelection = editor.plugins.get( TableSelection );
  413. const modelRoot = model.document.getRoot();
  414. tableSelection.setCellSelection(
  415. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  416. modelRoot.getNodeByPath( [ 0, 2, 0 ] )
  417. );
  418. command.execute( { forceValue: true } );
  419. assertEqualMarkup( getData( model, {
  420. withoutSelection: true
  421. } ), modelTable( [
  422. [ '00' ],
  423. [ '10' ],
  424. [ '20' ],
  425. [ '30' ]
  426. ], {
  427. headingRows: 3
  428. } ) );
  429. assertSelectedCells( model, [
  430. [ 0 ],
  431. [ 1 ],
  432. [ 1 ],
  433. [ 0 ]
  434. ] );
  435. } );
  436. it( 'should respect forceValue=false in case of multiple cell selection', () => {
  437. setData( model, modelTable( [
  438. [ '00' ],
  439. [ '10' ],
  440. [ '20' ],
  441. [ '30' ]
  442. ], {
  443. headingRows: 1
  444. } ) );
  445. const tableSelection = editor.plugins.get( TableSelection );
  446. const modelRoot = model.document.getRoot();
  447. tableSelection.setCellSelection(
  448. modelRoot.getNodeByPath( [ 0, 1, 0 ] ),
  449. modelRoot.getNodeByPath( [ 0, 2, 0 ] )
  450. );
  451. command.execute( { forceValue: false } );
  452. assertEqualMarkup( getData( model, {
  453. withoutSelection: true
  454. } ), modelTable( [
  455. [ '00' ],
  456. [ '10' ],
  457. [ '20' ],
  458. [ '30' ]
  459. ], {
  460. headingRows: 1
  461. } ) );
  462. assertSelectedCells( model, [
  463. [ 0 ],
  464. [ 1 ],
  465. [ 1 ],
  466. [ 0 ]
  467. ] );
  468. } );
  469. } );
  470. it( 'should respect forceValue parameter (forceValue=true)', () => {
  471. setData( model, modelTable( [
  472. [ '00' ],
  473. [ '[]10' ],
  474. [ '20' ],
  475. [ '30' ]
  476. ], { headingRows: 3 } ) );
  477. command.execute( { forceValue: true } );
  478. assertEqualMarkup( getData( model ), modelTable( [
  479. [ '00' ],
  480. [ '[]10' ],
  481. [ '20' ],
  482. [ '30' ]
  483. ], { headingRows: 3 } ) );
  484. } );
  485. it( 'should respect forceValue parameter (forceValue=false)', () => {
  486. setData( model, modelTable( [
  487. [ '00' ],
  488. [ '[]10' ],
  489. [ '20' ],
  490. [ '30' ]
  491. ], { headingRows: 1 } ) );
  492. command.execute( { forceValue: false } );
  493. assertEqualMarkup( getData( model ), modelTable( [
  494. [ '00' ],
  495. [ '[]10' ],
  496. [ '20' ],
  497. [ '30' ]
  498. ], { headingRows: 1 } ) );
  499. } );
  500. it( 'should fix rowspaned cells on the edge of an table head section', () => {
  501. setData( model, modelTable( [
  502. [ '00', '01', '02' ],
  503. [ { colspan: 2, rowspan: 2, contents: '10[]' }, '12' ],
  504. [ '22' ]
  505. ], { headingColumns: 2, headingRows: 1 } ) );
  506. command.execute();
  507. assertEqualMarkup( getData( model ), modelTable( [
  508. [ '00', '01', '02' ],
  509. [ { colspan: 2, contents: '10[]' }, '12' ],
  510. [ { colspan: 2, contents: '' }, '22' ]
  511. ], { headingColumns: 2, headingRows: 2 } ) );
  512. } );
  513. it( 'should split to at most 2 table cells when fixing rowspaned cells on the edge of an table head section', () => {
  514. setData( model, modelTable( [
  515. [ '00', '01', '02' ],
  516. [ { colspan: 2, rowspan: 5, contents: '10' }, '12' ],
  517. [ '22[]' ],
  518. [ '32' ],
  519. [ '42' ],
  520. [ '52' ]
  521. ], { headingColumns: 2, headingRows: 1 } ) );
  522. command.execute();
  523. assertEqualMarkup( getData( model ), modelTable( [
  524. [ '00', '01', '02' ],
  525. [ { colspan: 2, rowspan: 2, contents: '10' }, '12' ],
  526. [ '22[]' ],
  527. [ { colspan: 2, rowspan: 3, contents: '' }, '32' ],
  528. [ '42' ],
  529. [ '52' ]
  530. ], { headingColumns: 2, headingRows: 3 } ) );
  531. } );
  532. it( 'should fix rowspaned cells on the edge of an table head section when creating section', () => {
  533. setData( model, modelTable( [
  534. [ { rowspan: 2, contents: '00' }, '01' ],
  535. [ '[]11' ]
  536. ], { headingRows: 2 } ) );
  537. command.execute();
  538. assertEqualMarkup( getData( model ), modelTable( [
  539. [ '00', '01' ],
  540. [ '', '[]11' ]
  541. ], { headingRows: 1 } ) );
  542. } );
  543. it( 'should fix rowspaned cells inside a row', () => {
  544. setData( model, modelTable( [
  545. [ '00', { rowspan: 2, contents: '01' } ],
  546. [ '[]10' ]
  547. ], { headingRows: 2 } ) );
  548. command.execute();
  549. assertEqualMarkup( getData( model ), modelTable( [
  550. [ '00', '01' ],
  551. [ '[]10', '' ]
  552. ], { headingRows: 1 } ) );
  553. } );
  554. it( 'should work properly in the first row of a table', () => {
  555. setData( model, modelTable( [
  556. [ '00', '[]01', '02' ],
  557. [ { colspan: 2, rowspan: 2, contents: '10' }, '12' ],
  558. [ '22' ]
  559. ] ) );
  560. command.execute();
  561. assertEqualMarkup( getData( model ), modelTable( [
  562. [ '00', '[]01', '02' ],
  563. [ { colspan: 2, rowspan: 2, contents: '10' }, '12' ],
  564. [ '22' ]
  565. ], { headingRows: 1 } ) );
  566. } );
  567. } );
  568. } );