setheaderrowcommand.js 7.2 KB

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