| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\AtomicWidgets\CssConverter; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
trait Css_Block_Scanner_Trait { |
| 10 |
|
| 11 |
/** |
| 12 |
* Returns true when the quote/character at $pos is preceded by an odd number of backslashes, |
| 13 |
* meaning it is escaped. Counting consecutive backslashes handles `"\\"` correctly |
| 14 |
* (even count → backslash itself escaped → the following char is NOT escaped). |
| 15 |
*/ |
| 16 |
private function is_escaped( string $css, int $pos ): bool { |
| 17 |
$count = 0; |
| 18 |
$j = $pos - 1; |
| 19 |
while ( $j >= 0 && '\\' === $css[ $j ] ) { |
| 20 |
++$count; |
| 21 |
--$j; |
| 22 |
} |
| 23 |
return 1 === $count % 2; |
| 24 |
} |
| 25 |
|
| 26 |
private function find_block_end( string $css, int $start, int $len ): ?int { |
| 27 |
$depth = 1; |
| 28 |
$in_string = false; |
| 29 |
$str_char = ''; |
| 30 |
|
| 31 |
for ( $i = $start; $i < $len; $i++ ) { |
| 32 |
$c = $css[ $i ]; |
| 33 |
|
| 34 |
if ( $in_string ) { |
| 35 |
if ( $str_char === $c && ! $this->is_escaped( $css, $i ) ) { |
| 36 |
$in_string = false; |
| 37 |
} |
| 38 |
continue; |
| 39 |
} |
| 40 |
|
| 41 |
if ( '"' === $c || "'" === $c ) { |
| 42 |
$in_string = true; |
| 43 |
$str_char = $c; |
| 44 |
continue; |
| 45 |
} |
| 46 |
|
| 47 |
if ( '{' === $c ) { |
| 48 |
++$depth; |
| 49 |
} elseif ( '}' === $c ) { |
| 50 |
--$depth; |
| 51 |
if ( 0 === $depth ) { |
| 52 |
return $i + 1; |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
return null; |
| 58 |
} |
| 59 |
} |
| 60 |
|