tablecellpaddingcommand.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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, setTableCellWithObjectAttributes } from '../../_utils/utils';
  9. import TableCellPropertiesEditing from '../../../src/tablecellproperties/tablecellpropertiesediting';
  10. import TableCellPaddingCommand from '../../../src/tablecellproperties/commands/tablecellpaddingcommand';
  11. describe( 'table cell properties', () => {
  12. describe( 'commands', () => {
  13. describe( 'TableCellPaddingCommand', () => {
  14. let editor, model, command;
  15. beforeEach( async () => {
  16. editor = await ModelTestEditor.create( {
  17. plugins: [ Paragraph, TableCellPropertiesEditing ]
  18. } );
  19. model = editor.model;
  20. command = new TableCellPaddingCommand( editor );
  21. } );
  22. afterEach( () => {
  23. return editor.destroy();
  24. } );
  25. describe( 'isEnabled', () => {
  26. describe( 'collapsed selection', () => {
  27. it( 'should be false if selection does not have table cell', () => {
  28. setData( model, '<paragraph>foo[]</paragraph>' );
  29. expect( command.isEnabled ).to.be.false;
  30. } );
  31. it( 'should be true is selection has table cell', () => {
  32. setData( model, modelTable( [ [ '[]foo' ] ] ) );
  33. expect( command.isEnabled ).to.be.true;
  34. } );
  35. } );
  36. describe( 'non-collapsed selection', () => {
  37. it( 'should be false if selection does not have table cell', () => {
  38. setData( model, '<paragraph>f[oo]</paragraph>' );
  39. expect( command.isEnabled ).to.be.false;
  40. } );
  41. it( 'should be true is selection has table cell', () => {
  42. setData( model, modelTable( [ [ 'f[o]o' ] ] ) );
  43. expect( command.isEnabled ).to.be.true;
  44. } );
  45. } );
  46. } );
  47. describe( 'value', () => {
  48. describe( 'collapsed selection', () => {
  49. it( 'should be undefined if selected table cell has no padding property', () => {
  50. setData( model, modelTable( [ [ '[]foo' ] ] ) );
  51. expect( command.value ).to.be.undefined;
  52. } );
  53. it( 'should be set if selected table cell has padding property (single string)', () => {
  54. setData( model, modelTable( [ [ { padding: '2em', contents: '[]foo' } ] ] ) );
  55. expect( command.value ).to.equal( '2em' );
  56. } );
  57. it( 'should be set if selected table cell has padding property object with same values', () => {
  58. setTableCellWithObjectAttributes( model, {
  59. padding: {
  60. top: '2em',
  61. right: '2em',
  62. bottom: '2em',
  63. left: '2em'
  64. }
  65. }, '[]foo' );
  66. expect( command.value ).to.equal( '2em' );
  67. } );
  68. it( 'should be undefined if selected table cell has padding property object with different values', () => {
  69. setTableCellWithObjectAttributes( model, {
  70. padding: {
  71. top: '2em',
  72. right: '1px',
  73. bottom: '2em',
  74. left: '2em'
  75. }
  76. }, '[]foo' );
  77. expect( command.value ).to.be.undefined;
  78. } );
  79. } );
  80. describe( 'non-collapsed selection', () => {
  81. it( 'should be false if selection does not have table cell', () => {
  82. setData( model, '<paragraph>f[oo]</paragraph>' );
  83. expect( command.value ).to.be.undefined;
  84. } );
  85. it( 'should be true is selection has table cell', () => {
  86. setData( model, modelTable( [ [ { padding: '2em', contents: 'f[o]o' } ] ] ) );
  87. expect( command.value ).to.equal( '2em' );
  88. } );
  89. } );
  90. } );
  91. describe( 'execute()', () => {
  92. it( 'should use provided batch', () => {
  93. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  94. const batch = model.createBatch();
  95. const spy = sinon.spy( model, 'enqueueChange' );
  96. command.execute( { value: '0.5em', batch } );
  97. sinon.assert.calledWith( spy, batch );
  98. } );
  99. it( 'should add default unit for numeric values (number passed)', () => {
  100. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  101. command.execute( { value: 25 } );
  102. assertTableCellStyle( editor, 'padding:25px;' );
  103. } );
  104. it( 'should add default unit for numeric values (string passed)', () => {
  105. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  106. command.execute( { value: 25 } );
  107. assertTableCellStyle( editor, 'padding:25px;' );
  108. } );
  109. it( 'should not add default unit for numeric values with unit', () => {
  110. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  111. command.execute( { value: '25pt' } );
  112. assertTableCellStyle( editor, 'padding:25pt;' );
  113. } );
  114. it( 'should add default unit to floats (number passed)', () => {
  115. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  116. command.execute( { value: 25.1 } );
  117. assertTableCellStyle( editor, 'padding:25.1px;' );
  118. } );
  119. it( 'should add default unit to floats (string passed)', () => {
  120. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  121. command.execute( { value: '0.1' } );
  122. assertTableCellStyle( editor, 'padding:0.1px;' );
  123. } );
  124. it( 'should pass invalid values', () => {
  125. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  126. command.execute( { value: 'bar' } );
  127. assertTableCellStyle( editor, 'padding:bar;' );
  128. } );
  129. it( 'should pass invalid value (string passed, CSS float without leading 0)', () => {
  130. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  131. command.execute( { value: '.2' } );
  132. assertTableCellStyle( editor, 'padding:.2;' );
  133. } );
  134. describe( 'collapsed selection', () => {
  135. it( 'should set selected table cell padding to a passed value', () => {
  136. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  137. command.execute( { value: '0.5em' } );
  138. assertTableCellStyle( editor, 'padding:0.5em;' );
  139. } );
  140. it( 'should change selected table cell padding to a passed value', () => {
  141. setData( model, modelTable( [ [ { padding: '2em', contents: '[]foo' } ] ] ) );
  142. command.execute( { value: '0.5em' } );
  143. assertTableCellStyle( editor, 'padding:0.5em;' );
  144. } );
  145. it( 'should remove padding from a selected table cell if no value is passed', () => {
  146. setData( model, modelTable( [ [ { padding: '2em', contents: '[]foo' } ] ] ) );
  147. command.execute();
  148. assertTableCellStyle( editor, '' );
  149. } );
  150. } );
  151. describe( 'non-collapsed selection', () => {
  152. it( 'should set selected table cell padding to a passed value', () => {
  153. setData( model, modelTable( [ [ '[foo]' ] ] ) );
  154. command.execute( { value: '0.5em' } );
  155. assertTableCellStyle( editor, 'padding:0.5em;' );
  156. } );
  157. it( 'should change selected table cell padding to a passed value', () => {
  158. setData( model, modelTable( [ [ '[foo]' ] ] ) );
  159. command.execute( { value: '0.5em' } );
  160. assertTableCellStyle( editor, 'padding:0.5em;' );
  161. } );
  162. it( 'should remove padding from a selected table cell if no value is passed', () => {
  163. setData( model, modelTable( [ [ '[foo]' ] ] ) );
  164. command.execute();
  165. assertTableCellStyle( editor, '' );
  166. } );
  167. } );
  168. } );
  169. } );
  170. } );
  171. } );