tableborderwidthcommand.js 7.6 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 { assertTableStyle, modelTable, setTableWithObjectAttributes } from '../../_utils/utils';
  9. import TablePropertiesEditing from '../../../src/tableproperties/tablepropertiesediting';
  10. import TableBorderWidthCommand from '../../../src/tableproperties/commands/tableborderwidthcommand';
  11. describe( 'table properties', () => {
  12. describe( 'commands', () => {
  13. describe( 'TableBorderWidthCommand', () => {
  14. let editor, model, command;
  15. beforeEach( async () => {
  16. editor = await ModelTestEditor.create( {
  17. plugins: [ Paragraph, TablePropertiesEditing ]
  18. } );
  19. model = editor.model;
  20. command = new TableBorderWidthCommand( 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', () => {
  28. setData( model, '<paragraph>foo[]</paragraph>' );
  29. expect( command.isEnabled ).to.be.false;
  30. } );
  31. it( 'should be true is selection has table', () => {
  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', () => {
  38. setData( model, '<paragraph>f[oo]</paragraph>' );
  39. expect( command.isEnabled ).to.be.false;
  40. } );
  41. it( 'should be true is selection has table', () => {
  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 has no borderWidth property', () => {
  50. setData( model, modelTable( [ [ '[]foo' ] ] ) );
  51. expect( command.value ).to.be.undefined;
  52. } );
  53. it( 'should be set if selected table has borderWidth property (single string)', () => {
  54. setData( model, modelTable( [ [ '[]foo' ] ], { borderWidth: '2em' } ) );
  55. expect( command.value ).to.equal( '2em' );
  56. } );
  57. it( 'should be set if selected table has borderWidth property object with same values', () => {
  58. setTableWithObjectAttributes( model, {
  59. borderWidth: {
  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 has borderWidth property object with different values', () => {
  69. setTableWithObjectAttributes( model, {
  70. borderWidth: {
  71. top: '2em',
  72. right: '333px',
  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', () => {
  82. setData( model, '<paragraph>f[oo]</paragraph>' );
  83. expect( command.value ).to.be.undefined;
  84. } );
  85. it( 'should be true is selection has table', () => {
  86. setData( model, modelTable( [ [ 'f[o]o' ] ], { borderWidth: '2em' } ) );
  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: '1px', 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. assertTableStyle( editor, 'border-bottom:25px;border-left:25px;border-right:25px;border-top:25px;' );
  103. } );
  104. it( 'should add default unit for numeric values (string passed)', () => {
  105. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  106. command.execute( { value: 25 } );
  107. assertTableStyle( editor, 'border-bottom:25px;border-left:25px;border-right:25px;border-top: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. assertTableStyle( editor, 'border-bottom:25pt;border-left:25pt;border-right:25pt;border-top:25pt;' );
  113. } );
  114. it( 'should add default unit to floats (number passed)', () => {
  115. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  116. command.execute( { value: 25.1 } );
  117. assertTableStyle( editor, 'border-bottom:25.1px;border-left:25.1px;border-right:25.1px;border-top: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. assertTableStyle( editor, 'border-bottom:0.1px;border-left:0.1px;border-right:0.1px;border-top:0.1px;' );
  123. } );
  124. it( 'should pass invalid values', () => {
  125. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  126. command.execute( { value: 'bar' } );
  127. assertTableStyle( editor, 'border-bottom:bar;border-left:bar;border-right:bar;border-top: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. assertTableStyle( editor, 'border-bottom:.2;border-left:.2;border-right:.2;border-top:.2;' );
  133. } );
  134. describe( 'collapsed selection', () => {
  135. it( 'should set selected table borderWidth to a passed value', () => {
  136. setData( model, modelTable( [ [ 'foo[]' ] ] ) );
  137. command.execute( { value: '1px' } );
  138. assertTableStyle( editor, 'border-bottom:1px;border-left:1px;border-right:1px;border-top:1px;' );
  139. } );
  140. it( 'should change selected table borderWidth to a passed value', () => {
  141. setData( model, modelTable( [ [ '[]foo' ] ], { borderWidth: '2em' } ) );
  142. command.execute( { value: '1px' } );
  143. assertTableStyle( editor, 'border-bottom:1px;border-left:1px;border-right:1px;border-top:1px;' );
  144. } );
  145. it( 'should remove borderWidth from a selected table if no value is passed', () => {
  146. setData( model, modelTable( [ [ '[]foo' ] ], { borderWidth: '2em' } ) );
  147. command.execute();
  148. assertTableStyle( editor, '' );
  149. } );
  150. } );
  151. describe( 'non-collapsed selection', () => {
  152. it( 'should set selected table borderWidth to a passed value', () => {
  153. setData( model, modelTable( [ [ '[foo]' ] ] ) );
  154. command.execute( { value: '1px' } );
  155. assertTableStyle( editor, 'border-bottom:1px;border-left:1px;border-right:1px;border-top:1px;' );
  156. } );
  157. it( 'should change selected table borderWidth to a passed value', () => {
  158. setData( model, modelTable( [ [ '[foo]' ] ] ) );
  159. command.execute( { value: '1px' } );
  160. assertTableStyle( editor, 'border-bottom:1px;border-left:1px;border-right:1px;border-top:1px;' );
  161. } );
  162. it( 'should remove borderWidth from a selected table if no value is passed', () => {
  163. setData( model, modelTable( [ [ '[foo]' ] ] ) );
  164. command.execute();
  165. assertTableStyle( editor, '' );
  166. } );
  167. } );
  168. } );
  169. } );
  170. } );
  171. } );