| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\AtomicWidgets\CssConverter\ValueParsers; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; // Exit if accessed directly. |
| 7 |
} |
| 8 |
|
| 9 |
/** |
| 10 |
* Stateless tokenizer shared by the box shorthands and shorthand expanders. Splits a CSS value on |
| 11 |
* whitespace runs that sit at parenthesis depth 0, so function values such as calc(50% - 10px) or |
| 12 |
* rgb(0, 0, 0) stay intact as a single token. |
| 13 |
*/ |
| 14 |
class Css_Token_Splitter { |
| 15 |
/** |
| 16 |
* Split a CSS value on top-level commas (paren-aware), trimming each segment. |
| 17 |
* |
| 18 |
* @return string[] |
| 19 |
*/ |
| 20 |
public static function split_by_comma( string $value ): array { |
| 21 |
$tokens = []; |
| 22 |
$current = ''; |
| 23 |
$depth = 0; |
| 24 |
$length = strlen( $value ); |
| 25 |
|
| 26 |
for ( $i = 0; $i < $length; $i++ ) { |
| 27 |
$char = $value[ $i ]; |
| 28 |
|
| 29 |
if ( '(' === $char ) { |
| 30 |
++$depth; |
| 31 |
} elseif ( ')' === $char ) { |
| 32 |
$depth = max( 0, $depth - 1 ); |
| 33 |
} |
| 34 |
|
| 35 |
if ( 0 === $depth && ',' === $char ) { |
| 36 |
$tokens[] = trim( $current ); |
| 37 |
$current = ''; |
| 38 |
continue; |
| 39 |
} |
| 40 |
|
| 41 |
$current .= $char; |
| 42 |
} |
| 43 |
|
| 44 |
$last = trim( $current ); |
| 45 |
|
| 46 |
if ( '' !== $last ) { |
| 47 |
$tokens[] = $last; |
| 48 |
} |
| 49 |
|
| 50 |
return $tokens; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* @return string[] |
| 55 |
*/ |
| 56 |
public static function split_by_whitespace( string $value ): array { |
| 57 |
$tokens = []; |
| 58 |
$current = ''; |
| 59 |
$depth = 0; |
| 60 |
$length = strlen( $value ); |
| 61 |
|
| 62 |
for ( $i = 0; $i < $length; $i++ ) { |
| 63 |
$char = $value[ $i ]; |
| 64 |
|
| 65 |
if ( '(' === $char ) { |
| 66 |
++$depth; |
| 67 |
} elseif ( ')' === $char ) { |
| 68 |
$depth = max( 0, $depth - 1 ); |
| 69 |
} |
| 70 |
|
| 71 |
if ( 0 === $depth && ( ' ' === $char || "\t" === $char || "\n" === $char ) ) { |
| 72 |
if ( '' !== $current ) { |
| 73 |
$tokens[] = $current; |
| 74 |
$current = ''; |
| 75 |
} |
| 76 |
|
| 77 |
continue; |
| 78 |
} |
| 79 |
|
| 80 |
$current .= $char; |
| 81 |
} |
| 82 |
|
| 83 |
if ( '' !== $current ) { |
| 84 |
$tokens[] = $current; |
| 85 |
} |
| 86 |
|
| 87 |
return $tokens; |
| 88 |
} |
| 89 |
} |
| 90 |
|