utils.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. getShorthandValues,
  7. getBoxSidesShorthandValue,
  8. getBoxSidesValues,
  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(255, 0, 153.0)', // TODO: does not validate but might be skipped
  35. 'rgb(100%,0%,60%)',
  36. 'rgb(100%, 0%, 60%)',
  37. 'rgb(255 0 153)' // TODO: might be skipped - adds complexity
  38. ], isColor );
  39. } );
  40. it( 'returns false for invalid rgb() color', () => {
  41. testValues( [
  42. 'rgb()',
  43. 'rgb(1)',
  44. 'rgb(1,2)',
  45. 'rgb(11,',
  46. 'rgb(11, 22,',
  47. 'rgb(11, 22, 33',
  48. 'rgb((11, 22, 33',
  49. 'rgb((11, 22, 33)',
  50. 'rgb((11, 22, 33, 44)', // TODO: valid in Level 4 CSS
  51. 'rgb(11, 22, 33))',
  52. 'rgb(100%, 0, 60%)' // Don't mix numbers and percentages. TODO: might be skipped - adds complexity.
  53. ], value => !isColor( value ) );
  54. } );
  55. it( 'returns true for rgba() color', () => {
  56. testValues( [ 'rgba(1,2,3,0.7)', 'rgba(12%,0,0,1)' ], isColor );
  57. } );
  58. it( 'returns true for hsl() color', () => {
  59. testValues( [ 'hsl(0, 100%, 50%)', 'hsl(340,80%,40%)' ], isColor );
  60. } );
  61. it( 'returns true for hsla() color', () => {
  62. testValues( [ 'hsla(240, 100%, 50%, 1)', 'hsla(240, 100%, 50%, .05)' ], isColor );
  63. } );
  64. it( 'returns true for currentColor color', () => {
  65. testValues( [ 'currentColor' ], isColor );
  66. } );
  67. } );
  68. describe( 'isLineStyle()', () => {
  69. it( 'returns true for line style', () => {
  70. testValues(
  71. [ 'none', 'hidden', 'dotted', 'dashed', 'solid', 'double', 'groove', 'ridge', 'inset', 'outset' ],
  72. isLineStyle
  73. );
  74. } );
  75. } );
  76. describe( 'isLength()', () => {
  77. it( 'returns true for various units', () => {
  78. testValues(
  79. [ '1px', '2rem', '34.5px', '.2em', '0', '1346vmax' ],
  80. isLength
  81. );
  82. } );
  83. } );
  84. describe( 'getBoxSidesShorthandValue()', () => {
  85. it( 'should output one value for same values', () => {
  86. expect( getBoxSidesShorthandValue( { top: 'foo', right: 'foo', bottom: 'foo', left: 'foo' } ) ).to.equal( 'foo' );
  87. } );
  88. it( 'should output two value for top == bottom and right == left', () => {
  89. expect( getBoxSidesShorthandValue( { top: 'foo', right: 'bar', bottom: 'foo', left: 'bar' } ) ).to.equal( 'foo bar' );
  90. } );
  91. it( 'should output three values if bottom is different then top', () => {
  92. expect( getBoxSidesShorthandValue( { top: 'foo', right: 'foo', bottom: 'bar', left: 'foo' } ) )
  93. .to.equal( 'foo foo bar' );
  94. } );
  95. it( 'should output four values if left is different then right', () => {
  96. expect( getBoxSidesShorthandValue( { top: 'foo', right: 'foo', bottom: 'foo', left: 'bar' } ) )
  97. .to.equal( 'foo foo foo bar' );
  98. } );
  99. } );
  100. describe( 'getBoxSidesValues()', () => {
  101. it( 'should parse empty string', () => {
  102. expect( getBoxSidesValues( '' ) ).to.deep.equal( {
  103. top: undefined,
  104. right: undefined,
  105. bottom: undefined,
  106. left: undefined
  107. } );
  108. } );
  109. it( 'should parse one value', () => {
  110. expect( getBoxSidesValues( 'foo' ) ).to.deep.equal( {
  111. top: 'foo',
  112. right: 'foo',
  113. bottom: 'foo',
  114. left: 'foo'
  115. } );
  116. } );
  117. it( 'should parse one value', () => {
  118. expect( getBoxSidesValues( 'foo' ) ).to.deep.equal( {
  119. top: 'foo',
  120. right: 'foo',
  121. bottom: 'foo',
  122. left: 'foo'
  123. } );
  124. } );
  125. it( 'should parse two value', () => {
  126. expect( getBoxSidesValues( 'foo bar' ) ).to.deep.equal( {
  127. top: 'foo',
  128. right: 'bar',
  129. bottom: 'foo',
  130. left: 'bar'
  131. } );
  132. } );
  133. it( 'should parse three values', () => {
  134. expect( getBoxSidesValues( 'foo foo bar' ) ).to.deep.equal( {
  135. top: 'foo',
  136. right: 'foo',
  137. bottom: 'bar',
  138. left: 'foo'
  139. } );
  140. } );
  141. it( 'should output four values if left is different then right', () => {
  142. expect( getBoxSidesValues( 'foo foo foo bar' ) ).to.deep.equal( {
  143. top: 'foo',
  144. right: 'foo',
  145. bottom: 'foo',
  146. left: 'bar'
  147. } );
  148. } );
  149. } );
  150. describe( 'getParts()', () => {
  151. it( 'should split string to separate values', () => {
  152. expect( getShorthandValues( 'foo bar' ) ).to.deep.equal( [ 'foo', 'bar' ] );
  153. } );
  154. it( 'should split string to separate values when value contain grouping parens', () => {
  155. expect( getShorthandValues( 'foo bar(1, 3, 5) url("example.com:foo/bar?q=b")' ) )
  156. .to.deep.equal( [ 'foo', 'bar(1, 3, 5)', 'url("example.com:foo/bar?q=b")' ] );
  157. } );
  158. } );
  159. function testValues( values, callback ) {
  160. values.map( string => expect( callback( string ), string ).to.be.true );
  161. }
  162. } );