setheaderrowcommand.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 fix rowspaned cells on the edge of an table head section', () => {
  137. setData( model, modelTable( [
  138. [ '00', '01', '02' ],
  139. [ { colspan: 2, rowspan: 2, contents: '10[]' }, '12' ],
  140. [ '22' ]
  141. ], { headingColumns: 2, headingRows: 1 } ) );
  142. command.execute();
  143. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  144. [ '00', '01', '02' ],
  145. [ { colspan: 2, contents: '10[]' }, '12' ],
  146. [ { colspan: 2, contents: '' }, '22' ]
  147. ], { headingColumns: 2, headingRows: 2 } ) );
  148. } );
  149. it( 'should split to at most 2 table cells when fixing rowspaned cells on the edge of an table head section', () => {
  150. setData( model, modelTable( [
  151. [ '00', '01', '02' ],
  152. [ { colspan: 2, rowspan: 5, contents: '10' }, '12' ],
  153. [ '22[]' ],
  154. [ '32' ],
  155. [ '42' ],
  156. [ '52' ]
  157. ], { headingColumns: 2, headingRows: 1 } ) );
  158. command.execute();
  159. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  160. [ '00', '01', '02' ],
  161. [ { colspan: 2, rowspan: 2, contents: '10' }, '12' ],
  162. [ '22[]' ],
  163. [ { colspan: 2, rowspan: 3, contents: '' }, '32' ],
  164. [ '42' ],
  165. [ '52' ]
  166. ], { headingColumns: 2, headingRows: 3 } ) );
  167. } );
  168. it( 'should fix rowspaned cells on the edge of an table head section when creating section', () => {
  169. setData( model, modelTable( [
  170. [ { rowspan: 2, contents: '00' }, '01' ],
  171. [ '[]11' ]
  172. ], { headingRows: 2 } ) );
  173. command.execute();
  174. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  175. [ '00', '01' ],
  176. [ '', '[]11' ]
  177. ], { headingRows: 1 } ) );
  178. } );
  179. it( 'should fix rowspaned cells inside a row', () => {
  180. setData( model, modelTable( [
  181. [ '00', { rowspan: 2, contents: '01' } ],
  182. [ '[]10' ]
  183. ], { headingRows: 2 } ) );
  184. command.execute();
  185. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  186. [ '00', '01' ],
  187. [ '[]10', '' ]
  188. ], { headingRows: 1 } ) );
  189. } );
  190. it( 'should work properly in the first row of a table', () => {
  191. setData( model, modelTable( [
  192. [ '00', '[]01', '02' ],
  193. [ { colspan: 2, rowspan: 2, contents: '10' }, '12' ],
  194. [ '22' ]
  195. ] ) );
  196. command.execute();
  197. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  198. [ '00', '[]01', '02' ],
  199. [ { colspan: 2, rowspan: 2, contents: '10' }, '12' ],
  200. [ '22' ]
  201. ], { headingRows: 1 } ) );
  202. } );
  203. } );
  204. } );