utils.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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
  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. TODO: might be skipped - adds complexity.,
  57. ], value => !isColor( value ) );
  58. } );
  59. it( 'returns true for rgba() color', () => {
  60. testValues( [ 'rgba(1,2,3,0.7)', 'rgba(12%,0,0,1)' ], isColor );
  61. } );
  62. it( 'returns true for hsl() color', () => {
  63. testValues( [
  64. 'hsl(270,60%,70%)',
  65. 'hsl(270, 60%, 70%)',
  66. 'hsl(270, 60%, 50%, .15)',
  67. 'hsl(270, 60%, 50%, 0.15)',
  68. 'hsl(270, 60%, 50%, 15%)'
  69. // Unsupported:
  70. // 'hsl(270deg, 60%, 70%)', // Valid deg unit
  71. // 'hsl(4.71239rad, 60%, 70%)', // Valid rad unit
  72. // 'hsl(.75turn, 60%, 70%)', // Valid turn unit
  73. // 'hsl(270 60% 70%)', // CSS Level 4 notation
  74. // 'hsl(270 60% 50% / .15)', // CSS Level 4 notation
  75. // 'hsl(270 60% 50% / 15%)' // CSS Level 4 notation
  76. ], isColor );
  77. } );
  78. it( 'returns true for hsla() color', () => {
  79. testValues( [ 'hsla(240, 100%, 50%, 1)', 'hsla(240, 100%, 50%, .05)' ], isColor );
  80. } );
  81. it( 'returns true for color keywords', () => {
  82. testValues( [ 'currentColor', 'transparent' ], isColor );
  83. } );
  84. it( 'returns true for color keyword', () => {
  85. testValues( [
  86. // CSS Level 1
  87. 'red', 'green', 'blue', // ...
  88. // CSS Level 2
  89. 'orange',
  90. // CSS Level 3
  91. 'cyan', 'azure', 'wheat',
  92. // CSS Level 4
  93. 'rebeccapurple'
  94. ], isColor );
  95. } );
  96. it( 'returns false for unknown color keyword', () => {
  97. testValues( [ 'redx', 'greenx', 'bluex' ], value => !isColor( value ) );
  98. } );
  99. } );
  100. describe( 'isLineStyle()', () => {
  101. it( 'returns true for line style', () => {
  102. testValues(
  103. [ 'none', 'hidden', 'dotted', 'dashed', 'solid', 'double', 'groove', 'ridge', 'inset', 'outset' ],
  104. isLineStyle
  105. );
  106. } );
  107. } );
  108. describe( 'isLength()', () => {
  109. it( 'returns true for various units', () => {
  110. testValues(
  111. [
  112. '1px',
  113. '1cm',
  114. '1mm',
  115. '1in',
  116. '1pc',
  117. '1pt',
  118. '1ch',
  119. '1em',
  120. '1ex',
  121. '1rem',
  122. '1vh',
  123. '1vw',
  124. '1vmin',
  125. '1vmax'
  126. ],
  127. isLength
  128. );
  129. } );
  130. it( 'returns true for various values notation', () => {
  131. testValues(
  132. [
  133. '0',
  134. '1px',
  135. '1000px',
  136. '1.1px',
  137. '345.457px',
  138. '.457px'
  139. ],
  140. isLength
  141. );
  142. } );
  143. } );
  144. describe( 'getBoxSidesShorthandValue()', () => {
  145. it( 'should output one value for same values', () => {
  146. expect( getBoxSidesShorthandValue( { top: 'foo', right: 'foo', bottom: 'foo', left: 'foo' } ) ).to.equal( 'foo' );
  147. } );
  148. it( 'should output two value for top == bottom and right == left', () => {
  149. expect( getBoxSidesShorthandValue( { top: 'foo', right: 'bar', bottom: 'foo', left: 'bar' } ) ).to.equal( 'foo bar' );
  150. } );
  151. it( 'should output three values if bottom is different then top', () => {
  152. expect( getBoxSidesShorthandValue( { top: 'foo', right: 'foo', bottom: 'bar', left: 'foo' } ) )
  153. .to.equal( 'foo foo bar' );
  154. } );
  155. it( 'should output four values if left is different then right', () => {
  156. expect( getBoxSidesShorthandValue( { top: 'foo', right: 'foo', bottom: 'foo', left: 'bar' } ) )
  157. .to.equal( 'foo foo foo bar' );
  158. } );
  159. } );
  160. describe( 'getBoxSidesValues()', () => {
  161. it( 'should parse empty string', () => {
  162. expect( getBoxSidesValues( '' ) ).to.deep.equal( {
  163. top: undefined,
  164. right: undefined,
  165. bottom: undefined,
  166. left: undefined
  167. } );
  168. } );
  169. it( 'should parse one value', () => {
  170. expect( getBoxSidesValues( 'foo' ) ).to.deep.equal( {
  171. top: 'foo',
  172. right: 'foo',
  173. bottom: 'foo',
  174. left: 'foo'
  175. } );
  176. } );
  177. it( 'should parse one value', () => {
  178. expect( getBoxSidesValues( 'foo' ) ).to.deep.equal( {
  179. top: 'foo',
  180. right: 'foo',
  181. bottom: 'foo',
  182. left: 'foo'
  183. } );
  184. } );
  185. it( 'should parse two value', () => {
  186. expect( getBoxSidesValues( 'foo bar' ) ).to.deep.equal( {
  187. top: 'foo',
  188. right: 'bar',
  189. bottom: 'foo',
  190. left: 'bar'
  191. } );
  192. } );
  193. it( 'should parse three values', () => {
  194. expect( getBoxSidesValues( 'foo foo bar' ) ).to.deep.equal( {
  195. top: 'foo',
  196. right: 'foo',
  197. bottom: 'bar',
  198. left: 'foo'
  199. } );
  200. } );
  201. it( 'should output four values if left is different then right', () => {
  202. expect( getBoxSidesValues( 'foo foo foo bar' ) ).to.deep.equal( {
  203. top: 'foo',
  204. right: 'foo',
  205. bottom: 'foo',
  206. left: 'bar'
  207. } );
  208. } );
  209. } );
  210. describe( 'getParts()', () => {
  211. it( 'should split string to separate values', () => {
  212. expect( getShorthandValues( 'foo bar' ) ).to.deep.equal( [ 'foo', 'bar' ] );
  213. } );
  214. it( 'should split string to separate values when value contain grouping parens', () => {
  215. expect( getShorthandValues( 'foo bar(1, 3, 5) url("example.com:foo/bar?q=b")' ) )
  216. .to.deep.equal( [ 'foo', 'bar(1, 3, 5)', 'url("example.com:foo/bar?q=b")' ] );
  217. } );
  218. } );
  219. function testValues( values, callback ) {
  220. values.map( string => expect( callback( string ), string ).to.be.true );
  221. }
  222. } );