64 ) { return null; } if ( preg_match( '/^#(?:[0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/', $value ) ) { return strtolower( $value ); } $number = '-?(?:\d+\.?\d*|\.\d+)(?:%|deg)?'; $commas = "\\s*{$number}\\s*,\\s*{$number}\\s*,\\s*{$number}\\s*(?:,\\s*{$number}\\s*)?"; $spaces = "\\s*{$number}\\s+{$number}\\s+{$number}\\s*(?:\\/\\s*{$number}\\s*)?"; if ( preg_match( "/^(?:rgba?|hsla?)\\((?:{$commas}|{$spaces})\\)$/i", $value ) ) { return $value; } if ( preg_match( '/^[a-zA-Z]+$/', $value ) && 'color' === ( new Validator() )->detect_variable_type( $value ) ) { return strtolower( $value ); } return null; } /** A size value that passes the grammar, or null. */ public static function size( string $value ): ?string { $value = trim( $value ); if ( '' === $value || strlen( $value ) > self::MAX_LENGTH || preg_match( '/^-\s/', $value ) ) { return null; } $tokens = self::tokenize( $value ); if ( null === $tokens ) { return null; } $position = 0; if ( ! self::operand( $tokens, $position, 0, true ) || $position !== count( $tokens ) ) { return null; } return $value; } /** Is this a View Port fluid sizes can be built on: whole px, within the bounds, and wide enough? */ public static function viewport( $min, $max ): bool { if ( ! is_numeric( $min ) || ! is_numeric( $max ) || floor( (float) $min ) !== (float) $min || floor( (float) $max ) !== (float) $max ) { return false; } return $min >= self::VIEWPORT_LOWEST && $max <= self::VIEWPORT_HIGHEST && $max - $min >= self::VIEWPORT_GAP; } /** A fluid size from a min and max in px, reached at the View Port's min and max, as the shipped defaults write it. */ public static function fluid( float $min_px, float $max_px, int $viewport_min = self::VIEWPORT_MIN, int $viewport_max = self::VIEWPORT_MAX ): ?string { if ( $min_px < 0 || $max_px < 0 || $min_px > 4000 || $max_px > 4000 || ! self::viewport( $viewport_min, $viewport_max ) ) { return null; } $slope = ( $max_px - $min_px ) / ( $viewport_max - $viewport_min ) * 100; $intercept = ( $min_px - ( $slope / 100 ) * $viewport_min ) / self::ROOT_PX; $operator = $intercept < 0 ? '-' : '+'; return sprintf( 'clamp(%srem, calc(%svw %s %srem), %srem)', self::number_text( $min_px / self::ROOT_PX ), self::number_text( $slope ), $operator, self::number_text( abs( $intercept ) ), self::number_text( $max_px / self::ROOT_PX ) ); } /** A fixed size such as `1.5rem`. */ public static function fixed( float $number, string $unit ): ?string { if ( ! in_array( $unit, self::UNITS, true ) || abs( $number ) > 10000 ) { return null; } return self::number_text( $number ) . $unit; } /** A font stack built from a family name and a generic fallback, e.g. `"DM Sans", sans-serif`. */ public static function font( string $family, string $generic ): ?string { $family = trim( preg_replace( '/\s+/', ' ', $family ) ?? '' ); if ( ! in_array( $generic, self::GENERICS, true ) || ! preg_match( '/^[\p{L}\p{N}][\p{L}\p{N} ._-]{0,63}$/u', $family ) ) { return null; } return '"' . $family . '", ' . $generic; } /** A plain number, such as a line height or font weight. */ public static function number( string $value ): ?string { $value = trim( $value ); return preg_match( '/^-?(?:\d+\.?\d*|\.\d+)$/', $value ) && strlen( $value ) <= 16 ? $value : null; } /** A reference to another variable: `var(--name)`. */ public static function alias( string $target ): ?string { $target = '--' . ltrim( trim( $target ), '-' ); return preg_match( '/^' . self::NAME . '$/', $target ) ? 'var(' . $target . ')' : null; } /** * What a stored value is, as a form row model. The browser never parses CSS: it edits these * fields and the server rebuilds the value. `custom` is shown as stored. * * @return array */ public static function describe( string $value ): array { $value = trim( $value ); if ( preg_match( '/^var\(\s*(' . self::NAME . ')\s*\)$/', $value, $match ) ) { return [ 'kind' => 'alias', 'target' => $match[1] ]; } if ( null !== self::color( $value ) ) { return [ 'kind' => 'color', 'value' => $value ]; } $fluid = self::fluid_parts( $value ); if ( null !== $fluid ) { $model = [ 'kind' => 'size', 'mode' => 'fluid', 'min' => $fluid[0], 'max' => $fluid[1] ]; // The View Port is named only when it is not the default one. if ( self::VIEWPORT_MIN !== $fluid[2] || self::VIEWPORT_MAX !== $fluid[3] ) { $model['vmin'] = $fluid[2]; $model['vmax'] = $fluid[3]; } return $model; } if ( preg_match( '/^(-?(?:\d+\.?\d*|\.\d+))(' . implode( '|', array_map( 'preg_quote', self::UNITS ) ) . ')$/', $value, $match ) ) { return [ 'kind' => 'size', 'mode' => 'fixed', 'number' => (float) $match[1], 'unit' => $match[2] ]; } if ( preg_match( '/^"?([\p{L}\p{N}][\p{L}\p{N} ._-]{0,63}?)"?\s*,\s*(' . implode( '|', array_map( 'preg_quote', self::GENERICS ) ) . ')$/u', $value, $match ) ) { return [ 'kind' => 'font', 'family' => $match[1], 'generic' => $match[2] ]; } if ( null !== self::number( $value ) ) { return [ 'kind' => 'number', 'value' => $value ]; } if ( null !== self::size( $value ) ) { return [ 'kind' => 'size', 'mode' => 'expression', 'value' => $value ]; } return [ 'kind' => 'custom', 'value' => $value ]; } /** * Min and max in px, and the View Port they are reached at, when the value is a fluid size, else * null. The View Port is worked out from the clamp itself: where its middle term meets the min and * the max. The stored numbers are rounded to 4 decimals, so for a gentle slope several whole-px * View Ports near that point rebuild the same value; of those (within 2 px), the default one is * taken, then one in round numbers (tens, then fives), then the nearest. A clamp no valid View Port * rebuilds is an expression, so editing min/max never silently changes its curve. A flat value * (min equal to max) has no View Port of its own and takes the default one. * * @return array{0:float,1:float,2:int,3:int}|null */ private static function fluid_parts( string $value ): ?array { $number = '(-?(?:\d+\.?\d*|\.\d+))'; if ( ! preg_match( "/^clamp\\(\\s*{$number}rem\\s*,\\s*calc\\(\\s*{$number}vw\\s*([+-])\\s*{$number}rem\\s*\\)\\s*,\\s*{$number}rem\\s*\\)$/", $value, $m ) ) { return null; } $min = (float) $m[1] * self::ROOT_PX; $max = (float) $m[5] * self::ROOT_PX; $slope = (float) $m[2]; $stored = ( '-' === $m[3] ? -1 : 1 ) * (float) $m[4]; if ( abs( $slope ) < 0.00005 ) { return abs( $stored * self::ROOT_PX - $min ) <= 0.032 ? [ round( $min, 4 ), round( $max, 4 ), self::VIEWPORT_MIN, self::VIEWPORT_MAX ] : null; } $near_min = ( $min - $stored * self::ROOT_PX ) / ( $slope / 100 ); $near_max = ( $max - $stored * self::ROOT_PX ) / ( $slope / 100 ); $candidates = [ [ self::VIEWPORT_MIN, self::VIEWPORT_MAX ] ]; for ( $low = -2; $low <= 2; $low++ ) { for ( $high = -2; $high <= 2; $high++ ) { $candidates[] = [ (int) round( $near_min ) + $low, (int) round( $near_max ) + $high ]; } } $best = null; foreach ( $candidates as $candidate ) { [ $viewport_min, $viewport_max ] = $candidate; if ( ! self::viewport( $viewport_min, $viewport_max ) ) { continue; } $rebuilt = ( $max - $min ) / ( $viewport_max - $viewport_min ) * 100; $intercept = ( $min - ( $rebuilt / 100 ) * $viewport_min ) / self::ROOT_PX; if ( abs( $rebuilt - $slope ) > 0.002 || abs( $intercept - $stored ) > 0.002 ) { continue; } $rank = [ self::VIEWPORT_MIN === $viewport_min && self::VIEWPORT_MAX === $viewport_max ? 0 : ( 0 === $viewport_min % 10 && 0 === $viewport_max % 10 ? 1 : ( 0 === $viewport_min % 5 && 0 === $viewport_max % 5 ? 2 : 3 ) ), abs( $viewport_min - $near_min ) + abs( $viewport_max - $near_max ), ]; if ( null === $best || $rank < $best[0] ) { $best = [ $rank, $viewport_min, $viewport_max ]; } } return null === $best ? null : [ round( $min, 4 ), round( $max, 4 ), $best[1], $best[2] ]; } private static function number_text( float $number ): string { $text = rtrim( rtrim( number_format( round( $number, 4 ), 4, '.', '' ), '0' ), '.' ); return '-0' === $text ? '0' : $text; } /** * Tokens: ['n', unit|'', text] numbers, ['v'] variable references, ['f', name] function openings, * and single characters for ( ) , + - * /. Null for anything else. * * @return array>|null */ private static function tokenize( string $value ): ?array { $units = implode( '|', array_map( 'preg_quote', self::UNITS ) ); $tokens = []; $offset = 0; $length = strlen( $value ); while ( $offset < $length ) { $rest = substr( $value, $offset ); if ( preg_match( '/^\s+/', $rest, $m ) ) { $offset += strlen( $m[0] ); continue; } if ( preg_match( '/^var\(\s*' . self::NAME . '\s*\)/', $rest, $m ) ) { $tokens[] = [ 'v' ]; } elseif ( preg_match( '/^(clamp|min|max|calc)\(/i', $rest, $m ) ) { $tokens[] = [ 'f', strtolower( $m[1] ) ]; } elseif ( preg_match( '/^(?:\d+\.?\d*|\.\d+)(' . $units . ')?/', $rest, $m ) ) { $tokens[] = [ 'n', $m[1] ?? '', $m[0] ]; } elseif ( preg_match( '/^[(),+\-*\/]/', $rest, $m ) ) { $tokens[] = [ $m[0] ]; } else { return null; } $offset += strlen( $m[0] ); } return $tokens; } /** * operand := number | var | function | '(' expression ')'. At the top level only a lone 0 may * be unitless, and no operators are allowed outside a function. */ private static function operand( array $tokens, int &$position, int $depth, bool $top ): bool { $token = $tokens[ $position ] ?? null; if ( null === $token || $depth > self::MAX_DEPTH ) { return false; } if ( 'n' === $token[0] ) { ++$position; return ! $top || '' !== $token[1] || 0.0 === (float) $token[2]; } if ( 'v' === $token[0] ) { ++$position; return true; } if ( '-' === $token[0] ) { ++$position; // A negative length such as -2px is fine anywhere; at the top level nothing else may follow a sign. if ( $top && ( 'n' !== ( $tokens[ $position ][0] ?? null ) || '' === $tokens[ $position ][1] ) ) { return false; } return self::operand( $tokens, $position, $depth, $top ); } if ( 'f' === $token[0] ) { ++$position; $arguments = 0; do { if ( ! self::expression( $tokens, $position, $depth + 1 ) ) { return false; } ++$arguments; $separator = $tokens[ $position ][0] ?? null; ++$position; } while ( ',' === $separator ); if ( ')' !== $separator ) { return false; } $name = $token[1]; return ( 'clamp' === $name && 3 === $arguments ) || ( 'calc' === $name && 1 === $arguments ) || ( in_array( $name, [ 'min', 'max' ], true ) && $arguments >= 1 ); } if ( '(' === $token[0] && ! $top ) { ++$position; if ( ! self::expression( $tokens, $position, $depth + 1 ) || ')' !== ( $tokens[ $position ][0] ?? null ) ) { return false; } ++$position; return true; } return false; } private static function expression( array $tokens, int &$position, int $depth ): bool { if ( ! self::operand( $tokens, $position, $depth, false ) ) { return false; } while ( in_array( $tokens[ $position ][0] ?? null, [ '+', '-', '*', '/' ], true ) ) { ++$position; if ( ! self::operand( $tokens, $position, $depth, false ) ) { return false; } } return true; } }