| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\AtomicWidgets\CssConverter\ValueParsers; |
| 4 |
|
| 5 |
use Elementor\Modules\AtomicWidgets\Styles\Size_Constants; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; // Exit if accessed directly. |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* Stateless leaf parser: a raw CSS length-ish value -> the {size, unit} leaf a Size_Prop_Type accepts, |
| 13 |
* or null to decline (the caller then routes the declaration to custom_css). It never touches the |
| 14 |
* registry, context, or PropTypes. |
| 15 |
* |
| 16 |
* - "auto" -> { size: null, unit: 'auto' } |
| 17 |
* - calc()/clamp()/min()/max()/var()/env() -> { size: '<raw>', unit: 'custom' } (kept verbatim) |
| 18 |
* - "<number><unit>" (unit in all_supported_units) -> { size: <number>, unit: <unit> } |
| 19 |
* - unitless "0" -> { size: 0, unit: 'px' } |
| 20 |
* - unitless non-zero, $allow_unitless on -> { size: '<raw>', unit: 'custom' } (e.g. line-height: 1.1) |
| 21 |
* - anything else (unitless non-zero with $allow_unitless off, unknown unit, multi-value) -> null |
| 22 |
*/ |
| 23 |
class Size_Value_Parser { |
| 24 |
const NUMBER_WITH_UNIT_PATTERN = '/^(-?\d*\.?\d+)([a-z%]*)$/i'; |
| 25 |
const DYNAMIC_FUNCTION_PATTERN = '/(?:calc|clamp|min|max|var|env)\(/i'; |
| 26 |
|
| 27 |
/** |
| 28 |
* @param string $value The raw CSS value. |
| 29 |
* @param bool $allow_unitless When true, a unitless non-zero number (e.g. a line-height multiplier) |
| 30 |
* is kept verbatim as a `custom` unit instead of declining. |
| 31 |
* |
| 32 |
* @return array{size: mixed, unit: string}|null |
| 33 |
*/ |
| 34 |
public static function parse( string $value, bool $allow_unitless = false ): ?array { |
| 35 |
$value = trim( $value ); |
| 36 |
|
| 37 |
if ( '' === $value ) { |
| 38 |
return null; |
| 39 |
} |
| 40 |
|
| 41 |
if ( Size_Constants::UNIT_AUTO === strtolower( $value ) ) { |
| 42 |
return [ |
| 43 |
'size' => null, |
| 44 |
'unit' => Size_Constants::UNIT_AUTO, |
| 45 |
]; |
| 46 |
} |
| 47 |
|
| 48 |
if ( self::is_dynamic_value( $value ) ) { |
| 49 |
return [ |
| 50 |
'size' => $value, |
| 51 |
'unit' => Size_Constants::UNIT_CUSTOM, |
| 52 |
]; |
| 53 |
} |
| 54 |
|
| 55 |
return self::parse_number_with_unit( $value, $allow_unitless ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* @return array{size: mixed, unit: string}|null |
| 60 |
*/ |
| 61 |
private static function parse_number_with_unit( string $value, bool $allow_unitless ): ?array { |
| 62 |
if ( ! preg_match( self::NUMBER_WITH_UNIT_PATTERN, $value, $matches ) ) { |
| 63 |
return null; |
| 64 |
} |
| 65 |
|
| 66 |
$size = $matches[1] + 0; |
| 67 |
$unit = strtolower( $matches[2] ); |
| 68 |
|
| 69 |
if ( '' === $unit ) { |
| 70 |
if ( 0.0 === (float) $size ) { |
| 71 |
return [ |
| 72 |
'size' => $size, |
| 73 |
'unit' => Size_Constants::DEFAULT_UNIT, |
| 74 |
]; |
| 75 |
} |
| 76 |
|
| 77 |
return $allow_unitless |
| 78 |
? [ |
| 79 |
'size' => $matches[1], |
| 80 |
'unit' => Size_Constants::UNIT_CUSTOM, |
| 81 |
] |
| 82 |
: null; |
| 83 |
} |
| 84 |
|
| 85 |
if ( ! in_array( $unit, Size_Constants::all_supported_units(), true ) ) { |
| 86 |
return null; |
| 87 |
} |
| 88 |
|
| 89 |
return [ |
| 90 |
'size' => $size, |
| 91 |
'unit' => $unit, |
| 92 |
]; |
| 93 |
} |
| 94 |
|
| 95 |
private static function is_dynamic_value( string $value ): bool { |
| 96 |
return 1 === preg_match( self::DYNAMIC_FUNCTION_PATTERN, $value ); |
| 97 |
} |
| 98 |
} |
| 99 |
|