setheadercolumncommand.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 SetHeaderColumnCommand from '../../src/commands/setheadercolumncommand';
  8. import { assertSelectedCells, defaultConversion, defaultSchema, modelTable } from '../_utils/utils';
  9. import TableSelection from '../../src/tableselection';
  10. import TableUtils from '../../src/tableutils';
  11. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  12. describe( 'SetHeaderColumnCommand', () => {
  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. command = new SetHeaderColumnCommand( editor );
  23. defaultSchema( model.schema );
  24. defaultConversion( editor.conversion );
  25. } );
  26. } );
  27. afterEach( () => {
  28. return editor.destroy();
  29. } );
  30. describe( 'isEnabled', () => {
  31. it( 'should be false if selection is not in a table', () => {
  32. setData( model, '<paragraph>foo[]</paragraph>' );
  33. expect( command.isEnabled ).to.be.false;
  34. } );
  35. it( 'should be true if selection is in table', () => {
  36. setData( model, '<table><tableRow><tableCell><paragraph>foo[]</paragraph></tableCell></tableRow></table>' );
  37. expect( command.isEnabled ).to.be.true;
  38. } );
  39. it( 'should be true if multiple columns are selected', () => {
  40. setData( model, modelTable( [
  41. [ '01', '02', '03' ]
  42. ] ) );
  43. const tableSelection = editor.plugins.get( TableSelection );
  44. const modelRoot = model.document.getRoot();
  45. tableSelection.setCellSelection(
  46. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  47. modelRoot.getNodeByPath( [ 0, 0, 1 ] )
  48. );
  49. expect( command.isEnabled ).to.be.true;
  50. } );
  51. it( 'should be true if multiple header columns are selected', () => {
  52. setData( model, modelTable( [
  53. [ '01', '02', '03' ]
  54. ], { headingColumns: 2 } ) );
  55. const tableSelection = editor.plugins.get( TableSelection );
  56. const modelRoot = model.document.getRoot();
  57. tableSelection.setCellSelection(
  58. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  59. modelRoot.getNodeByPath( [ 0, 0, 1 ] )
  60. );
  61. expect( command.isEnabled ).to.be.true;
  62. } );
  63. } );
  64. describe( 'value', () => {
  65. it( 'should be false if selection is not in a heading column', () => {
  66. setData( model, modelTable( [
  67. [ '01', '02' ],
  68. [ '11', '12[]' ]
  69. ], { headingColumns: 1 } ) );
  70. expect( command.value ).to.be.false;
  71. } );
  72. it( 'should be true if selection is in a heading column', () => {
  73. setData( model, modelTable( [
  74. [ '01[]', '02' ],
  75. [ '11', '12' ]
  76. ], { headingColumns: 1 } ) );
  77. expect( command.value ).to.be.true;
  78. } );
  79. it( 'should be true if multiple header columns are selected', () => {
  80. setData( model, modelTable( [
  81. [ '01', '02', '03' ]
  82. ], { headingColumns: 2 } ) );
  83. const tableSelection = editor.plugins.get( TableSelection );
  84. const modelRoot = model.document.getRoot();
  85. tableSelection.setCellSelection(
  86. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  87. modelRoot.getNodeByPath( [ 0, 0, 1 ] )
  88. );
  89. expect( command.value ).to.be.true;
  90. } );
  91. it( 'should be true if multiple header columns are selected in reversed order', () => {
  92. setData( model, modelTable( [
  93. [ '01', '02', '03' ]
  94. ], { headingColumns: 2 } ) );
  95. const tableSelection = editor.plugins.get( TableSelection );
  96. const modelRoot = model.document.getRoot();
  97. tableSelection.setCellSelection(
  98. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  99. modelRoot.getNodeByPath( [ 0, 0, 0 ] )
  100. );
  101. expect( command.value ).to.be.true;
  102. } );
  103. it( 'should be false if selection is in a heading row', () => {
  104. setData( model, modelTable( [
  105. [ '01', '02[]' ],
  106. [ '11', '12' ]
  107. ], { headingRows: 1, headingColumns: 1 } ) );
  108. expect( command.value ).to.be.false;
  109. } );
  110. it( 'should be false if only part of selected columns are headers', () => {
  111. setData( model, modelTable( [
  112. [ '01', '02', '03', '04' ]
  113. ], { headingColumns: 2 } ) );
  114. const tableSelection = editor.plugins.get( TableSelection );
  115. const modelRoot = model.document.getRoot();
  116. tableSelection.setCellSelection(
  117. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  118. modelRoot.getNodeByPath( [ 0, 0, 2 ] )
  119. );
  120. expect( command.value ).to.be.false;
  121. } );
  122. } );
  123. describe( 'execute()', () => {
  124. it( 'should set heading columns attribute that cover column with collapsed selection', () => {
  125. setData( model, modelTable( [
  126. [ '00', '01[]', '02', '03' ]
  127. ] ) );
  128. command.execute();
  129. assertEqualMarkup( getData( model ), modelTable( [
  130. [ '00', '01[]', '02', '03' ]
  131. ], { headingColumns: 2 } ) );
  132. } );
  133. it( 'should set heading columns attribute that cover column with entire cell selected', () => {
  134. setData( model, modelTable( [
  135. [ '00', '01', '02', '03' ]
  136. ] ) );
  137. const tableSelection = editor.plugins.get( TableSelection );
  138. const modelRoot = model.document.getRoot();
  139. tableSelection.setCellSelection(
  140. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  141. modelRoot.getNodeByPath( [ 0, 0, 1 ] )
  142. );
  143. command.execute();
  144. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  145. [ '00', '01', '02', '03' ]
  146. ], { headingColumns: 2 } ) );
  147. assertSelectedCells( model, [
  148. [ 0, 1, 0, 0 ]
  149. ] );
  150. } );
  151. it( 'should set heading columns attribute below current selection column', () => {
  152. setData( model, modelTable( [
  153. [ '00', '01[]', '02', '03' ]
  154. ], { headingColumns: 3 } ) );
  155. command.execute();
  156. assertEqualMarkup( getData( model ), modelTable( [
  157. [ '00', '01[]', '02', '03' ]
  158. ], { headingColumns: 1 } ) );
  159. } );
  160. describe( 'multi-cell selection', () => {
  161. describe( 'setting header', () => {
  162. it( 'should set it correctly in a middle of single-row, multiple cell selection', () => {
  163. setData( model, modelTable( [
  164. [ '00', '01', '02', '03' ]
  165. ] ) );
  166. const tableSelection = editor.plugins.get( TableSelection );
  167. const modelRoot = model.document.getRoot();
  168. tableSelection.setCellSelection(
  169. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  170. modelRoot.getNodeByPath( [ 0, 0, 2 ] )
  171. );
  172. command.execute();
  173. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  174. [ '00', '01', '02', '03' ]
  175. ], {
  176. headingColumns: 3
  177. } ) );
  178. assertSelectedCells( model, [
  179. [ 0, 1, 1, 0 ]
  180. ] );
  181. } );
  182. it( 'should set it correctly in a middle of multi-row, multiple cell selection', () => {
  183. setData( model, modelTable( [
  184. [ '00', '01', '02', '03' ],
  185. [ '10', '11', '12', '13' ]
  186. ] ) );
  187. const tableSelection = editor.plugins.get( TableSelection );
  188. const modelRoot = model.document.getRoot();
  189. tableSelection.setCellSelection(
  190. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  191. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  192. );
  193. command.execute();
  194. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  195. [ '00', '01', '02', '03' ],
  196. [ '10', '11', '12', '13' ]
  197. ], {
  198. headingColumns: 2
  199. } ) );
  200. assertSelectedCells( model, [
  201. [ 0, 1, 0, 0 ],
  202. [ 0, 1, 0, 0 ]
  203. ] );
  204. } );
  205. it( 'should remove header columns in case of multiple cell selection', () => {
  206. setData( model, modelTable( [
  207. [ '00', '01', '02', '03' ]
  208. ], { headingColumns: 4 } ) );
  209. const tableSelection = editor.plugins.get( TableSelection );
  210. const modelRoot = model.document.getRoot();
  211. tableSelection.setCellSelection(
  212. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  213. modelRoot.getNodeByPath( [ 0, 0, 2 ] )
  214. );
  215. command.execute();
  216. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  217. [ '00', '01', '02', '03' ]
  218. ], {
  219. headingColumns: 1
  220. } ) );
  221. assertSelectedCells( model, [
  222. [ 0, 1, 1, 0 ]
  223. ] );
  224. } );
  225. it( 'should remove header columns in case of multiple cell selection - reversed order', () => {
  226. setData( model, modelTable( [
  227. [ '00', '01', '02', '03' ]
  228. ], {
  229. headingColumns: 4
  230. } ) );
  231. const tableSelection = editor.plugins.get( TableSelection );
  232. const modelRoot = model.document.getRoot();
  233. tableSelection.setCellSelection(
  234. modelRoot.getNodeByPath( [ 0, 0, 2 ] ),
  235. modelRoot.getNodeByPath( [ 0, 0, 1 ] )
  236. );
  237. command.execute();
  238. assertEqualMarkup( getData( model, {
  239. withoutSelection: true
  240. } ), modelTable( [
  241. [ '00', '01', '02', '03' ]
  242. ], {
  243. headingColumns: 1
  244. } ) );
  245. assertSelectedCells( model, [
  246. [ 0, 1, 1, 0 ]
  247. ] );
  248. } );
  249. it( 'should respect forceValue=true in case of multiple cell selection', () => {
  250. setData( model, modelTable( [
  251. [ '00', '01', '02', '03' ]
  252. ], {
  253. headingColumns: 3
  254. } ) );
  255. const tableSelection = editor.plugins.get( TableSelection );
  256. const modelRoot = model.document.getRoot();
  257. tableSelection.setCellSelection(
  258. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  259. modelRoot.getNodeByPath( [ 0, 0, 2 ] )
  260. );
  261. command.execute( { forceValue: true } );
  262. assertEqualMarkup( getData( model, {
  263. withoutSelection: true
  264. } ), modelTable( [
  265. [ '00', '01', '02', '03' ]
  266. ], {
  267. headingColumns: 3
  268. } ) );
  269. assertSelectedCells( model, [
  270. [ 0, 1, 1, 0 ]
  271. ] );
  272. } );
  273. it( 'should respect forceValue=false in case of multiple cell selection', () => {
  274. setData( model, modelTable( [
  275. [ '00', '01', '02', '03' ]
  276. ], {
  277. headingColumns: 1
  278. } ) );
  279. const tableSelection = editor.plugins.get( TableSelection );
  280. const modelRoot = model.document.getRoot();
  281. tableSelection.setCellSelection(
  282. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  283. modelRoot.getNodeByPath( [ 0, 0, 2 ] )
  284. );
  285. command.execute( { forceValue: false } );
  286. assertEqualMarkup( getData( model, {
  287. withoutSelection: true
  288. } ), modelTable( [
  289. [ '00', '01', '02', '03' ]
  290. ], {
  291. headingColumns: 1
  292. } ) );
  293. assertSelectedCells( model, [
  294. [ 0, 1, 1, 0 ]
  295. ] );
  296. } );
  297. it( 'should set it correctly in table with more than 10 columns (array sort bug)', () => {
  298. setData( model, modelTable( [
  299. [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14' ]
  300. ] ) );
  301. const tableSelection = editor.plugins.get( TableSelection );
  302. const modelRoot = model.document.getRoot();
  303. tableSelection.setCellSelection(
  304. modelRoot.getNodeByPath( [ 0, 0, 2 ] ),
  305. modelRoot.getNodeByPath( [ 0, 0, 13 ] )
  306. );
  307. command.execute();
  308. assertEqualMarkup( getData( model, { withoutSelection: true } ), modelTable( [
  309. [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14' ]
  310. ], { headingColumns: 14 } ) );
  311. } );
  312. } );
  313. } );
  314. it( 'should toggle of selected column', () => {
  315. setData( model, modelTable( [
  316. [ '00', '01[]', '02', '03' ]
  317. ], { headingColumns: 2 } ) );
  318. command.execute();
  319. assertEqualMarkup( getData( model ), modelTable( [
  320. [ '00', '01[]', '02', '03' ]
  321. ], { headingColumns: 1 } ) );
  322. command.execute();
  323. assertEqualMarkup( getData( model ), modelTable( [
  324. [ '00', '01[]', '02', '03' ]
  325. ], { headingColumns: 2 } ) );
  326. } );
  327. it( 'should respect forceValue parameter #1', () => {
  328. setData( model, modelTable( [
  329. [ '00', '01[]', '02', '03' ]
  330. ], { headingColumns: 3 } ) );
  331. command.execute( { forceValue: true } );
  332. assertEqualMarkup( getData( model ), modelTable( [
  333. [ '00', '01[]', '02', '03' ]
  334. ], { headingColumns: 3 } ) );
  335. } );
  336. it( 'should respect forceValue parameter #2', () => {
  337. setData( model, modelTable( [
  338. [ '00', '01[]', '02', '03' ]
  339. ], { headingColumns: 1 } ) );
  340. command.execute( { forceValue: false } );
  341. assertEqualMarkup( getData( model ), modelTable( [
  342. [ '00', '01[]', '02', '03' ]
  343. ], { headingColumns: 1 } ) );
  344. } );
  345. } );
  346. } );