← All changes
|
modules/atomic-widgets/props-resolver/transformers/styles/font-family-transformer.php
+21
-7
4.2.0-dev2
→
trunk
View file →
| @@ -9,8 +9,18 @@ | ||
| 9 | 9 | exit; // Exit if accessed directly. |
| 10 | 10 | } |
| 11 | 11 | |
| 12 | 12 | class Font_Family_Transformer extends Transformer_Base { |
| 13 | + /** | |
| 14 | + * Per the CSS spec, font-family values only need to be quoted when they contain whitespace | |
| 15 | + * or special characters. Quoting is actively harmful for: | |
| 16 | + * - CSS variable references (`var(--x)` becomes a literal string, breaking resolution) | |
| 17 | + * - Generic families (`"sans-serif"` becomes a font named "sans-serif", not the generic) | |
| 18 | + * - CSS-wide keywords (`inherit`, `initial`, `unset`) | |
| 19 | + * - Any other CSS function like `local(...)` | |
| 20 | + * | |
| 21 | + * We only wrap values that actually need it (multi-word font names like "Open Sans"). | |
| 22 | + */ | |
| 13 | 23 | public function transform( $value, Props_Resolver_Context $context ) { |
| 14 | 24 | if ( ! is_string( $value ) ) { |
| 15 | 25 | return null; |
| 16 | 26 | } |
| @@ -16,16 +26,20 @@ | ||
| 16 | 26 | } |
| 17 | 27 | |
| 18 | 28 | $trimmed = trim( $value ); |
| 19 | 29 | |
| 20 | - $is_quoted = ( | |
| 21 | - ( str_starts_with( $trimmed, '"' ) && str_ends_with( $trimmed, '"' ) ) || | |
| 22 | - ( str_starts_with( $trimmed, "'" ) && str_ends_with( $trimmed, "'" ) ) | |
| 23 | - ); | |
| 24 | - | |
| 25 | - if ( $is_quoted ) { | |
| 30 | + if ( '' === $trimmed || $this->is_already_quoted( $trimmed ) ) { | |
| 26 | 31 | return $trimmed; |
| 27 | 32 | } |
| 28 | 33 | |
| 29 | - return '"' . $trimmed . '"'; | |
| 34 | + // Only multi-word font names (e.g. `Open Sans`) need to be wrapped in quotes. | |
| 35 | + // Single-token values like `var(--x)`, `sans-serif` or `inherit` must stay unquoted to remain valid CSS. | |
| 36 | + return preg_match( '/\s/', $trimmed ) | |
| 37 | + ? '"' . $trimmed . '"' | |
| 38 | + : $trimmed; | |
| 39 | + } | |
| 40 | + | |
| 41 | + private function is_already_quoted( string $value ): bool { | |
| 42 | + return ( str_starts_with( $value, '"' ) && str_ends_with( $value, '"' ) ) | |
| 43 | + || ( str_starts_with( $value, "'" ) && str_ends_with( $value, "'" ) ); | |
| 30 | 44 | } |
| 31 | 45 | } |