stylesmap.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 StylesMap, { StylesProcessor } from '../../src/view/stylesmap';
  6. import encodedImage from './_utils/encodedimage.txt';
  7. import { addPaddingStylesProcessor } from '../../src/view/styles/paddingstyles';
  8. describe( 'StylesMap', () => {
  9. let styles, converter;
  10. beforeEach( () => {
  11. converter = new StylesProcessor();
  12. // Define simple "foo" shorthand normalizers, similar to the "margin" shorthand normalizers, for testing purposes.
  13. converter.setNormalizer( 'foo', value => ( {
  14. path: 'foo',
  15. value: { top: value, right: value, bottom: value, left: value }
  16. } ) );
  17. converter.setNormalizer( 'foo-top', value => ( {
  18. path: 'foo',
  19. value: { top: value }
  20. } ) );
  21. addPaddingStylesProcessor( converter );
  22. styles = new StylesMap( 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.setTo( 'color:blue' );
  30. expect( styles.size ).to.equal( 1 );
  31. styles.setTo( 'margin:1px;' );
  32. expect( styles.size ).to.equal( 1 );
  33. styles.setTo( 'margin-top:1px;margin-bottom:1px;' );
  34. expect( styles.size ).to.equal( 2 );
  35. } );
  36. } );
  37. describe( 'setTo()', () => {
  38. it( 'should reset styles to a new value', () => {
  39. styles.setTo( 'color:red;margin:1px;' );
  40. expect( styles.getNormalized() ).to.deep.equal( { color: 'red', margin: '1px' } );
  41. styles.setTo( '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.setTo( '' );
  47. expect( styles.getStyleNames() ).to.deep.equal( [] );
  48. } );
  49. it( 'should be able to parse big styles definition', () => {
  50. expect( () => {
  51. styles.setTo( `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.setTo( 'background-image:url("im;color:g.jpg")' );
  56. expect( styles.getInlineProperty( 'background-image' ) ).to.equal( 'url("im;color:g.jpg")' );
  57. styles.setTo( '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.setTo( '\ncolor:\n red ' );
  62. expect( styles.getInlineProperty( 'color' ) ).to.equal( 'red' );
  63. } );
  64. it( 'should not be confused by duplicated semicolon', () => {
  65. styles.setTo( '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.setTo( '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.setTo( '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.setTo( '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.setTo( '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.setTo( 'margin-top:1px' );
  100. expect( styles.getInlineProperty( 'margin' ) ).to.be.undefined;
  101. } );
  102. } );
  103. describe( 'hasProperty()', () => {
  104. it( 'should return false if normalized property is not set', () => {
  105. expect( styles.hasProperty( 'bar' ) ).to.be.false;
  106. } );
  107. it( 'should return false if normalized property is not set', () => {
  108. expect( styles.hasProperty( 'foo' ) ).to.be.false;
  109. } );
  110. it( 'should return false if normalized property is not set', () => {
  111. styles.setTo( 'foo-top:1px' );
  112. expect( styles.hasProperty( 'foo' ) ).to.be.true;
  113. } );
  114. it( 'should return true if property is set', () => {
  115. styles.setTo( 'bar:deeppink' );
  116. expect( styles.hasProperty( 'bar' ) ).to.be.true;
  117. } );
  118. it( 'should return true if normalized shorthanded property is set', () => {
  119. styles.setTo( 'foo:1px' );
  120. expect( styles.hasProperty( 'foo' ) ).to.be.true;
  121. expect( styles.hasProperty( 'foo-top' ) ).to.be.true;
  122. } );
  123. } );
  124. describe( 'insertProperty()', () => {
  125. it( 'should insert new property (empty styles)', () => {
  126. styles.insertProperty( 'color', 'blue' );
  127. expect( styles.getInlineProperty( 'color' ) ).to.equal( 'blue' );
  128. } );
  129. it( 'should insert new property (other properties are set)', () => {
  130. styles.setTo( 'margin: 1px;' );
  131. styles.insertProperty( 'color', 'blue' );
  132. expect( styles.getInlineProperty( 'color' ) ).to.equal( 'blue' );
  133. } );
  134. it( 'should overwrite property', () => {
  135. styles.setTo( 'color: red;' );
  136. styles.insertProperty( 'color', 'blue' );
  137. expect( styles.getInlineProperty( 'color' ) ).to.equal( 'blue' );
  138. } );
  139. it( 'should set multiple styles by providing an object', () => {
  140. styles.setTo( 'color: red;' );
  141. styles.insertProperty( { color: 'blue', foo: '1px' } );
  142. expect( styles.getInlineProperty( 'color' ) ).to.equal( 'blue' );
  143. expect( styles.getInlineProperty( 'foo-top' ) ).to.equal( '1px' );
  144. } );
  145. it( 'should set object property', () => {
  146. styles.setTo( 'foo:1px;' );
  147. styles.insertProperty( 'foo', { right: '2px' } );
  148. expect( styles.getInlineProperty( 'foo-left' ) ).to.equal( '1px' );
  149. expect( styles.getInlineProperty( 'foo-right' ) ).to.equal( '2px' );
  150. } );
  151. } );
  152. describe( 'removeProperty()', () => {
  153. it( 'should do nothing if property is not set', () => {
  154. styles.removeProperty( 'color' );
  155. expect( styles.getInlineProperty( 'color' ) ).to.be.undefined;
  156. } );
  157. it( 'should insert new property (other properties are set)', () => {
  158. styles.setTo( 'color:blue' );
  159. styles.removeProperty( 'color' );
  160. expect( styles.getInlineProperty( 'color' ) ).to.be.undefined;
  161. } );
  162. it( 'should remove normalized property', () => {
  163. styles.setTo( 'margin:1px' );
  164. styles.removeProperty( 'margin-top' );
  165. expect( styles.getInlineProperty( 'margin-top' ) ).to.be.undefined;
  166. } );
  167. } );
  168. describe( 'getStyleNames()', () => {
  169. it( 'should output empty array for empty styles', () => {
  170. expect( styles.getStyleNames() ).to.deep.equal( [] );
  171. } );
  172. it( 'should output custom style names', () => {
  173. styles.setTo( 'foo: 2;bar: baz;foo-bar-baz:none;' );
  174. expect( styles.getStyleNames() ).to.deep.equal( [ 'foo', 'bar', 'foo-bar-baz' ] );
  175. } );
  176. it( 'should output full names for known style names', () => {
  177. styles.setTo( 'foo: 1px;foo-top: 2em;' );
  178. expect( styles.getStyleNames() ).to.deep.equal( [ 'foo' ] );
  179. } );
  180. } );
  181. } );