utils.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 currentColor color', () => {
  82. testValues( [ 'currentColor' ], isColor );
  83. } );
  84. } );
  85. describe( 'isLineStyle()', () => {
  86. it( 'returns true for line style', () => {
  87. testValues(
  88. [ 'none', 'hidden', 'dotted', 'dashed', 'solid', 'double', 'groove', 'ridge', 'inset', 'outset' ],
  89. isLineStyle
  90. );
  91. } );
  92. } );
  93. describe( 'isLength()', () => {
  94. it( 'returns true for various units', () => {
  95. testValues(
  96. [ '1px', '2rem', '34.5px', '.2em', '0', '1346vmax' ],
  97. isLength
  98. );
  99. } );
  100. } );
  101. describe( 'getBoxSidesShorthandValue()', () => {
  102. it( 'should output one value for same values', () => {
  103. expect( getBoxSidesShorthandValue( { top: 'foo', right: 'foo', bottom: 'foo', left: 'foo' } ) ).to.equal( 'foo' );
  104. } );
  105. it( 'should output two value for top == bottom and right == left', () => {
  106. expect( getBoxSidesShorthandValue( { top: 'foo', right: 'bar', bottom: 'foo', left: 'bar' } ) ).to.equal( 'foo bar' );
  107. } );
  108. it( 'should output three values if bottom is different then top', () => {
  109. expect( getBoxSidesShorthandValue( { top: 'foo', right: 'foo', bottom: 'bar', left: 'foo' } ) )
  110. .to.equal( 'foo foo bar' );
  111. } );
  112. it( 'should output four values if left is different then right', () => {
  113. expect( getBoxSidesShorthandValue( { top: 'foo', right: 'foo', bottom: 'foo', left: 'bar' } ) )
  114. .to.equal( 'foo foo foo bar' );
  115. } );
  116. } );
  117. describe( 'getBoxSidesValues()', () => {
  118. it( 'should parse empty string', () => {
  119. expect( getBoxSidesValues( '' ) ).to.deep.equal( {
  120. top: undefined,
  121. right: undefined,
  122. bottom: undefined,
  123. left: undefined
  124. } );
  125. } );
  126. it( 'should parse one value', () => {
  127. expect( getBoxSidesValues( 'foo' ) ).to.deep.equal( {
  128. top: 'foo',
  129. right: 'foo',
  130. bottom: 'foo',
  131. left: 'foo'
  132. } );
  133. } );
  134. it( 'should parse one value', () => {
  135. expect( getBoxSidesValues( 'foo' ) ).to.deep.equal( {
  136. top: 'foo',
  137. right: 'foo',
  138. bottom: 'foo',
  139. left: 'foo'
  140. } );
  141. } );
  142. it( 'should parse two value', () => {
  143. expect( getBoxSidesValues( 'foo bar' ) ).to.deep.equal( {
  144. top: 'foo',
  145. right: 'bar',
  146. bottom: 'foo',
  147. left: 'bar'
  148. } );
  149. } );
  150. it( 'should parse three values', () => {
  151. expect( getBoxSidesValues( 'foo foo bar' ) ).to.deep.equal( {
  152. top: 'foo',
  153. right: 'foo',
  154. bottom: 'bar',
  155. left: 'foo'
  156. } );
  157. } );
  158. it( 'should output four values if left is different then right', () => {
  159. expect( getBoxSidesValues( 'foo foo foo bar' ) ).to.deep.equal( {
  160. top: 'foo',
  161. right: 'foo',
  162. bottom: 'foo',
  163. left: 'bar'
  164. } );
  165. } );
  166. } );
  167. describe( 'getParts()', () => {
  168. it( 'should split string to separate values', () => {
  169. expect( getShorthandValues( 'foo bar' ) ).to.deep.equal( [ 'foo', 'bar' ] );
  170. } );
  171. it( 'should split string to separate values when value contain grouping parens', () => {
  172. expect( getShorthandValues( 'foo bar(1, 3, 5) url("example.com:foo/bar?q=b")' ) )
  173. .to.deep.equal( [ 'foo', 'bar(1, 3, 5)', 'url("example.com:foo/bar?q=b")' ] );
  174. } );
  175. } );
  176. function testValues( values, callback ) {
  177. values.map( string => expect( callback( string ), string ).to.be.true );
  178. }
  179. } );