stylesmap.js 9.1 KB

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