setheaderrowcommand.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 SetHeaderRowCommand from '../../src/commands/setheaderrowcommand';
  8. import { 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( 'SetHeaderRowCommand', () => {
  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 SetHeaderRowCommand( 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 cells 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 cells in a header row are selected', () => {
  52. setData( model, modelTable( [
  53. [ '01', '02', '03' ]
  54. ], { headingRows: 1 } ) );
  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 table without heading row', () => {
  66. setData( model, modelTable( [
  67. [ '01[]', '02' ],
  68. [ '11', '12' ]
  69. ] ) );
  70. expect( command.value ).to.be.false;
  71. } );
  72. it( 'should be false if selection is not in a heading row', () => {
  73. setData( model, modelTable( [
  74. [ '01', '02' ],
  75. [ '11', '12[]' ]
  76. ], { headingRows: 1 } ) );
  77. expect( command.value ).to.be.false;
  78. } );
  79. it( 'should be true if selection is in a heading row', () => {
  80. setData( model, modelTable( [
  81. [ '01[]', '02' ],
  82. [ '11', '12' ]
  83. ], { headingRows: 1 } ) );
  84. expect( command.value ).to.be.true;
  85. } );
  86. it( 'should be false if selection is in a heading column', () => {
  87. setData( model, modelTable( [
  88. [ '01', '02' ],
  89. [ '11[]', '12' ]
  90. ], { headingRows: 1, headingColumns: 1 } ) );
  91. expect( command.value ).to.be.false;
  92. } );
  93. it( 'should be true if multiple header rows are selected', () => {
  94. setData( model, modelTable( [
  95. [ '01', '02' ],
  96. [ '11', '12' ]
  97. ], { headingRows: 2 } ) );
  98. const tableSelection = editor.plugins.get( TableSelection );
  99. const modelRoot = model.document.getRoot();
  100. tableSelection._setCellSelection(
  101. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  102. modelRoot.getNodeByPath( [ 0, 1, 1 ] )
  103. );
  104. expect( command.value ).to.be.true;
  105. } );
  106. it( 'should be true if multiple header columns are selected in reversed order', () => {
  107. setData( model, modelTable( [
  108. [ '01', '02' ],
  109. [ '11', '12' ]
  110. ], { headingRows: 2 } ) );
  111. const tableSelection = editor.plugins.get( TableSelection );
  112. const modelRoot = model.document.getRoot();
  113. tableSelection._setCellSelection(
  114. modelRoot.getNodeByPath( [ 0, 1, 1 ] ),
  115. modelRoot.getNodeByPath( [ 0, 0, 1 ] )
  116. );
  117. expect( command.value ).to.be.true;
  118. } );
  119. it( 'should be false if only part of selected columns are headers', () => {
  120. setData( model, modelTable( [
  121. [ '01', '02' ],
  122. [ '11', '12' ]
  123. ], { headingRows: 1 } ) );
  124. const tableSelection = editor.plugins.get( TableSelection );
  125. const modelRoot = model.document.getRoot();
  126. tableSelection._setCellSelection(
  127. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  128. modelRoot.getNodeByPath( [ 0, 1, 0 ] )
  129. );
  130. expect( command.value ).to.be.false;
  131. } );
  132. } );
  133. describe( 'execute()', () => {
  134. it( 'should set heading rows attribute that cover row in which is selection', () => {
  135. setData( model, modelTable( [
  136. [ '00' ],
  137. [ '[]10' ],
  138. [ '20' ],
  139. [ '30' ]
  140. ] ) );
  141. command.execute();
  142. assertEqualMarkup( getData( model ), modelTable( [
  143. [ '00' ],
  144. [ '[]10' ],
  145. [ '20' ],
  146. [ '30' ]
  147. ], { headingRows: 2 } ) );
  148. } );
  149. it( 'should toggle heading rows attribute', () => {
  150. setData( model, modelTable( [
  151. [ '00' ],
  152. [ '[]10' ],
  153. [ '20' ],
  154. [ '30' ]
  155. ], { headingRows: 2 } ) );
  156. command.execute();
  157. assertEqualMarkup( getData( model ), modelTable( [
  158. [ '00' ],
  159. [ '[]10' ],
  160. [ '20' ],
  161. [ '30' ]
  162. ], { headingRows: 1 } ) );
  163. command.execute();
  164. setData( model, modelTable( [
  165. [ '00' ],
  166. [ '[]10' ],
  167. [ '20' ],
  168. [ '30' ]
  169. ], { headingRows: 2 } ) );
  170. } );
  171. it( 'should set heading rows attribute if currently selected row is a heading so the heading section is below this row', () => {
  172. setData( model, modelTable( [
  173. [ '00' ],
  174. [ '[]10' ],
  175. [ '20' ],
  176. [ '30' ]
  177. ], { headingRows: 3 } ) );
  178. command.execute();
  179. assertEqualMarkup( getData( model ), modelTable( [
  180. [ '00' ],
  181. [ '[]10' ],
  182. [ '20' ],
  183. [ '30' ]
  184. ], { headingRows: 1 } ) );
  185. } );
  186. it( 'should unsetset heading rows attribute', () => {
  187. setData( model, modelTable( [
  188. [ '[]00' ],
  189. [ '10' ],
  190. [ '20' ],
  191. [ '30' ]
  192. ], { headingRows: 3 } ) );
  193. command.execute();
  194. assertEqualMarkup( getData( model ), modelTable( [
  195. [ '[]00' ],
  196. [ '10' ],
  197. [ '20' ],
  198. [ '30' ]
  199. ] ) );
  200. } );
  201. it( 'should respect forceValue parameter #1', () => {
  202. setData( model, modelTable( [
  203. [ '00' ],
  204. [ '[]10' ],
  205. [ '20' ],
  206. [ '30' ]
  207. ], { headingRows: 3 } ) );
  208. command.execute( { forceValue: true } );
  209. assertEqualMarkup( getData( model ), modelTable( [
  210. [ '00' ],
  211. [ '[]10' ],
  212. [ '20' ],
  213. [ '30' ]
  214. ], { headingRows: 3 } ) );
  215. } );
  216. it( 'should respect forceValue parameter #2', () => {
  217. setData( model, modelTable( [
  218. [ '00' ],
  219. [ '[]10' ],
  220. [ '20' ],
  221. [ '30' ]
  222. ], { headingRows: 1 } ) );
  223. command.execute( { forceValue: false } );
  224. assertEqualMarkup( getData( model ), modelTable( [
  225. [ '00' ],
  226. [ '[]10' ],
  227. [ '20' ],
  228. [ '30' ]
  229. ], { headingRows: 1 } ) );
  230. } );
  231. it( 'should fix rowspaned cells on the edge of an table head section', () => {
  232. setData( model, modelTable( [
  233. [ '00', '01', '02' ],
  234. [ { colspan: 2, rowspan: 2, contents: '10[]' }, '12' ],
  235. [ '22' ]
  236. ], { headingColumns: 2, headingRows: 1 } ) );
  237. command.execute();
  238. assertEqualMarkup( getData( model ), modelTable( [
  239. [ '00', '01', '02' ],
  240. [ { colspan: 2, contents: '10[]' }, '12' ],
  241. [ { colspan: 2, contents: '' }, '22' ]
  242. ], { headingColumns: 2, headingRows: 2 } ) );
  243. } );
  244. it( 'should split to at most 2 table cells when fixing rowspaned cells on the edge of an table head section', () => {
  245. setData( model, modelTable( [
  246. [ '00', '01', '02' ],
  247. [ { colspan: 2, rowspan: 5, contents: '10' }, '12' ],
  248. [ '22[]' ],
  249. [ '32' ],
  250. [ '42' ],
  251. [ '52' ]
  252. ], { headingColumns: 2, headingRows: 1 } ) );
  253. command.execute();
  254. assertEqualMarkup( getData( model ), modelTable( [
  255. [ '00', '01', '02' ],
  256. [ { colspan: 2, rowspan: 2, contents: '10' }, '12' ],
  257. [ '22[]' ],
  258. [ { colspan: 2, rowspan: 3, contents: '' }, '32' ],
  259. [ '42' ],
  260. [ '52' ]
  261. ], { headingColumns: 2, headingRows: 3 } ) );
  262. } );
  263. it( 'should fix rowspaned cells on the edge of an table head section when creating section', () => {
  264. setData( model, modelTable( [
  265. [ { rowspan: 2, contents: '00' }, '01' ],
  266. [ '[]11' ]
  267. ], { headingRows: 2 } ) );
  268. command.execute();
  269. assertEqualMarkup( getData( model ), modelTable( [
  270. [ '00', '01' ],
  271. [ '', '[]11' ]
  272. ], { headingRows: 1 } ) );
  273. } );
  274. it( 'should fix rowspaned cells inside a row', () => {
  275. setData( model, modelTable( [
  276. [ '00', { rowspan: 2, contents: '01' } ],
  277. [ '[]10' ]
  278. ], { headingRows: 2 } ) );
  279. command.execute();
  280. assertEqualMarkup( getData( model ), modelTable( [
  281. [ '00', '01' ],
  282. [ '[]10', '' ]
  283. ], { headingRows: 1 } ) );
  284. } );
  285. it( 'should work properly in the first row of a table', () => {
  286. setData( model, modelTable( [
  287. [ '00', '[]01', '02' ],
  288. [ { colspan: 2, rowspan: 2, contents: '10' }, '12' ],
  289. [ '22' ]
  290. ] ) );
  291. command.execute();
  292. assertEqualMarkup( getData( model ), modelTable( [
  293. [ '00', '[]01', '02' ],
  294. [ { colspan: 2, rowspan: 2, contents: '10' }, '12' ],
  295. [ '22' ]
  296. ], { headingRows: 1 } ) );
  297. } );
  298. } );
  299. } );