utils.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 {
  6. getBoxSidesShorthandValue,
  7. getBoxSidesValues,
  8. getShorthandValues,
  9. isColor,
  10. isLength,
  11. isLineStyle, isPercentage
  12. } from '../../../src/view/styles/utils';
  13. describe( 'Styles utils', () => {
  14. describe( 'isColor()', () => {
  15. it( 'returns true for #RGB color', () => {
  16. testValues( [ '#f00', '#ba2', '#F00', '#BA2', '#AbC' ], isColor );
  17. } );
  18. it( 'returns true for #RRGGBB color', () => {
  19. testValues( [ '#ff0000', '#bbaa22', '#FF0000', '#BBAA22', '#AabBCC' ], isColor );
  20. } );
  21. it( 'returns true for #RGBA color', () => {
  22. testValues( [ '#f000', '#ba24', '#F000', '#BA24', '#aBcD' ], isColor );
  23. } );
  24. it( 'returns true for #RRGGBBAA color', () => {
  25. testValues( [ '#ff000000', '#bbaa2244', '#FF000000', '#BBAA2244', '#AabBCCdd' ], isColor );
  26. } );
  27. it( 'returns false for invalid # color', () => {
  28. testValues( [ '#ttt', '#1', '#12', '#12345' ], value => !isColor( value ) );
  29. } );
  30. it( 'returns true for rgb() color', () => {
  31. testValues( [
  32. 'rgb(255,0,153)',
  33. 'rgb(255, 0, 153)',
  34. 'rgb(100%,0%,60%)',
  35. 'rgb(100%, 0%, 60%)'
  36. // Unsupported:
  37. // 'rgb(11, 22, 33, 0.1)', // rgba() is equal to rgb()
  38. // 'rgb(255, 0, 153.0)', // Floats are valid
  39. // 'rgb(255 0 153)', // CSS Level 4 notation
  40. ], isColor );
  41. } );
  42. it( 'returns false for invalid rgb() color', () => {
  43. testValues( [
  44. 'rgb()',
  45. 'rgb(1)',
  46. 'rgb(1,2)',
  47. 'rgb(11,',
  48. 'rgb(11, 22,',
  49. 'rgb(11, 22, 33',
  50. 'rgb((11, 22, 33',
  51. 'rgb((11, 22, 33)',
  52. 'rgb(11, 22, 33))',
  53. 'rgb(11, 22, 33, 0.1)',
  54. 'rgb(11, 22, 33, .153)'
  55. // Unsupported:
  56. // 'rgb(100%, 0, 60%)', // Mixed numbers and percentages - adds complexity.,
  57. ], value => !isColor( value ) );
  58. } );
  59. it( 'returns true for rgba() color', () => {
  60. testValues( [
  61. 'rgba(1,2,3,0.7)',
  62. 'rgba(12%,0,0,1)',
  63. 'rgba(255,0,153, 0.123)',
  64. 'rgba(255, 0, 153, 0.123)',
  65. 'rgba(100%,0%,60%, 0.123)',
  66. 'rgba(100%, 0%, 60%, 0.123)',
  67. 'rgba(255,0,153, 0)',
  68. 'rgba(255, 0, 153, 0)',
  69. 'rgba(100%,0%,60%, 0)',
  70. 'rgba(100%, 0%, 60%, 0)',
  71. 'rgba(255,0,153, 1)',
  72. 'rgba(255, 0, 153, 1)',
  73. 'rgba(100%,0%,60%, 1)',
  74. 'rgba(100%, 0%, 60%, 1)'
  75. ], isColor );
  76. } );
  77. it( 'returns false for wrong rgba() color', () => {
  78. testValues( [
  79. 'rgba(1,2,3,0.7',
  80. 'rgba((1,2,3,0.7',
  81. 'rgba(1,a,3,0.7)',
  82. 'rgba(1,2,3,*)'
  83. ], value => !isColor( value ) );
  84. } );
  85. it( 'returns true for hsl() color', () => {
  86. testValues( [
  87. 'hsl(270,60%,70%)',
  88. 'hsl(270, 60%, 70%)',
  89. 'hsl(270, 60%, 50%, .15)',
  90. 'hsl(270, 60%, 50%, 0.15)',
  91. 'hsl(270, 60%, 50%, 15%)'
  92. // Unsupported:
  93. // 'hsl(270deg, 60%, 70%)', // Valid deg unit
  94. // 'hsl(4.71239rad, 60%, 70%)', // Valid rad unit
  95. // 'hsl(.75turn, 60%, 70%)', // Valid turn unit
  96. // 'hsl(270 60% 70%)', // CSS Level 4 notation
  97. // 'hsl(270 60% 50% / .15)', // CSS Level 4 notation
  98. // 'hsl(270 60% 50% / 15%)' // CSS Level 4 notation
  99. ], isColor );
  100. } );
  101. it( 'returns true for hsla() color', () => {
  102. testValues( [ 'hsla(240, 100%, 50%, 1)', 'hsla(240, 100%, 50%, .05)' ], isColor );
  103. } );
  104. it( 'returns true for color keywords', () => {
  105. testValues( [ 'currentColor', 'transparent' ], isColor );
  106. } );
  107. it( 'returns true for color keyword', () => {
  108. testValues( [
  109. // CSS Level 1
  110. 'red', 'green', 'blue', // ...
  111. // CSS Level 2
  112. 'orange',
  113. // CSS Level 3
  114. 'cyan', 'azure', 'wheat',
  115. // CSS Level 4
  116. 'rebeccapurple'
  117. ], isColor );
  118. } );
  119. it( 'returns false for unknown color keyword', () => {
  120. testValues( [ 'redx', 'greenx', 'bluex' ], value => !isColor( value ) );
  121. } );
  122. } );
  123. describe( 'isLineStyle()', () => {
  124. it( 'returns true for line style', () => {
  125. testValues(
  126. [ 'none', 'hidden', 'dotted', 'dashed', 'solid', 'double', 'groove', 'ridge', 'inset', 'outset' ],
  127. isLineStyle
  128. );
  129. } );
  130. } );
  131. describe( 'isLength()', () => {
  132. it( 'returns true for various units', () => {
  133. testValues(
  134. [
  135. '1px',
  136. '1cm',
  137. '1mm',
  138. '1in',
  139. '1pc',
  140. '1pt',
  141. '1ch',
  142. '1em',
  143. '1ex',
  144. '1rem',
  145. '1vh',
  146. '1vw',
  147. '1vmin',
  148. '1vmax'
  149. ],
  150. isLength
  151. );
  152. } );
  153. it( 'returns true for various values notation', () => {
  154. testValues(
  155. [
  156. '0',
  157. '1px',
  158. '1000px',
  159. '1.1px',
  160. '345.457px',
  161. '.457px'
  162. ],
  163. isLength
  164. );
  165. } );
  166. } );
  167. describe( 'isPercentage()', () => {
  168. it( 'returns true valid values', () => {
  169. testValues( [ '1%', '100%', '1123.1312%', '0.9876%' ], isPercentage );
  170. } );
  171. it( 'returns false for not a percentage values', () => {
  172. testValues( [ '0', '1px', '1000px', '1.1px', '345.457px', '.457px' ], value => !isPercentage( value ) );
  173. } );
  174. } );
  175. describe( 'getBoxSidesShorthandValue()', () => {
  176. it( 'should output one value for same values', () => {
  177. expect( getBoxSidesShorthandValue( { top: 'foo', right: 'foo', bottom: 'foo', left: 'foo' } ) ).to.equal( 'foo' );
  178. } );
  179. it( 'should output two value for top == bottom and right == left', () => {
  180. expect( getBoxSidesShorthandValue( { top: 'foo', right: 'bar', bottom: 'foo', left: 'bar' } ) ).to.equal( 'foo bar' );
  181. } );
  182. it( 'should output three values if bottom is different then top', () => {
  183. expect( getBoxSidesShorthandValue( { top: 'foo', right: 'foo', bottom: 'bar', left: 'foo' } ) )
  184. .to.equal( 'foo foo bar' );
  185. } );
  186. it( 'should output four values if left is different then right', () => {
  187. expect( getBoxSidesShorthandValue( { top: 'foo', right: 'foo', bottom: 'foo', left: 'bar' } ) )
  188. .to.equal( 'foo foo foo bar' );
  189. } );
  190. } );
  191. describe( 'getBoxSidesValues()', () => {
  192. it( 'should parse empty string', () => {
  193. expect( getBoxSidesValues( '' ) ).to.deep.equal( {
  194. top: undefined,
  195. right: undefined,
  196. bottom: undefined,
  197. left: undefined
  198. } );
  199. } );
  200. it( 'should parse one value', () => {
  201. expect( getBoxSidesValues( 'foo' ) ).to.deep.equal( {
  202. top: 'foo',
  203. right: 'foo',
  204. bottom: 'foo',
  205. left: 'foo'
  206. } );
  207. } );
  208. it( 'should parse one value', () => {
  209. expect( getBoxSidesValues( 'foo' ) ).to.deep.equal( {
  210. top: 'foo',
  211. right: 'foo',
  212. bottom: 'foo',
  213. left: 'foo'
  214. } );
  215. } );
  216. it( 'should parse two value', () => {
  217. expect( getBoxSidesValues( 'foo bar' ) ).to.deep.equal( {
  218. top: 'foo',
  219. right: 'bar',
  220. bottom: 'foo',
  221. left: 'bar'
  222. } );
  223. } );
  224. it( 'should parse three values', () => {
  225. expect( getBoxSidesValues( 'foo foo bar' ) ).to.deep.equal( {
  226. top: 'foo',
  227. right: 'foo',
  228. bottom: 'bar',
  229. left: 'foo'
  230. } );
  231. } );
  232. it( 'should output four values if left is different then right', () => {
  233. expect( getBoxSidesValues( 'foo foo foo bar' ) ).to.deep.equal( {
  234. top: 'foo',
  235. right: 'foo',
  236. bottom: 'foo',
  237. left: 'bar'
  238. } );
  239. } );
  240. } );
  241. describe( 'getParts()', () => {
  242. it( 'should split string to separate values', () => {
  243. expect( getShorthandValues( 'foo bar' ) ).to.deep.equal( [ 'foo', 'bar' ] );
  244. } );
  245. it( 'should split string to separate values when value contain grouping parens', () => {
  246. expect( getShorthandValues( 'foo bar(1, 3, 5) url("example.com:foo/bar?q=b")' ) )
  247. .to.deep.equal( [ 'foo', 'bar(1, 3, 5)', 'url("example.com:foo/bar?q=b")' ] );
  248. } );
  249. } );
  250. function testValues( values, callback ) {
  251. values.map( string => expect( callback( string ), string ).to.be.true );
  252. }
  253. } );