8
0

utils.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module engine/view/styles/utils
  7. */
  8. export function isColor( string ) {
  9. return /(^[#0-9A-Fa-f]{3,9}$|rgba?\(|hsla?\(|^currentColor$)/.test( string );
  10. }
  11. export function isLineStyle( string ) {
  12. return /^(none|hidden|dotted|dashed|solid|double|groove|ridge|inset|outset)$/.test( string );
  13. }
  14. export function isLength( string ) {
  15. return /^[+-]?[0-9]?[.]?[0-9]+([a-z]+|%)$/.test( string );
  16. }
  17. export function isRepeat( string ) {
  18. return /^(repeat-x|repeat-y|repeat|space|round|no-repeat)$/.test( string );
  19. }
  20. export function isPosition( string ) {
  21. return /^(center|top|bottom|left|right)$/.test( string );
  22. }
  23. export function isAttachment( string ) {
  24. return /^(fixed|scroll|local)$/.test( string );
  25. }
  26. export function isURL( string ) {
  27. return /^url\(/.test( string );
  28. }
  29. export function getTopRightBottomLeftValues( value = '' ) {
  30. if ( value === '' ) {
  31. return { top: undefined, right: undefined, bottom: undefined, left: undefined };
  32. }
  33. const values = getParts( value );
  34. const top = values[ 0 ];
  35. const bottom = values[ 2 ] || top;
  36. const right = values[ 1 ] || top;
  37. const left = values[ 3 ] || right;
  38. return { top, bottom, right, left };
  39. }
  40. export function getTopRightBottomLeftValueReducer( styleShorthand ) {
  41. return ( evt, data ) => {
  42. const { top, right, bottom, left } = ( data.value || {} );
  43. const reduced = [];
  44. if ( ![ top, right, left, bottom ].every( value => !!value ) ) {
  45. if ( top ) {
  46. reduced.push( [ styleShorthand + '-top', top ] );
  47. }
  48. if ( right ) {
  49. reduced.push( [ styleShorthand + '-right', right ] );
  50. }
  51. if ( bottom ) {
  52. reduced.push( [ styleShorthand + '-bottom', bottom ] );
  53. }
  54. if ( left ) {
  55. reduced.push( [ styleShorthand + '-left', left ] );
  56. }
  57. } else {
  58. reduced.push( [ styleShorthand, getTopRightBottomLeftShorthandValue( data.value ) ] );
  59. }
  60. data.reduced = reduced;
  61. };
  62. }
  63. export function getTopRightBottomLeftShorthandValue( { left, right, top, bottom } ) {
  64. const out = [];
  65. if ( left !== right ) {
  66. out.push( top, right, bottom, left );
  67. } else if ( bottom !== top ) {
  68. out.push( top, right, bottom );
  69. } else if ( right !== top ) {
  70. out.push( top, right );
  71. } else {
  72. out.push( top );
  73. }
  74. return out.join( ' ' );
  75. }
  76. export function getPositionShorthandNormalizer( longhand ) {
  77. return ( evt, data ) => {
  78. data.path = longhand;
  79. data.value = getTopRightBottomLeftValues( data.value );
  80. };
  81. }
  82. export function getParts( string ) {
  83. return string.replace( /, /g, ',' ).split( ' ' ).map( string => string.replace( /,/g, ', ' ) );
  84. }