setheaderrowcommand.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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( 'HeaderRowCommand', () => {
  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>foo[]</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', '01' ],
  72. [ '[]10', '11' ],
  73. [ '20', '21' ]
  74. ] ) );
  75. command.execute();
  76. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  77. [ '00', '01' ],
  78. [ '[]10', '11' ],
  79. [ '20', '21' ]
  80. ], { headingRows: 2 } ) );
  81. } );
  82. it( 'should toggle heading rows attribute', () => {
  83. setData( model, modelTable( [
  84. [ '[]00', '01' ],
  85. [ '10', '11' ],
  86. [ '20', '21' ]
  87. ] ) );
  88. command.execute();
  89. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  90. [ '[]00', '01' ],
  91. [ '10', '11' ],
  92. [ '20', '21' ]
  93. ], { headingRows: 1 } ) );
  94. command.execute();
  95. setData( model, modelTable( [
  96. [ '[]00', '01' ],
  97. [ '10', '11' ],
  98. [ '20', '21' ]
  99. ] ) );
  100. } );
  101. it( 'should set heading rows attribute if currently selected row is a heading so the heading section is below this row', () => {
  102. setData( model, modelTable( [
  103. [ '00', '01' ],
  104. [ '[]10', '11' ],
  105. [ '20', '21' ]
  106. ], { headingRows: 2 } ) );
  107. command.execute();
  108. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  109. [ '00', '01' ],
  110. [ '[]10', '11' ],
  111. [ '20', '21' ]
  112. ], { headingRows: 1 } ) );
  113. } );
  114. it( 'should fix rowspaned cells on the edge of an table head section', () => {
  115. setData( model, modelTable( [
  116. [ '00', '01', '02' ],
  117. [ { colspan: 2, rowspan: 2, contents: '10[]' }, '12' ],
  118. [ '22' ]
  119. ], { headingColumns: 2, headingRows: 1 } ) );
  120. command.execute();
  121. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  122. [ '00', '01', '02' ],
  123. [ { colspan: 2, contents: '10[]' }, '12' ],
  124. [ { colspan: 2, contents: '' }, '22' ]
  125. ], { headingColumns: 2, headingRows: 2 } ) );
  126. } );
  127. it( 'should split to at most 2 table cells when fixing rowspaned cells on the edge of an table head section', () => {
  128. setData( model, modelTable( [
  129. [ '00', '01', '02' ],
  130. [ { colspan: 2, rowspan: 5, contents: '10' }, '12' ],
  131. [ '22[]' ],
  132. [ '32' ],
  133. [ '42' ],
  134. [ '52' ]
  135. ], { headingColumns: 2, headingRows: 1 } ) );
  136. command.execute();
  137. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  138. [ '00', '01', '02' ],
  139. [ { colspan: 2, rowspan: 2, contents: '10' }, '12' ],
  140. [ '22[]' ],
  141. [ { colspan: 2, rowspan: 3, contents: '' }, '32' ],
  142. [ '42' ],
  143. [ '52' ]
  144. ], { headingColumns: 2, headingRows: 3 } ) );
  145. } );
  146. it( 'should fix rowspaned cells on the edge of an table head section when creating section', () => {
  147. setData( model, modelTable( [
  148. [ { rowspan: 2, contents: '[]00' }, '01' ],
  149. [ '11' ]
  150. ], { headingRows: 2 } ) );
  151. command.execute();
  152. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  153. [ '[]00', '01' ],
  154. [ '', '11' ]
  155. ], { headingRows: 1 } ) );
  156. } );
  157. it( 'should fix rowspaned cells inside a row', () => {
  158. setData( model, modelTable( [
  159. [ '00', { rowspan: 2, contents: '[]01' } ],
  160. [ '10' ]
  161. ], { headingRows: 2 } ) );
  162. command.execute();
  163. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  164. [ '00', '[]01' ],
  165. [ '10', '' ]
  166. ], { headingRows: 1 } ) );
  167. } );
  168. it( 'should work properly in the first row of a table', () => {
  169. setData( model, modelTable( [
  170. [ '00', '[]01', '02' ],
  171. [ { colspan: 2, rowspan: 2, contents: '10' }, '12' ],
  172. [ '22' ]
  173. ] ) );
  174. command.execute();
  175. expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
  176. [ '00', '[]01', '02' ],
  177. [ { colspan: 2, rowspan: 2, contents: '10' }, '12' ],
  178. [ '22' ]
  179. ], { headingRows: 1 } ) );
  180. } );
  181. } );
  182. } );