setheaderrowcommand.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 TableUtils from '../../src/tableutils';
  10. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  11. describe( 'SetHeaderRowCommand', () => {
  12. let editor, model, command;
  13. beforeEach( () => {
  14. return ModelTestEditor
  15. .create( {
  16. plugins: [ TableUtils ]
  17. } )
  18. .then( newEditor => {
  19. editor = newEditor;
  20. model = editor.model;
  21. command = new SetHeaderRowCommand( editor );
  22. defaultSchema( model.schema );
  23. defaultConversion( editor.conversion );
  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. } );
  39. describe( 'value', () => {
  40. it( 'should be false if selection is not in a table without heading row', () => {
  41. setData( model, modelTable( [
  42. [ '01[]', '02' ],
  43. [ '11', '12' ]
  44. ] ) );
  45. expect( command.value ).to.be.false;
  46. } );
  47. it( 'should be false if selection is not in a heading row', () => {
  48. setData( model, modelTable( [
  49. [ '01', '02' ],
  50. [ '11', '12[]' ]
  51. ], { headingRows: 1 } ) );
  52. expect( command.value ).to.be.false;
  53. } );
  54. it( 'should be true if selection is in a heading row', () => {
  55. setData( model, modelTable( [
  56. [ '01[]', '02' ],
  57. [ '11', '12' ]
  58. ], { headingRows: 1 } ) );
  59. expect( command.value ).to.be.true;
  60. } );
  61. it( 'should be false if selection is in a heading column', () => {
  62. setData( model, modelTable( [
  63. [ '01', '02' ],
  64. [ '11[]', '12' ]
  65. ], { headingRows: 1, headingColumns: 1 } ) );
  66. expect( command.value ).to.be.false;
  67. } );
  68. } );
  69. describe( 'execute()', () => {
  70. it( 'should set heading rows attribute that cover row in which is selection', () => {
  71. setData( model, modelTable( [
  72. [ '00' ],
  73. [ '[]10' ],
  74. [ '20' ],
  75. [ '30' ]
  76. ] ) );
  77. command.execute();
  78. assertEqualMarkup( getData( model ), modelTable( [
  79. [ '00' ],
  80. [ '[]10' ],
  81. [ '20' ],
  82. [ '30' ]
  83. ], { headingRows: 2 } ) );
  84. } );
  85. it( 'should toggle heading rows attribute', () => {
  86. setData( model, modelTable( [
  87. [ '00' ],
  88. [ '[]10' ],
  89. [ '20' ],
  90. [ '30' ]
  91. ], { headingRows: 2 } ) );
  92. command.execute();
  93. assertEqualMarkup( getData( model ), modelTable( [
  94. [ '00' ],
  95. [ '[]10' ],
  96. [ '20' ],
  97. [ '30' ]
  98. ], { headingRows: 1 } ) );
  99. command.execute();
  100. setData( model, modelTable( [
  101. [ '00' ],
  102. [ '[]10' ],
  103. [ '20' ],
  104. [ '30' ]
  105. ], { headingRows: 2 } ) );
  106. } );
  107. it( 'should set heading rows attribute if currently selected row is a heading so the heading section is below this row', () => {
  108. setData( model, modelTable( [
  109. [ '00' ],
  110. [ '[]10' ],
  111. [ '20' ],
  112. [ '30' ]
  113. ], { headingRows: 3 } ) );
  114. command.execute();
  115. assertEqualMarkup( getData( model ), modelTable( [
  116. [ '00' ],
  117. [ '[]10' ],
  118. [ '20' ],
  119. [ '30' ]
  120. ], { headingRows: 1 } ) );
  121. } );
  122. it( 'should unsetset heading rows attribute', () => {
  123. setData( model, modelTable( [
  124. [ '[]00' ],
  125. [ '10' ],
  126. [ '20' ],
  127. [ '30' ]
  128. ], { headingRows: 3 } ) );
  129. command.execute();
  130. assertEqualMarkup( getData( model ), modelTable( [
  131. [ '[]00' ],
  132. [ '10' ],
  133. [ '20' ],
  134. [ '30' ]
  135. ] ) );
  136. } );
  137. it( 'should respect forceValue parameter #1', () => {
  138. setData( model, modelTable( [
  139. [ '00' ],
  140. [ '[]10' ],
  141. [ '20' ],
  142. [ '30' ]
  143. ], { headingRows: 3 } ) );
  144. command.execute( { forceValue: true } );
  145. assertEqualMarkup( getData( model ), modelTable( [
  146. [ '00' ],
  147. [ '[]10' ],
  148. [ '20' ],
  149. [ '30' ]
  150. ], { headingRows: 3 } ) );
  151. } );
  152. it( 'should respect forceValue parameter #2', () => {
  153. setData( model, modelTable( [
  154. [ '00' ],
  155. [ '[]10' ],
  156. [ '20' ],
  157. [ '30' ]
  158. ], { headingRows: 1 } ) );
  159. command.execute( { forceValue: false } );
  160. assertEqualMarkup( getData( model ), modelTable( [
  161. [ '00' ],
  162. [ '[]10' ],
  163. [ '20' ],
  164. [ '30' ]
  165. ], { headingRows: 1 } ) );
  166. } );
  167. it( 'should fix rowspaned cells on the edge of an table head section', () => {
  168. setData( model, modelTable( [
  169. [ '00', '01', '02' ],
  170. [ { colspan: 2, rowspan: 2, contents: '10[]' }, '12' ],
  171. [ '22' ]
  172. ], { headingColumns: 2, headingRows: 1 } ) );
  173. command.execute();
  174. assertEqualMarkup( getData( model ), modelTable( [
  175. [ '00', '01', '02' ],
  176. [ { colspan: 2, contents: '10[]' }, '12' ],
  177. [ { colspan: 2, contents: '' }, '22' ]
  178. ], { headingColumns: 2, headingRows: 2 } ) );
  179. } );
  180. it( 'should split to at most 2 table cells when fixing rowspaned cells on the edge of an table head section', () => {
  181. setData( model, modelTable( [
  182. [ '00', '01', '02' ],
  183. [ { colspan: 2, rowspan: 5, contents: '10' }, '12' ],
  184. [ '22[]' ],
  185. [ '32' ],
  186. [ '42' ],
  187. [ '52' ]
  188. ], { headingColumns: 2, headingRows: 1 } ) );
  189. command.execute();
  190. assertEqualMarkup( getData( model ), modelTable( [
  191. [ '00', '01', '02' ],
  192. [ { colspan: 2, rowspan: 2, contents: '10' }, '12' ],
  193. [ '22[]' ],
  194. [ { colspan: 2, rowspan: 3, contents: '' }, '32' ],
  195. [ '42' ],
  196. [ '52' ]
  197. ], { headingColumns: 2, headingRows: 3 } ) );
  198. } );
  199. it( 'should fix rowspaned cells on the edge of an table head section when creating section', () => {
  200. setData( model, modelTable( [
  201. [ { rowspan: 2, contents: '00' }, '01' ],
  202. [ '[]11' ]
  203. ], { headingRows: 2 } ) );
  204. command.execute();
  205. assertEqualMarkup( getData( model ), modelTable( [
  206. [ '00', '01' ],
  207. [ '', '[]11' ]
  208. ], { headingRows: 1 } ) );
  209. } );
  210. it( 'should fix rowspaned cells inside a row', () => {
  211. setData( model, modelTable( [
  212. [ '00', { rowspan: 2, contents: '01' } ],
  213. [ '[]10' ]
  214. ], { headingRows: 2 } ) );
  215. command.execute();
  216. assertEqualMarkup( getData( model ), modelTable( [
  217. [ '00', '01' ],
  218. [ '[]10', '' ]
  219. ], { headingRows: 1 } ) );
  220. } );
  221. it( 'should work properly in the first row of a table', () => {
  222. setData( model, modelTable( [
  223. [ '00', '[]01', '02' ],
  224. [ { colspan: 2, rowspan: 2, contents: '10' }, '12' ],
  225. [ '22' ]
  226. ] ) );
  227. command.execute();
  228. assertEqualMarkup( getData( model ), modelTable( [
  229. [ '00', '[]01', '02' ],
  230. [ { colspan: 2, rowspan: 2, contents: '10' }, '12' ],
  231. [ '22' ]
  232. ], { headingRows: 1 } ) );
  233. } );
  234. } );
  235. } );