stylesmap.js 8.5 KB

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