tablecellhorizontalalignmentcommand.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  7. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import { assertTableCellStyle, modelTable, viewTable } from '../../_utils/utils';
  9. import TableCellPropertiesEditing from '../../../src/tablecellproperties/tablecellpropertiesediting';
  10. import TableCellHorizontalAlignmentCommand from '../../../src/tablecellproperties/commands/tablecellhorizontalalignmentcommand';
  11. import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  12. describe( 'table cell properties', () => {
  13. describe( 'commands', () => {
  14. describe( 'TableCellHorizontalAlignmentCommand', () => {
  15. let editor, model, command;
  16. beforeEach( async () => {
  17. editor = await ModelTestEditor.create( {
  18. plugins: [ Paragraph, TableCellPropertiesEditing ]
  19. } );
  20. model = editor.model;
  21. command = new TableCellHorizontalAlignmentCommand( editor );
  22. } );
  23. afterEach( () => {
  24. return editor.destroy();
  25. } );
  26. describe( 'isEnabled', () => {
  27. describe( 'collapsed selection', () => {
  28. it( 'should be false if selection does not have table cell', () => {
  29. setData( model, '<paragraph>foo[]</paragraph>' );
  30. expect( command.isEnabled ).to.be.false;
  31. } );
  32. it( 'should be true is selection has table cell', () => {
  33. setData( model, modelTable( [ [ '[]foo' ] ] ) );
  34. expect( command.isEnabled ).to.be.true;
  35. } );
  36. } );
  37. describe( 'non-collapsed selection', () => {
  38. it( 'should be false if selection does not have table cell', () => {
  39. setData( model, '<paragraph>f[oo]</paragraph>' );
  40. expect( command.isEnabled ).to.be.false;
  41. } );
  42. it( 'should be true is selection has table cell', () => {
  43. setData( model, modelTable( [ [ 'f[o]o' ] ] ) );
  44. expect( command.isEnabled ).to.be.true;
  45. } );
  46. } );
  47. describe( 'multi-cell selection', () => {
  48. it( 'should be true if the selection contains some table cells', () => {
  49. setData( model, modelTable( [
  50. [ { contents: '00', isSelected: true }, '01' ],
  51. [ '10', { contents: '11', isSelected: true } ]
  52. ] ) );
  53. expect( command.isEnabled ).to.be.true;
  54. } );
  55. } );
  56. } );
  57. describe( 'value', () => {
  58. describe( 'collapsed selection', () => {
  59. it( 'should be undefined if selected table cell has no horizontalAlignment property', () => {
  60. setData( model, modelTable( [ [ '[]foo' ] ] ) );
  61. expect( command.value ).to.be.undefined;
  62. } );
  63. it( 'should be set if selected table cell has horizontalAlignment property', () => {
  64. setData( model, modelTable( [ [ { horizontalAlignment: 'center', contents: '[]foo' } ] ] ) );
  65. expect( command.value ).to.equal( 'center' );
  66. } );
  67. } );
  68. describe( 'non-collapsed selection', () => {
  69. it( 'should be false if selection does not have table cell', () => {
  70. setData( model, '<paragraph>f[oo]</paragraph>' );
  71. expect( command.value ).to.be.undefined;
  72. } );
  73. it( 'should be true is selection has table cell', () => {
  74. setData( model, modelTable( [ [ { horizontalAlignment: 'center', contents: 'f[o]o' } ] ] ) );
  75. expect( command.value ).to.equal( 'center' );
  76. } );
  77. } );
  78. describe( 'multi-cell selection', () => {
  79. it( 'should be undefined if no table cells have the "horizontalAlignment" property', () => {
  80. setData( model, modelTable( [
  81. [
  82. { contents: '00', isSelected: true },
  83. { contents: '01', isSelected: true }
  84. ],
  85. [
  86. '10',
  87. { contents: '11', isSelected: true }
  88. ]
  89. ] ) );
  90. expect( command.value ).to.be.undefined;
  91. } );
  92. it( 'should be undefined if only some table cells have the "horizontalAlignment" property', () => {
  93. setData( model, modelTable( [
  94. [
  95. { contents: '00', isSelected: true, horizontalAlignment: 'center' },
  96. { contents: '01', isSelected: true }
  97. ],
  98. [
  99. '10',
  100. { contents: '11', isSelected: true, horizontalAlignment: 'center' }
  101. ]
  102. ] ) );
  103. expect( command.value ).to.be.undefined;
  104. } );
  105. it( 'should be undefined if one of selected table cells has a different "horizontalAlignment" property value', () => {
  106. setData( model, modelTable( [
  107. [
  108. { contents: '00', isSelected: true, horizontalAlignment: 'center' },
  109. { contents: '01', isSelected: true, horizontalAlignment: 'right' }
  110. ],
  111. [
  112. '10',
  113. { contents: '11', isSelected: true, horizontalAlignment: 'center' }
  114. ]
  115. ] ) );
  116. expect( command.value ).to.be.undefined;
  117. } );
  118. it( 'should be set if all table cells have the same "horizontalAlignment" property value', () => {
  119. setData( model, modelTable( [
  120. [
  121. { contents: '00', isSelected: true, horizontalAlignment: 'center' },
  122. { contents: '01', isSelected: true, horizontalAlignment: 'center' }
  123. ],
  124. [
  125. '10',
  126. { contents: '11', isSelected: true, horizontalAlignment: 'center' }
  127. ]
  128. ] ) );
  129. expect( command.value ).to.equal( 'center' );
  130. } );
  131. } );
  132. } );
  133. describe( 'execute()', () => {
  134. it( 'should use provided batch', () => {
  135. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  136. const batch = model.createBatch();
  137. const spy = sinon.spy( model, 'enqueueChange' );
  138. command.execute( { value: 'right', batch } );
  139. sinon.assert.calledWith( spy, batch );
  140. } );
  141. describe( 'collapsed selection', () => {
  142. it( 'should set selected table cell horizontalAlignment to a passed value', () => {
  143. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  144. command.execute( { value: 'right' } );
  145. assertTableCellStyle( editor, 'text-align:right;' );
  146. } );
  147. it( 'should change selected table cell horizontalAlignment to a passed value', () => {
  148. setData( model, modelTable( [ [ { horizontalAlignment: 'center', contents: '[]foo' } ] ] ) );
  149. command.execute( { value: 'right' } );
  150. assertTableCellStyle( editor, 'text-align:right;' );
  151. } );
  152. it( 'should remove horizontalAlignment from a selected table cell if no value is passed', () => {
  153. setData( model, modelTable( [ [ { horizontalAlignment: 'center', contents: '[]foo' } ] ] ) );
  154. command.execute();
  155. assertTableCellStyle( editor, '' );
  156. } );
  157. } );
  158. describe( 'non-collapsed selection', () => {
  159. it( 'should set selected table cell horizontalAlignment to a passed value', () => {
  160. setData( model, modelTable( [ [ '[foo]' ] ] ) );
  161. command.execute( { value: 'right' } );
  162. assertTableCellStyle( editor, 'text-align:right;' );
  163. } );
  164. it( 'should change selected table cell horizontalAlignment to a passed value', () => {
  165. setData( model, modelTable( [ [ '[foo]' ] ] ) );
  166. command.execute( { value: 'right' } );
  167. assertTableCellStyle( editor, 'text-align:right;' );
  168. } );
  169. it( 'should remove horizontalAlignment from a selected table cell if no value is passed', () => {
  170. setData( model, modelTable( [ [ '[foo]' ] ] ) );
  171. command.execute();
  172. assertTableCellStyle( editor, '' );
  173. } );
  174. } );
  175. describe( 'multi-cell selection', () => {
  176. beforeEach( () => {
  177. setData( model, modelTable( [
  178. [ { contents: '00', isSelected: true }, '01' ],
  179. [ '10', { contents: '11', isSelected: true } ]
  180. ] ) );
  181. } );
  182. it( 'should set the "horizontalAlignment" attribute value of selected table cells', () => {
  183. command.execute( { value: 'right' } );
  184. assertEqualMarkup( editor.getData(), viewTable( [
  185. [ { contents: '00', style: 'text-align:right;' }, '01' ],
  186. [ '10', { contents: '11', style: 'text-align:right;' } ]
  187. ] ) );
  188. } );
  189. it( 'should remove the "horizontalAlignment" attribute from selected table cells if no value is passed', () => {
  190. setData( model, modelTable( [
  191. [ { contents: '00', isSelected: true, horizontalAlignment: 'right' }, '01' ],
  192. [ '10', { contents: '11', isSelected: true, horizontalAlignment: 'right' } ]
  193. ] ) );
  194. command.execute();
  195. assertEqualMarkup( editor.getData(), viewTable( [
  196. [ '00', '01' ],
  197. [ '10', '11' ]
  198. ] ) );
  199. } );
  200. } );
  201. } );
  202. } );
  203. } );
  204. } );