value pairs, respecting CSS structure. * * Splitting on ';' and ':' is wrong and silently corrupts real stylesheets: * * background: url("data:image/svg+xml;base64,PHN2Zz...") -- ';' inside a quoted URL * grid-template-areas: "a b" "c d" -- ':' never appears again * font-family: "Foo; Bar", serif -- ';' inside a string * background: url(data:image/png;base64,iVBOR) -- ';' inside url() unquoted * --custom: { a: 1; b: 2 } -- braces in a custom property * * This scans character by character, tracking quote state, parenthesis depth and brace * depth, and only treats ';' as a separator and the FIRST ':' as the property/value split * when both are at depth zero and outside any string. * * @return array property => value, in source order; later wins on repeats. */ public static function split_declarations( string $css ): array { $declarations = []; $buffer = ''; $quote = ''; $paren = 0; $brace = 0; $length = strlen( $css ); $flush = static function ( string $chunk ) use ( &$declarations ): void { $chunk = trim( $chunk ); if ( '' === $chunk ) { return; } $split = self::split_property_value( $chunk ); if ( null === $split ) { return; } $declarations[ $split[0] ] = $split[1]; }; for ( $i = 0; $i < $length; $i++ ) { $char = $css[ $i ]; if ( '' !== $quote ) { $buffer .= $char; // A backslash escapes the next character, including the closing quote. if ( '\\' === $char && $i + 1 < $length ) { $buffer .= $css[ ++$i ]; continue; } if ( $char === $quote ) { $quote = ''; } continue; } if ( '"' === $char || "'" === $char ) { $quote = $char; $buffer .= $char; continue; } if ( '(' === $char ) { $paren++; } elseif ( ')' === $char ) { $paren = max( 0, $paren - 1 ); } elseif ( '{' === $char ) { $brace++; } elseif ( '}' === $char ) { $brace = max( 0, $brace - 1 ); } if ( ';' === $char && 0 === $paren && 0 === $brace ) { $flush( $buffer ); $buffer = ''; continue; } $buffer .= $char; } $flush( $buffer ); return $declarations; } /** * Split one declaration at its first top-level ':'. * * @return array{0:string,1:string}|null */ public static function split_property_value( string $declaration ): ?array { $quote = ''; $paren = 0; $length = strlen( $declaration ); for ( $i = 0; $i < $length; $i++ ) { $char = $declaration[ $i ]; if ( '' !== $quote ) { if ( '\\' === $char ) { $i++; continue; } if ( $char === $quote ) { $quote = ''; } continue; } if ( '"' === $char || "'" === $char ) { $quote = $char; continue; } if ( '(' === $char ) { $paren++; continue; } if ( ')' === $char ) { $paren = max( 0, $paren - 1 ); continue; } if ( ':' === $char && 0 === $paren ) { $property = strtolower( trim( substr( $declaration, 0, $i ) ) ); $value = trim( substr( $declaration, $i + 1 ) ); if ( '' === $property || '' === $value ) { return null; } return [ $property, $value ]; } } return null; } /** * Split a `font-family` value into its ordered list of families. * * Returns one entry per family, each `[ 'name' => string, 'quoted' => bool ]`, or NULL when * the value cannot be parsed. NULL is the diagnostic: the caller reports and leaves the * declaration alone rather than guessing at a repair. * * WHAT MUST NOT BE FLATTENED * - `"serif"` is a NAMED font whose name happens to be "serif"; bare `serif` is the * generic family. Collapsing the two would silently change which font renders, so the * quoting is carried alongside the name instead of being stripped. * - `"Foo, Bar"` is ONE family containing a comma. Splitting on every comma would invent * a second family that does not exist. * - `"Foo\"Bar"` escapes a quote inside the name; the escape is resolved into the name. * - Quoted whitespace is significant: `" Foo "` is not `Foo`. * * REJECTED, with NULL rather than a guess: an unterminated string, an empty family (a * doubled or trailing comma), a `var()` or any other function, and stray text after a * closing quote (`"Foo" Bar`). Those are either malformed or outside what this repair * understands, and both cases must reach a human rather than a heuristic. * * @return array|null */ public static function split_font_families( string $value ): ?array { $value = trim( $value ); if ( '' === $value ) { return null; } // A function anywhere in the list -- var(), local(), anything -- is out of scope. if ( preg_match( '/[a-zA-Z-]+\s*\(/', $value ) ) { return null; } $families = []; $buffer = ''; $quote = ''; $quoted = false; $closed = false; $length = strlen( $value ); for ( $i = 0; $i < $length; $i++ ) { $char = $value[ $i ]; if ( '' !== $quote ) { if ( '\\' === $char && $i + 1 < $length ) { $buffer .= $value[ ++$i ]; continue; } if ( $char === $quote ) { $quote = ''; $closed = true; continue; } $buffer .= $char; continue; } if ( '"' === $char || "'" === $char ) { // A quote may only open a family, never appear part-way through an unquoted one. if ( '' !== trim( $buffer ) || $closed ) { return null; } $quote = $char; $quoted = true; continue; } if ( ',' === $char ) { $name = $quoted ? $buffer : trim( $buffer ); if ( '' === $name ) { return null; } $families[] = [ 'name' => $name, 'quoted' => $quoted ]; $buffer = ''; $quoted = false; $closed = false; continue; } // Text after a closing quote, e.g. `"Foo" Bar`, is malformed. if ( $closed && '' !== trim( $char ) ) { return null; } $buffer .= $char; } if ( '' !== $quote ) { return null; // unterminated string } $name = $quoted ? $buffer : trim( $buffer ); if ( '' === $name ) { return null; } $families[] = [ 'name' => $name, 'quoted' => $quoted ]; return $families; } /** * Render a family list back to a `font-family` value. * * A family that was quoted stays quoted, so the named-vs-generic distinction survives the * round trip. Embedded quotes and backslashes are re-escaped. * * @param array $families */ public static function join_font_families( array $families ): string { $parts = []; foreach ( $families as $family ) { $name = (string) ( $family['name'] ?? '' ); if ( '' === $name ) { continue; } if ( ! empty( $family['quoted'] ) ) { $parts[] = '"' . str_replace( [ '\\', '"' ], [ '\\\\', '\\"' ], $name ) . '"'; continue; } $parts[] = $name; } return implode( ', ', $parts ); } /** * Compare two families for identity. * * Font family names are case-insensitive, but a QUOTED name and an UNQUOTED one are only * the same family when the unquoted one is not a generic. `"serif"` never equals `serif`. * * @param array{name:string,quoted:bool} $a * @param array{name:string,quoted:bool} $b */ public static function font_families_match( array $a, array $b ): bool { $a_name = strtolower( trim( (string) ( $a['name'] ?? '' ) ) ); $b_name = strtolower( trim( (string) ( $b['name'] ?? '' ) ) ); if ( $a_name !== $b_name ) { return false; } if ( ! empty( $a['quoted'] ) === ! empty( $b['quoted'] ) ) { return true; } // One is quoted and the other is not: only a generic keyword makes that a real difference. return ! in_array( $a_name, self::GENERIC_FONT_FAMILIES, true ); } /** CSS generic families. Bare, these are keywords; quoted, they are ordinary font names. */ public const GENERIC_FONT_FAMILIES = [ 'serif', 'sans-serif', 'monospace', 'cursive', 'fantasy', 'system-ui', 'ui-serif', 'ui-sans-serif', 'ui-monospace', 'ui-rounded', 'math', 'emoji', 'fangsong', ]; /** * A CSS property name V4 Studio may emit, or '' when it may not. * * Standard properties are lower-cased and must be known. A custom property (`--name`) is accepted * when its name has the shape is_safe_css_variable_name() allows, and keeps its case: custom * properties are case-sensitive. Its value goes through sanitize_css_declaration_value() like any * other. Unmapped, they reach Elementor as the class's custom_css, where Elementor itself stores them. */ public static function sanitize_css_property( string $property ): string { $property = trim( $property ); if ( str_starts_with( $property, '--' ) ) { return self::is_safe_css_variable_name( $property ) ? $property : ''; } $property = strtolower( $property ); return self::is_known_css_property( $property ) ? $property : ''; } /** * Is this a standard CSS property name? * * Every property that reaches Elementor -- as a native prop, as a class's custom CSS, or in * the fallback stylesheet -- passes through sanitize_css_property(), so this list is what * keeps class CSS to real CSS. It is the W3C index of all CSS properties plus a short * supplement, maintained in includes/data/css-properties.php. * A vendor-prefixed spelling of a listed property is accepted too. */ public static function is_known_css_property( string $property ): bool { static $known = null; if ( null === $known ) { $known = array_flip( (array) require __DIR__ . '/../data/css-properties.php' ); } $property = strtolower( $property ); if ( isset( $known[ $property ] ) ) { return true; } return 1 === preg_match( '/^-(?:webkit|moz|ms|o)-([a-z][a-z0-9-]*)$/', $property, $match ) && isset( $known[ $match[1] ] ); } /** * A well-formed property name that is not standard CSS, such as a typo. A class write * reports these by name, since sanitize_css_property() drops them. Custom properties are * not reported here. */ public static function is_unrecognised_css_property( string $property ): bool { $property = strtolower( trim( $property ) ); return 1 === preg_match( '/^-?[a-z][a-z0-9-]*$/', $property ) && ! self::is_known_css_property( $property ); } /** * Sanitise a declaration value without corrupting legitimate CSS. * * `;`, `{` and `}` must not survive at TOP LEVEL: that is how a value breaks out of its own * declaration and opens a new rule (`red; } body { display:none`). They are, however, * perfectly legal inside a quoted string or a function, and stripping them there silently * damaged real values: * * url("data:image/svg+xml;base64,PHN2Zz4=") -> url("data:image/svg+xmlbase64,...") * "Foo; Bar", serif -> "Foo Bar", serif * * The first case is the serious one: a corrupted data URL is a broken image with no error * anywhere. So the strip is now structure-aware — quote state and parenthesis depth are * tracked, and only top-level structural characters are removed. * * SAFETY. Structure awareness is only safe while the structure itself is trustworthy. A * value with an unterminated string or unbalanced parentheses is exactly how an attacker * would smuggle a top-level `;` past a naive scanner, so such a value falls back to the * original blunt strip rather than being trusted. */ public static function sanitize_css_declaration_value( string $value ): string { $value = wp_strip_all_tags( $value ); $value = preg_replace( '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/', '', $value ) ?? $value; $value = str_ireplace( '