utils.js 4.6 KB

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