styles.js 7.7 KB

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