| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Mcp\Abilities\Appliers\V3\Mapper; |
| 4 |
|
| 5 |
use Elementor\Modules\Mcp\Abilities\Utils\Style_Variants_Merger; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* Extracts `property: value` declarations from a CSS block and normalizes selector |
| 13 |
* fragments into V3 pseudo-states (hover|focus|active or null). |
| 14 |
*/ |
| 15 |
class Css_Declaration_Parser { |
| 16 |
|
| 17 |
/** |
| 18 |
* @return array<int, array{property: string, value: string}> |
| 19 |
*/ |
| 20 |
public function parse_declarations( string $css ): array { |
| 21 |
$rules = []; |
| 22 |
|
| 23 |
foreach ( explode( ';', $css ) as $declaration ) { |
| 24 |
$declaration = trim( $declaration ); |
| 25 |
$separator = strpos( $declaration, ':' ); |
| 26 |
|
| 27 |
if ( false === $separator ) { |
| 28 |
continue; |
| 29 |
} |
| 30 |
|
| 31 |
$property = strtolower( trim( substr( $declaration, 0, $separator ) ) ); |
| 32 |
$value = trim( substr( $declaration, $separator + 1 ) ); |
| 33 |
|
| 34 |
if ( '' === $property || '' === $value || 'null' === $value ) { |
| 35 |
continue; |
| 36 |
} |
| 37 |
|
| 38 |
$rules[] = [ |
| 39 |
'property' => $property, |
| 40 |
'value' => $value, |
| 41 |
]; |
| 42 |
} |
| 43 |
|
| 44 |
return $rules; |
| 45 |
} |
| 46 |
|
| 47 |
public function normalize_state( ?string $selector ): ?string { |
| 48 |
if ( null === $selector || '' === $selector ) { |
| 49 |
return null; |
| 50 |
} |
| 51 |
|
| 52 |
$state = strtolower( ltrim( $selector, ':' ) ); |
| 53 |
|
| 54 |
return in_array( $state, Style_Variants_Merger::PSEUDO_STATES, true ) ? $state : null; |
| 55 |
} |
| 56 |
} |
| 57 |
|