utils.js 4.7 KB

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