setheadercolumncommand.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 SetHeaderColumnCommand from '../../src/commands/setheadercolumncommand';
  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( 'SetHeaderColumnCommand', () => {
  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 SetHeaderColumnCommand( 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 columns 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 header columns are selected', () => {
  52. setData( model, modelTable( [
  53. [ '01', '02', '03' ]
  54. ], { headingColumns: 2 } ) );
  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 heading column', () => {
  66. setData( model, modelTable( [
  67. [ '01', '02' ],
  68. [ '11', '12[]' ]
  69. ], { headingColumns: 1 } ) );
  70. expect( command.value ).to.be.false;
  71. } );
  72. it( 'should be true if selection is in a heading column', () => {
  73. setData( model, modelTable( [
  74. [ '01[]', '02' ],
  75. [ '11', '12' ]
  76. ], { headingColumns: 1 } ) );
  77. expect( command.value ).to.be.true;
  78. } );
  79. it( 'should be true if multiple header columns are selected', () => {
  80. setData( model, modelTable( [
  81. [ '01', '02', '03' ]
  82. ], { headingColumns: 2 } ) );
  83. const tableSelection = editor.plugins.get( TableSelection );
  84. const modelRoot = model.document.getRoot();
  85. tableSelection._setCellSelection(
  86. modelRoot.getNodeByPath( [ 0, 0, 0 ] ),
  87. modelRoot.getNodeByPath( [ 0, 0, 1 ] )
  88. );
  89. expect( command.value ).to.be.true;
  90. } );
  91. it( 'should be true if multiple header columns are selected in reversed order', () => {
  92. setData( model, modelTable( [
  93. [ '01', '02', '03' ]
  94. ], { headingColumns: 2 } ) );
  95. const tableSelection = editor.plugins.get( TableSelection );
  96. const modelRoot = model.document.getRoot();
  97. tableSelection._setCellSelection(
  98. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  99. modelRoot.getNodeByPath( [ 0, 0, 0 ] )
  100. );
  101. expect( command.value ).to.be.true;
  102. } );
  103. it( 'should be false if selection is in a heading row', () => {
  104. setData( model, modelTable( [
  105. [ '01', '02[]' ],
  106. [ '11', '12' ]
  107. ], { headingRows: 1, headingColumns: 1 } ) );
  108. expect( command.value ).to.be.false;
  109. } );
  110. it( 'should be false if only part of selected columns are headers', () => {
  111. setData( model, modelTable( [
  112. [ '01', '02', '03', '04' ]
  113. ], { headingColumns: 2 } ) );
  114. const tableSelection = editor.plugins.get( TableSelection );
  115. const modelRoot = model.document.getRoot();
  116. tableSelection._setCellSelection(
  117. modelRoot.getNodeByPath( [ 0, 0, 1 ] ),
  118. modelRoot.getNodeByPath( [ 0, 0, 2 ] )
  119. );
  120. expect( command.value ).to.be.false;
  121. } );
  122. } );
  123. describe( 'execute()', () => {
  124. it( 'should set heading columns attribute that cover column in which is selection', () => {
  125. setData( model, modelTable( [
  126. [ '00', '01[]', '02', '03' ]
  127. ] ) );
  128. command.execute();
  129. assertEqualMarkup( getData( model ), modelTable( [
  130. [ '00', '01[]', '02', '03' ]
  131. ], { headingColumns: 2 } ) );
  132. } );
  133. it( 'should set heading columns attribute below current selection column', () => {
  134. setData( model, modelTable( [
  135. [ '00', '01[]', '02', '03' ]
  136. ], { headingColumns: 3 } ) );
  137. command.execute();
  138. assertEqualMarkup( getData( model ), modelTable( [
  139. [ '00', '01[]', '02', '03' ]
  140. ], { headingColumns: 1 } ) );
  141. } );
  142. it( 'should toggle of selected column', () => {
  143. setData( model, modelTable( [
  144. [ '00', '01[]', '02', '03' ]
  145. ], { headingColumns: 2 } ) );
  146. command.execute();
  147. assertEqualMarkup( getData( model ), modelTable( [
  148. [ '00', '01[]', '02', '03' ]
  149. ], { headingColumns: 1 } ) );
  150. command.execute();
  151. assertEqualMarkup( getData( model ), modelTable( [
  152. [ '00', '01[]', '02', '03' ]
  153. ], { headingColumns: 2 } ) );
  154. } );
  155. it( 'should respect forceValue parameter #1', () => {
  156. setData( model, modelTable( [
  157. [ '00', '01[]', '02', '03' ]
  158. ], { headingColumns: 3 } ) );
  159. command.execute( { forceValue: true } );
  160. assertEqualMarkup( getData( model ), modelTable( [
  161. [ '00', '01[]', '02', '03' ]
  162. ], { headingColumns: 3 } ) );
  163. } );
  164. it( 'should respect forceValue parameter #2', () => {
  165. setData( model, modelTable( [
  166. [ '00', '01[]', '02', '03' ]
  167. ], { headingColumns: 1 } ) );
  168. command.execute( { forceValue: false } );
  169. assertEqualMarkup( getData( model ), modelTable( [
  170. [ '00', '01[]', '02', '03' ]
  171. ], { headingColumns: 1 } ) );
  172. } );
  173. } );
  174. } );