setheadercolumncommand.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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 TableSelection from '../../src/tableselection';
  10. import TableEditing from '../../src/tableediting';
  11. import { assertSelectedCells, modelTable } from '../_utils/utils';
  12. import SetHeaderColumnCommand from '../../src/commands/setheadercolumncommand';
  13. describe( 'SetHeaderColumnCommand', () => {
  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 SetHeaderColumnCommand( 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 columns 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 header columns are selected', () => {
  51. setData( model, modelTable( [
  52. [ '01', '02', '03' ]
  53. ], { headingColumns: 2 } ) );
  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 heading column', () => {
  65. setData( model, modelTable( [
  66. [ '01', '02' ],
  67. [ '11', '12[]' ]
  68. ], { headingColumns: 1 } ) );
  69. expect( command.value ).to.be.false;
  70. } );
  71. it( 'should be true if selection is in a heading column', () => {
  72. setData( model, modelTable( [
  73. [ '01[]', '02' ],
  74. [ '11', '12' ]
  75. ], { headingColumns: 1 } ) );
  76. expect( command.value ).to.be.true;
  77. } );
  78. it( 'should be true if multiple header columns are selected', () => {
  79. setData( model, modelTable( [
  80. [ '01', '02', '03' ]
  81. ], { headingColumns: 2 } ) );
  82. const tableSelection = editor.plugins.get( TableSelection );
  83. const modelRoot = model.document.getRoot();
  84. tableSelection.setCellSelection(
  85. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  86. modelRoot.getNodeByPath( [ 0, 0, 1 ] )
  87. );
  88. expect( command.value ).to.be.true;
  89. } );
  90. it( 'should be true if multiple header columns are selected in reversed order', () => {
  91. setData( model, modelTable( [
  92. [ '01', '02', '03' ]
  93. ], { headingColumns: 2 } ) );
  94. const tableSelection = editor.plugins.get( TableSelection );
  95. const modelRoot = model.document.getRoot();
  96. tableSelection.setCellSelection(
  97. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  98. modelRoot.getNodeByPath( [ 0, 0, 0 ] )
  99. );
  100. expect( command.value ).to.be.true;
  101. } );
  102. it( 'should be false if selection is in a heading row', () => {
  103. setData( model, modelTable( [
  104. [ '01', '02[]' ],
  105. [ '11', '12' ]
  106. ], { headingRows: 1, headingColumns: 1 } ) );
  107. expect( command.value ).to.be.false;
  108. } );
  109. it( 'should be false if only part of selected columns are headers', () => {
  110. setData( model, modelTable( [
  111. [ '01', '02', '03', '04' ]
  112. ], { headingColumns: 2 } ) );
  113. const tableSelection = editor.plugins.get( TableSelection );
  114. const modelRoot = model.document.getRoot();
  115. tableSelection.setCellSelection(
  116. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  117. modelRoot.getNodeByPath( [ 0, 0, 2 ] )
  118. );
  119. expect( command.value ).to.be.false;
  120. } );
  121. } );
  122. describe( 'execute()', () => {
  123. it( 'should set heading columns attribute that cover column with collapsed selection', () => {
  124. setData( model, modelTable( [
  125. [ '00', '01[]', '02', '03' ]
  126. ] ) );
  127. command.execute();
  128. assertEqualMarkup( getData( model ), modelTable( [
  129. [ '00', '01[]', '02', '03' ]
  130. ], { headingColumns: 2 } ) );
  131. } );
  132. it( 'should set heading columns attribute that cover column with entire cell selected', () => {
  133. setData( model, modelTable( [
  134. [ '00', '01', '02', '03' ]
  135. ] ) );
  136. const tableSelection = editor.plugins.get( TableSelection );
  137. const modelRoot = model.document.getRoot();
  138. tableSelection.setCellSelection(
  139. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  140. modelRoot.getNodeByPath( [ 0, 0, 1 ] )
  141. );
  142. command.execute();
  143. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  144. [ '00', '01', '02', '03' ]
  145. ], { headingColumns: 2 } ) );
  146. assertSelectedCells( model, [
  147. [ 0, 1, 0, 0 ]
  148. ] );
  149. } );
  150. it( 'should set heading columns attribute below current selection column', () => {
  151. setData( model, modelTable( [
  152. [ '00', '01[]', '02', '03' ]
  153. ], { headingColumns: 3 } ) );
  154. command.execute();
  155. assertEqualMarkup( getData( model ), modelTable( [
  156. [ '00', '01[]', '02', '03' ]
  157. ], { headingColumns: 1 } ) );
  158. } );
  159. it( 'should remove "headingColumns" attribute from table if no value was given', () => {
  160. setData( model, modelTable( [
  161. [ '[]00', '01', '02', '03' ]
  162. ], { headingColumns: 3 } ) );
  163. command.execute();
  164. assertEqualMarkup( getData( model ), modelTable( [
  165. [ '[]00', '01', '02', '03' ]
  166. ] ) );
  167. } );
  168. describe( 'multi-cell selection', () => {
  169. describe( 'setting header', () => {
  170. it( 'should set it correctly in a middle of single-row, multiple cell selection', () => {
  171. setData( model, modelTable( [
  172. [ '00', '01', '02', '03' ]
  173. ] ) );
  174. const tableSelection = editor.plugins.get( TableSelection );
  175. const modelRoot = model.document.getRoot();
  176. tableSelection.setCellSelection(
  177. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  178. modelRoot.getNodeByPath( [ 0, 0, 2 ] )
  179. );
  180. command.execute();
  181. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  182. [ '00', '01', '02', '03' ]
  183. ], {
  184. headingColumns: 3
  185. } ) );
  186. assertSelectedCells( model, [
  187. [ 0, 1, 1, 0 ]
  188. ] );
  189. } );
  190. it( 'should set it correctly in a middle of multi-row, multiple cell selection', () => {
  191. setData( model, modelTable( [
  192. [ '00', '01', '02', '03' ],
  193. [ '10', '11', '12', '13' ]
  194. ] ) );
  195. const tableSelection = editor.plugins.get( TableSelection );
  196. const modelRoot = model.document.getRoot();
  197. tableSelection.setCellSelection(
  198. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  199. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  200. );
  201. command.execute();
  202. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  203. [ '00', '01', '02', '03' ],
  204. [ '10', '11', '12', '13' ]
  205. ], {
  206. headingColumns: 2
  207. } ) );
  208. assertSelectedCells( model, [
  209. [ 0, 1, 0, 0 ],
  210. [ 0, 1, 0, 0 ]
  211. ] );
  212. } );
  213. it( 'should remove header columns in case of multiple cell selection', () => {
  214. setData( model, modelTable( [
  215. [ '00', '01', '02', '03' ]
  216. ], { headingColumns: 4 } ) );
  217. const tableSelection = editor.plugins.get( TableSelection );
  218. const modelRoot = model.document.getRoot();
  219. tableSelection.setCellSelection(
  220. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  221. modelRoot.getNodeByPath( [ 0, 0, 2 ] )
  222. );
  223. command.execute();
  224. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  225. [ '00', '01', '02', '03' ]
  226. ], {
  227. headingColumns: 1
  228. } ) );
  229. assertSelectedCells( model, [
  230. [ 0, 1, 1, 0 ]
  231. ] );
  232. } );
  233. it( 'should remove header columns in case of multiple cell selection - reversed order', () => {
  234. setData( model, modelTable( [
  235. [ '00', '01', '02', '03' ]
  236. ], {
  237. headingColumns: 4
  238. } ) );
  239. const tableSelection = editor.plugins.get( TableSelection );
  240. const modelRoot = model.document.getRoot();
  241. tableSelection.setCellSelection(
  242. modelRoot.getNodeByPath( [ 0, 0, 2 ] ),
  243. modelRoot.getNodeByPath( [ 0, 0, 1 ] )
  244. );
  245. command.execute();
  246. assertEqualMarkup( getData( model, {
  247. withoutSelection: true
  248. } ), modelTable( [
  249. [ '00', '01', '02', '03' ]
  250. ], {
  251. headingColumns: 1
  252. } ) );
  253. assertSelectedCells( model, [
  254. [ 0, 1, 1, 0 ]
  255. ] );
  256. } );
  257. it( 'should respect forceValue=true in case of multiple cell selection', () => {
  258. setData( model, modelTable( [
  259. [ '00', '01', '02', '03' ]
  260. ], {
  261. headingColumns: 3
  262. } ) );
  263. const tableSelection = editor.plugins.get( TableSelection );
  264. const modelRoot = model.document.getRoot();
  265. tableSelection.setCellSelection(
  266. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  267. modelRoot.getNodeByPath( [ 0, 0, 2 ] )
  268. );
  269. command.execute( { forceValue: true } );
  270. assertEqualMarkup( getData( model, {
  271. withoutSelection: true
  272. } ), modelTable( [
  273. [ '00', '01', '02', '03' ]
  274. ], {
  275. headingColumns: 3
  276. } ) );
  277. assertSelectedCells( model, [
  278. [ 0, 1, 1, 0 ]
  279. ] );
  280. } );
  281. it( 'should respect forceValue=false in case of multiple cell selection', () => {
  282. setData( model, modelTable( [
  283. [ '00', '01', '02', '03' ]
  284. ], {
  285. headingColumns: 1
  286. } ) );
  287. const tableSelection = editor.plugins.get( TableSelection );
  288. const modelRoot = model.document.getRoot();
  289. tableSelection.setCellSelection(
  290. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  291. modelRoot.getNodeByPath( [ 0, 0, 2 ] )
  292. );
  293. command.execute( { forceValue: false } );
  294. assertEqualMarkup( getData( model, {
  295. withoutSelection: true
  296. } ), modelTable( [
  297. [ '00', '01', '02', '03' ]
  298. ], {
  299. headingColumns: 1
  300. } ) );
  301. assertSelectedCells( model, [
  302. [ 0, 1, 1, 0 ]
  303. ] );
  304. } );
  305. it( 'should set it correctly in table with more than 10 columns (array sort bug)', () => {
  306. setData( model, modelTable( [
  307. [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14' ]
  308. ] ) );
  309. const tableSelection = editor.plugins.get( TableSelection );
  310. const modelRoot = model.document.getRoot();
  311. tableSelection.setCellSelection(
  312. modelRoot.getNodeByPath( [ 0, 0, 2 ] ),
  313. modelRoot.getNodeByPath( [ 0, 0, 13 ] )
  314. );
  315. command.execute();
  316. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  317. [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14' ]
  318. ], { headingColumns: 14 } ) );
  319. } );
  320. } );
  321. } );
  322. it( 'should toggle of selected column', () => {
  323. setData( model, modelTable( [
  324. [ '00', '01[]', '02', '03' ]
  325. ], { headingColumns: 2 } ) );
  326. command.execute();
  327. assertEqualMarkup( getData( model ), modelTable( [
  328. [ '00', '01[]', '02', '03' ]
  329. ], { headingColumns: 1 } ) );
  330. command.execute();
  331. assertEqualMarkup( getData( model ), modelTable( [
  332. [ '00', '01[]', '02', '03' ]
  333. ], { headingColumns: 2 } ) );
  334. } );
  335. it( 'should respect forceValue parameter (forceValue=true)', () => {
  336. setData( model, modelTable( [
  337. [ '00', '01[]', '02', '03' ]
  338. ], { headingColumns: 3 } ) );
  339. command.execute( { forceValue: true } );
  340. assertEqualMarkup( getData( model ), modelTable( [
  341. [ '00', '01[]', '02', '03' ]
  342. ], { headingColumns: 3 } ) );
  343. } );
  344. it( 'should respect forceValue parameter (forceValue=false)', () => {
  345. setData( model, modelTable( [
  346. [ '00', '01[]', '02', '03' ]
  347. ], { headingColumns: 1 } ) );
  348. command.execute( { forceValue: false } );
  349. assertEqualMarkup( getData( model ), modelTable( [
  350. [ '00', '01[]', '02', '03' ]
  351. ], { headingColumns: 1 } ) );
  352. } );
  353. it( 'should fix col-spanned cells on the edge of an table heading columns section', () => {
  354. // +----+----+----+
  355. // | 00 | 01 |
  356. // +----+ +
  357. // | 10 | |
  358. // +----+----+----+
  359. // | 20 | 21 | 22 |
  360. // +----+----+----+
  361. // ^-- heading columns
  362. setData( model, modelTable( [
  363. [ '00', { contents: '[]01', colspan: 2, rowspan: 2 } ],
  364. [ '10' ],
  365. [ '20', '21', '22' ]
  366. ], { headingColumns: 1 } ) );
  367. command.execute();
  368. // +----+----+----+
  369. // | 00 | 01 | |
  370. // +----+ + +
  371. // | 10 | | |
  372. // +----+----+----+
  373. // | 20 | 21 | 22 |
  374. // +----+----+----+
  375. // ^-- heading columns
  376. assertEqualMarkup( getData( model ), modelTable( [
  377. [ '00', { contents: '[]01', rowspan: 2 }, { contents: '', rowspan: 2 } ],
  378. [ '10' ],
  379. [ '20', '21', '22' ]
  380. ], { headingColumns: 2 } ) );
  381. } );
  382. it( 'should split to at most 2 table cells when fixing col-spanned cells on the edge of an table heading columns section', () => {
  383. // +----+----+----+----+----+----+
  384. // | 00 | 01 |
  385. // +----+ +
  386. // | 10 | |
  387. // +----+----+----+----+----+----+
  388. // | 20 | 21 | 22 | 23 | 24 | 25 |
  389. // +----+----+----+----+----+----+
  390. // ^-- heading columns
  391. setData( model, modelTable( [
  392. [ '00', { contents: '01', colspan: 5, rowspan: 2 } ],
  393. [ '10' ],
  394. [ '20', '21', '22[]', '23', '24', '25' ]
  395. ], { headingColumns: 1 } ) );
  396. command.execute();
  397. // +----+----+----+----+----+----+
  398. // | 00 | 01 | |
  399. // +----+ + +
  400. // | 10 | | |
  401. // +----+----+----+----+----+----+
  402. // | 20 | 21 | 22 | 23 | 24 | 25 |
  403. // +----+----+----+----+----+----+
  404. // ^-- heading columns
  405. assertEqualMarkup( getData( model ), modelTable( [
  406. [ '00', { contents: '01', colspan: 2, rowspan: 2 }, { contents: '', colspan: 3, rowspan: 2 } ],
  407. [ '10' ],
  408. [ '20', '21', '22[]', '23', '24', '25' ]
  409. ], { headingColumns: 3 } ) );
  410. } );
  411. it( 'should fix col-spanned cells on the edge of an table heading columns section when creating section', () => {
  412. // +----+----+
  413. // | 00 |
  414. // +----+----+
  415. // | 10 | 11 |
  416. // +----+----+
  417. // ^-- heading columns
  418. setData( model, modelTable( [
  419. [ { contents: '00', colspan: 2 } ],
  420. [ '10', '[]11' ]
  421. ], { headingColumns: 2 } ) );
  422. command.execute();
  423. // +----+----+
  424. // | 00 | |
  425. // +----+----+
  426. // | 10 | 11 |
  427. // +----+----+
  428. // ^-- heading columns
  429. assertEqualMarkup( getData( model ), modelTable( [
  430. [ '00', '' ],
  431. [ '10', '[]11' ]
  432. ], { headingColumns: 1 } ) );
  433. } );
  434. } );
  435. } );