styles.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import Styles from '../../src/view/styles';
  6. import encodedImage from './_utils/encodedimage.txt';
  7. describe( 'Styles', () => {
  8. let styles;
  9. beforeEach( () => {
  10. styles = new Styles();
  11. } );
  12. describe( 'size getter', () => {
  13. it( 'should return 0 if no styles are set', () => {
  14. expect( styles.size ).to.equal( 0 );
  15. } );
  16. it( 'should return number of set styles', () => {
  17. styles.setStyle( 'color:blue' );
  18. expect( styles.size ).to.equal( 1 );
  19. styles.setStyle( 'margin:1px;' );
  20. expect( styles.size ).to.equal( 1 );
  21. styles.setStyle( 'margin-top:1px;margin-bottom:1px;' );
  22. expect( styles.size ).to.equal( 2 );
  23. } );
  24. } );
  25. describe( 'setStyle()', () => {
  26. it( 'should reset styles to a new value', () => {
  27. styles.setStyle( 'color:red;margin-top:1px;' );
  28. expect( styles.getNormalized() ).to.deep.equal( { color: 'red', margin: { top: '1px' } } );
  29. styles.setStyle( 'margin-bottom:2em;' );
  30. expect( styles.getNormalized() ).to.deep.equal( { margin: { bottom: '2em' } } );
  31. } );
  32. describe( 'styles parsing edge cases and incorrect styles', () => {
  33. it( 'should not crash and not add any styles if styles attribute was empty', () => {
  34. styles.setStyle( '' );
  35. expect( styles.getStyleNames() ).to.deep.equal( [] );
  36. } );
  37. it( 'should be able to parse big styles definition', () => {
  38. expect( () => {
  39. styles.setStyle( `background-image:url('data:image/jpeg;base64,${ encodedImage }')` );
  40. } ).not.to.throw();
  41. } );
  42. it( 'should work with both types of quotes and ignore values inside quotes', () => {
  43. styles.setStyle( 'background-image:url("im;color:g.jpg")' );
  44. expect( styles.getInlineProperty( 'background-image' ) ).to.equal( 'url("im;color:g.jpg")' );
  45. styles.setStyle( 'background-image:url(\'im;color:g.jpg\')' );
  46. expect( styles.getInlineProperty( 'background-image' ) ).to.equal( 'url(\'im;color:g.jpg\')' );
  47. } );
  48. it( 'should not be confused by whitespaces', () => {
  49. styles.setStyle( '\ncolor:\n red ' );
  50. expect( styles.getInlineProperty( 'color' ) ).to.equal( 'red' );
  51. } );
  52. it( 'should not be confused by duplicated semicolon', () => {
  53. styles.setStyle( 'color: red;; display: inline' );
  54. expect( styles.getInlineProperty( 'color' ) ).to.equal( 'red' );
  55. expect( styles.getInlineProperty( 'display' ) ).to.equal( 'inline' );
  56. } );
  57. it( 'should not throw when value is missing', () => {
  58. styles.setStyle( 'color:; display: inline' );
  59. expect( styles.getInlineProperty( 'color' ) ).to.equal( '' );
  60. expect( styles.getInlineProperty( 'display' ) ).to.equal( 'inline' );
  61. } );
  62. it( 'should not throw when colon is duplicated', () => {
  63. styles.setStyle( 'color:: red; display: inline' );
  64. // The result makes no sense, but here we just check that the algorithm doesn't crash.
  65. expect( styles.getInlineProperty( 'color' ) ).to.equal( ': red' );
  66. expect( styles.getInlineProperty( 'display' ) ).to.equal( 'inline' );
  67. } );
  68. it( 'should not throw when random stuff passed', () => {
  69. styles.setStyle( 'color: red;:; ;;" ": display: inline; \'aaa;:' );
  70. // The result makes no sense, but here we just check that the algorithm doesn't crash.
  71. expect( styles.getInlineProperty( 'color' ) ).to.equal( 'red' );
  72. expect( styles.getInlineProperty( 'display' ) ).to.be.undefined;
  73. } );
  74. } );
  75. } );
  76. describe( 'getInlineStyle()', () => {
  77. it( 'should return undefined for empty styles', () => {
  78. expect( styles.getInlineStyle() ).to.be.undefined;
  79. } );
  80. it( 'should return sorted styles string if styles are set', () => {
  81. styles.setStyle( 'margin-top:1px;color:blue;' );
  82. expect( styles.getInlineStyle() ).to.equal( 'color:blue;margin-top:1px;' );
  83. } );
  84. } );
  85. describe( 'getInlineProperty', () => {
  86. it( 'should return empty string for missing shorthand', () => {
  87. styles.setStyle( 'margin-top:1px' );
  88. expect( styles.getInlineProperty( 'margin' ) ).to.be.undefined;
  89. } );
  90. } );
  91. describe( 'hasProperty()', () => {
  92. it( 'should return false if property is not set', () => {
  93. expect( styles.hasProperty( 'foo' ) ).to.be.false;
  94. } );
  95. it( 'should return false if normalized property is not set', () => {
  96. styles.setStyle( 'margin-top:1px' );
  97. // TODO
  98. // expect( styles.hasProperty( 'margin' ) ).to.be.false;
  99. expect( styles.hasProperty( 'margin' ) ).to.be.true;
  100. } );
  101. it( 'should return true if property is set', () => {
  102. styles.setStyle( 'color:deeppink' );
  103. expect( styles.hasProperty( 'color' ) ).to.be.true;
  104. } );
  105. it( 'should return true if normalized shorthanded property is set', () => {
  106. styles.setStyle( 'margin:1px 2px 3px 4px' );
  107. expect( styles.hasProperty( 'margin-top' ) ).to.be.true;
  108. } );
  109. } );
  110. describe( 'insertProperty()', () => {
  111. it( 'should insert new property (empty styles)', () => {
  112. styles.insertProperty( 'color', 'blue' );
  113. expect( styles.getInlineProperty( 'color' ) ).to.equal( 'blue' );
  114. } );
  115. it( 'should insert new property (other properties are set)', () => {
  116. styles.setStyle( 'margin: 1px;' );
  117. styles.insertProperty( 'color', 'blue' );
  118. expect( styles.getInlineProperty( 'color' ) ).to.equal( 'blue' );
  119. } );
  120. it( 'should overwrite property', () => {
  121. styles.setStyle( 'color: red;' );
  122. styles.insertProperty( 'color', 'blue' );
  123. expect( styles.getInlineProperty( 'color' ) ).to.equal( 'blue' );
  124. } );
  125. it( 'should set multiple styles by providing an object', () => {
  126. styles.setStyle( 'color: red;' );
  127. styles.insertProperty( { color: 'blue', margin: '1px' } );
  128. expect( styles.getInlineProperty( 'color' ) ).to.equal( 'blue' );
  129. expect( styles.getInlineProperty( 'margin-top' ) ).to.equal( '1px' );
  130. } );
  131. it( 'should set object property', () => {
  132. styles.setStyle( 'margin:1px;' );
  133. styles.insertProperty( 'margin', { right: '2px' } );
  134. expect( styles.getInlineProperty( 'margin-left' ) ).to.equal( '1px' );
  135. expect( styles.getInlineProperty( 'margin-right' ) ).to.equal( '2px' );
  136. } );
  137. } );
  138. describe( 'removeProperty()', () => {
  139. it( 'should do nothing if property is not set', () => {
  140. styles.removeProperty( 'color' );
  141. expect( styles.getInlineProperty( 'color' ) ).to.be.undefined;
  142. } );
  143. it( 'should insert new property (other properties are set)', () => {
  144. styles.setStyle( 'color:blue' );
  145. styles.removeProperty( 'color' );
  146. expect( styles.getInlineProperty( 'color' ) ).to.be.undefined;
  147. } );
  148. it( 'should remove normalized property', () => {
  149. styles.setStyle( 'margin:1px' );
  150. styles.removeProperty( 'margin-top' );
  151. expect( styles.getInlineProperty( 'margin-top' ) ).to.be.undefined;
  152. } );
  153. } );
  154. describe( 'getStyleNames()', () => {
  155. it( 'should output empty array for empty styles', () => {
  156. expect( styles.getStyleNames() ).to.deep.equal( [] );
  157. } );
  158. it( 'should output custom style names', () => {
  159. styles.setStyle( 'foo: 2;bar: baz;foo-bar-baz:none;' );
  160. expect( styles.getStyleNames() ).to.deep.equal( [ 'bar', 'foo', 'foo-bar-baz' ] );
  161. } );
  162. it( 'should output full names for known style names', () => {
  163. styles.setStyle( 'margin: 1px;margin-left: 2em;' );
  164. expect( styles.getStyleNames() ).to.deep.equal( [ 'margin' ] );
  165. } );
  166. } );
  167. } );