From d64c5514168665d17aaec5628de016edc56a2c84 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 17 Nov 2026 11:18:34 +0400 Subject: [PATCH 1/5] Style Engine: Check transform values with CSS tokens, not regexes Replace WP_Style_Engine::VALID_ANGLE_PATTERN and VALID_LENGTH_PERCENTAGE_PATTERN with WP_Style_Engine::is_angle() and WP_Style_Engine::is_length_percentage(). Each takes CSS text or a WP_CSS_Token. Text must tokenize as exactly one token: a dimension token with an angle or length unit compared in ASCII lowercase, a percentage token, or the number token `0`. The unit tables list the units the regexes listed. kses checks `translate` on tokens instead of preg_split( '/\s+/' ): one or two length or percentage tokens, the second after a whitespace token, comments ignored. The parser path passes its value tokens directly and no longer serializes them to run a regex. The legacy path tokenizes its trimmed value. The accepted set differs from the regexes in two ways: - A number with an exponent is accepted, as CSS reads it: input regex tokens rotate: 100deg accepted accepted rotate: 1e2deg rejected accepted - In the style engine and theme.json, an escaped unit is accepted. The style engine output is already filtered by safecss_filter_attr(), whose parser path writes the unit decoded: rotate value regex tokens 1deg rotate:1deg; rotate:1deg; 1\64 eg (empty) rotate:1deg; kses was already the same on both: the parser path checks decoded tokens, and the legacy path rejects every `\`. Comments, `+1deg`, `.5turn`, uppercase units and every whitespace form tested give the same result on both. The number `0` stays the only unitless value, so `0.0` and `-0` are rejected as before. Every test from https://github.com/WordPress/wordpress-develop/pull/15408 and https://github.com/WordPress/wordpress-develop/pull/15465 passes unchanged. `--group kses,style-engine,block-supports,themes,css-api`: 3503 tests pass. --- src/wp-includes/block-supports/transform.php | 2 +- src/wp-includes/class-wp-theme-json.php | 2 +- src/wp-includes/kses.php | 72 +++++-- .../style-engine/class-wp-style-engine.php | 190 +++++++++++++++++- .../phpunit/tests/kses/safecssFilterAttr.php | 58 ++++++ .../style-engine/wpStyleEngineValueTypes.php | 153 ++++++++++++++ 6 files changed, 446 insertions(+), 31 deletions(-) create mode 100644 tests/phpunit/tests/style-engine/wpStyleEngineValueTypes.php diff --git a/src/wp-includes/block-supports/transform.php b/src/wp-includes/block-supports/transform.php index 72f6fd08622dc..970ae0c271421 100644 --- a/src/wp-includes/block-supports/transform.php +++ b/src/wp-includes/block-supports/transform.php @@ -60,7 +60,7 @@ function wp_fill_translate_axes( $translate, ...$fallbacks ) { } foreach ( $fallbacks as $fallback ) { $value = is_array( $fallback ) ? ( $fallback[ $axis ] ?? null ) : null; - if ( is_string( $value ) && preg_match( WP_Style_Engine::VALID_LENGTH_PERCENTAGE_PATTERN, $value ) ) { + if ( WP_Style_Engine::is_length_percentage( $value ) ) { $translate[ $axis ] = $value; break; } diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 27d1a31d74935..cfb39ed4c7bf3 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -3292,7 +3292,7 @@ protected static function compute_style_properties( $styles, $settings = array() } // Mirrors the style engine, which outputs a single angle only. - if ( 'rotate' === $css_property && ( ! is_string( $value ) || ! preg_match( WP_Style_Engine::VALID_ANGLE_PATTERN, $value ) ) ) { + if ( 'rotate' === $css_property && ! WP_Style_Engine::is_angle( $value ) ) { continue; } diff --git a/src/wp-includes/kses.php b/src/wp-includes/kses.php index 82c375da4dded..0954278b1ff8e 100644 --- a/src/wp-includes/kses.php +++ b/src/wp-includes/kses.php @@ -4212,18 +4212,17 @@ function _safecss_filter_attr_legacy( $css, $allowed_attr ) { // Only allow a single angle for `rotate`, mirroring the style engine. if ( $allow_css && $angle_attr ) { - $allow_css = 1 === preg_match( WP_Style_Engine::VALID_ANGLE_PATTERN, trim( $parts[1] ) ); + $allow_css = WP_Style_Engine::is_angle( trim( $parts[1] ) ); } // Only allow one or two lengths or percentages for `translate`, mirroring the style engine. if ( $allow_css && $translate_attr ) { - $axes = preg_split( '/\s+/', trim( $parts[1] ) ); - $allow_css = count( $axes ) <= 2; - foreach ( $axes as $axis ) { - if ( $allow_css && 1 !== preg_match( WP_Style_Engine::VALID_LENGTH_PERCENTAGE_PATTERN, $axis ) ) { - $allow_css = false; - } + $processor = new WP_CSS_Token_Processor( trim( $parts[1] ) ); + $tokens = array(); + while ( $processor->next_token() ) { + $tokens[] = $processor->get_token(); } + $allow_css = _safecss_filter_attr_is_translate_value( $tokens ); } /** @@ -4278,9 +4277,10 @@ function _safecss_filter_attr_legacy( $css, $allowed_attr ) { * - The value has no `&` delimiter at any depth. The output contains no * `&`: other tokens escape it, and no property value uses a bare `&`. * - The value has no `\` or control character delimiter at any depth. - * - A `rotate` value is a single angle and a `translate` value one or two - * lengths or percentages, as WP_Style_Engine::VALID_ANGLE_PATTERN and - * WP_Style_Engine::VALID_LENGTH_PERCENTAGE_PATTERN match them. + * - A `rotate` value is a single angle, as WP_Style_Engine::is_angle() + * checks, and a `translate` value one or two lengths or percentages + * separated by whitespace, as _safecss_filter_attr_is_translate_value() + * checks. * * Checks run on decoded values. Comments are dropped; an empty comment is * written only between two tokens that would otherwise read as other @@ -4339,21 +4339,13 @@ function _safecss_filter_attr_declarations( $css, $allowed_attr ) { } // Only allow a single angle for `rotate`, mirroring the style engine. - if ( 'rotate' === $name && 1 !== preg_match( WP_Style_Engine::VALID_ANGLE_PATTERN, WP_CSS_Token_Serializer::serialize( $tokens ) ) ) { + if ( 'rotate' === $name && ( 1 !== count( $tokens ) || ! WP_Style_Engine::is_angle( $tokens[0] ) ) ) { continue; } // Only allow one or two lengths or percentages for `translate`, mirroring the style engine. - if ( 'translate' === $name ) { - $axes = preg_split( '/\s+/', WP_CSS_Token_Serializer::serialize( $tokens ) ); - if ( count( $axes ) > 2 ) { - continue; - } - foreach ( $axes as $axis ) { - if ( 1 !== preg_match( WP_Style_Engine::VALID_LENGTH_PERCENTAGE_PATTERN, $axis ) ) { - continue 2; - } - } + if ( 'translate' === $name && ! _safecss_filter_attr_is_translate_value( $tokens ) ) { + continue; } // The deprecated `safecss_filter_attr_allow_css` filter is applied by the legacy implementation only. @@ -4364,6 +4356,44 @@ function _safecss_filter_attr_declarations( $css, $allowed_attr ) { return implode( ' ', $declarations ); } +/** + * Checks whether tokens are one or two lengths or percentages separated by whitespace, the `translate` values this filter allows. + * + * Comments are ignored, so a comment alone does not separate two lengths. + * + * @since 7.3.0 + * @access private + * @ignore + * @internal + * + * @see WP_Style_Engine::is_length_percentage() + * + * @param WP_CSS_Token[] $tokens Value tokens, without leading or trailing whitespace. + * @return bool Whether the tokens are an allowed `translate` value. + */ +function _safecss_filter_attr_is_translate_value( $tokens ) { + $count = 0; + $after_space = true; + foreach ( $tokens as $token ) { + switch ( $token->get_type() ) { + case WP_CSS_Token_Processor::TOKEN_COMMENT: + continue 2; + + case WP_CSS_Token_Processor::TOKEN_WHITESPACE: + $after_space = true; + continue 2; + } + + // A second length must follow whitespace, and a third is not allowed. + if ( ! $after_space || ++$count > 2 || ! WP_Style_Engine::is_length_percentage( $token ) ) { + return false; + } + $after_space = false; + } + + return $count > 0; +} + /** * Checks a declaration value's tokens against the function, URL, structure and delimiter policy. * diff --git a/src/wp-includes/style-engine/class-wp-style-engine.php b/src/wp-includes/style-engine/class-wp-style-engine.php index 82f3890e0c9ed..c974637ccca7a 100644 --- a/src/wp-includes/style-engine/class-wp-style-engine.php +++ b/src/wp-includes/style-engine/class-wp-style-engine.php @@ -49,20 +49,79 @@ final class WP_Style_Engine { const VALID_BACKGROUND_CLIP_VALUES = array( 'border-box', 'padding-box', 'content-box', 'text' ); /** - * Matches a single CSS angle: a number with a `deg`, `grad`, `rad` or `turn` unit, or `0`. + * Angle units, lowercase. * * @since 7.3.0 - * @var string + * @ignore + * + * @link https://www.w3.org/TR/css-values-4/#angles + * + * @var string[] */ - const VALID_ANGLE_PATTERN = '/^(?:[+-]?(?:\d+(?:\.\d+)?|\.\d+)(?:deg|grad|rad|turn)|0)$/iD'; + private const ANGLE_UNITS = array( 'deg', 'grad', 'rad', 'turn' ); /** - * Matches a single CSS length or percentage: a number with a length unit or `%`, or `0`. + * Length units, lowercase. * * @since 7.3.0 - * @var string + * @ignore + * + * @link https://www.w3.org/TR/css-values-4/#lengths + * @link https://www.w3.org/TR/css-contain-3/#container-lengths + * + * @var string[] */ - const VALID_LENGTH_PERCENTAGE_PATTERN = '/^(?:[+-]?(?:\d+(?:\.\d+)?|\.\d+)(?:%|px|cm|mm|q|in|pt|pc|r?em|r?ex|r?ch|r?cap|r?ic|r?lh|[sld]?v(?:w|h|i|b|min|max)|cq(?:w|h|i|b|min|max))|0)$/iD'; + private const LENGTH_UNITS = array( + 'px', + 'cm', + 'mm', + 'q', + 'in', + 'pt', + 'pc', + 'em', + 'rem', + 'ex', + 'rex', + 'ch', + 'rch', + 'cap', + 'rcap', + 'ic', + 'ric', + 'lh', + 'rlh', + 'vw', + 'vh', + 'vi', + 'vb', + 'vmin', + 'vmax', + 'svw', + 'svh', + 'svi', + 'svb', + 'svmin', + 'svmax', + 'lvw', + 'lvh', + 'lvi', + 'lvb', + 'lvmin', + 'lvmax', + 'dvw', + 'dvh', + 'dvi', + 'dvb', + 'dvmin', + 'dvmax', + 'cqw', + 'cqh', + 'cqi', + 'cqb', + 'cqmin', + 'cqmax', + ); /** * Style definitions that contain the instructions to parse/output valid Gutenberg styles from a block's attributes. @@ -892,6 +951,121 @@ protected static function get_background_clip_css_declarations( $style_value, $s return $css_declarations; } + /** + * Checks whether a value is a single CSS angle. + * + * An angle is a dimension token with a `deg`, `grad`, `rad` or `turn` + * unit in any ASCII case, or the number token `0`. A string must + * tokenize as that one token, so whitespace, comments, lists and + * functions such as `calc()` are rejected. + * + * Example: + * + * WP_Style_Engine::is_angle( '-.5turn' ); // true + * WP_Style_Engine::is_angle( '1E2DEG' ); // true + * WP_Style_Engine::is_angle( '45' ); // false + * WP_Style_Engine::is_angle( ' 45deg' ); // false + * + * @since 7.3.0 + * + * @link https://www.w3.org/TR/css-values-4/#angles + * + * @param mixed $value CSS text, or a token read by WP_CSS_Token_Processor. + * @return bool Whether the value is a single angle. + */ + public static function is_angle( $value ): bool { + $token = static::get_single_token( $value ); + if ( null === $token ) { + return false; + } + + if ( WP_CSS_Token_Processor::TOKEN_DIMENSION === $token->get_type() ) { + return in_array( strtolower( (string) $token->get_unit() ), self::ANGLE_UNITS, true ); + } + + return static::is_zero( $token ); + } + + /** + * Checks whether a value is a single CSS length or percentage. + * + * A length is a dimension token with a length unit in any ASCII case, or + * the number token `0`. A percentage is a percentage token. A string must + * tokenize as that one token, so whitespace, comments, lists and + * functions such as `calc()` are rejected. + * + * Example: + * + * WP_Style_Engine::is_length_percentage( '-50%' ); // true + * WP_Style_Engine::is_length_percentage( '10DVH' ); // true + * WP_Style_Engine::is_length_percentage( '10' ); // false + * WP_Style_Engine::is_length_percentage( '5deg' ); // false + * + * @since 7.3.0 + * + * @link https://www.w3.org/TR/css-values-4/#typedef-length-percentage + * + * @param mixed $value CSS text, or a token read by WP_CSS_Token_Processor. + * @return bool Whether the value is a single length or percentage. + */ + public static function is_length_percentage( $value ): bool { + $token = static::get_single_token( $value ); + if ( null === $token ) { + return false; + } + + switch ( $token->get_type() ) { + case WP_CSS_Token_Processor::TOKEN_PERCENTAGE: + return true; + + case WP_CSS_Token_Processor::TOKEN_DIMENSION: + return in_array( strtolower( (string) $token->get_unit() ), self::LENGTH_UNITS, true ); + + default: + return static::is_zero( $token ); + } + } + + /** + * Returns the token a value is, if it is exactly one token. + * + * @since 7.3.0 + * @ignore + * + * @param mixed $value CSS text, or a token read by WP_CSS_Token_Processor. + * @return WP_CSS_Token|null The token, or null when the value is not a string of exactly one token or a token. + */ + private static function get_single_token( $value ): ?WP_CSS_Token { + if ( $value instanceof WP_CSS_Token ) { + return $value; + } + + if ( ! is_string( $value ) ) { + return null; + } + + $processor = new WP_CSS_Token_Processor( $value ); + if ( ! $processor->next_token() ) { + return null; + } + + $token = $processor->get_token(); + return $processor->next_token() ? null : $token; + } + + /** + * Checks whether a token is the number `0`, which is accepted as a zero length or angle. + * + * @since 7.3.0 + * @ignore + * + * @param WP_CSS_Token $token Token. + * @return bool Whether the token is the number `0`. + */ + private static function is_zero( WP_CSS_Token $token ): bool { + return WP_CSS_Token_Processor::TOKEN_NUMBER === $token->get_type() && '0' === $token->get_value(); + } + /** * Style value parser that returns a CSS definition array for a property that takes a single angle. * @@ -904,7 +1078,7 @@ protected static function get_background_clip_css_declarations( $style_value, $s * @return array An associative array of CSS definitions, mapping properties to values. */ protected static function get_angle_css_declarations( $style_value, $style_definition ) { - if ( ! is_string( $style_value ) || ! preg_match( static::VALID_ANGLE_PATTERN, $style_value ) ) { + if ( ! static::is_angle( $style_value ) ) { return array(); } @@ -932,7 +1106,7 @@ protected static function get_translate_css_declarations( $style_value, $style_d $axes = array(); foreach ( array( 'x', 'y' ) as $axis ) { $axis_value = $style_value[ $axis ] ?? '0'; - if ( ! is_string( $axis_value ) || ! preg_match( static::VALID_LENGTH_PERCENTAGE_PATTERN, $axis_value ) ) { + if ( ! is_string( $axis_value ) || ! static::is_length_percentage( $axis_value ) ) { return array(); } $axes[] = $axis_value; diff --git a/tests/phpunit/tests/kses/safecssFilterAttr.php b/tests/phpunit/tests/kses/safecssFilterAttr.php index 584231a802368..08e4f76b6ef83 100644 --- a/tests/phpunit/tests/kses/safecssFilterAttr.php +++ b/tests/phpunit/tests/kses/safecssFilterAttr.php @@ -483,6 +483,64 @@ public function test_whitespace_runs_become_one_space() { $this->assertSame( 'aspect-ratio: calc( 16 / 9 );', safecss_filter_attr( 'aspect-ratio: calc( 16 / 9 );' ) ); } + /** + * `rotate` keeps a single angle and `translate` one or two lengths or + * percentages separated by whitespace, on both implementations. + * + * @ticket 66468 + * @ticket 66467 + * @dataProvider data_transform_values + * + * @covers ::_safecss_filter_attr_is_translate_value + * + * @param string $css Input declaration. + * @param string $expected Output of the parser-based implementation. + * @param string $expected_legacy Output of the legacy implementation. + */ + public function test_transform_values( $css, $expected, $expected_legacy ) { + $this->assertSame( $expected, safecss_filter_attr( $css ), 'Parser-based implementation' ); + + add_filter( 'wp_kses_force_legacy_css_parser', '__return_true' ); + $actual_legacy = safecss_filter_attr( $css ); + remove_filter( 'wp_kses_force_legacy_css_parser', '__return_true' ); + + $this->assertSame( $expected_legacy, $actual_legacy, 'Legacy implementation' ); + } + + /** + * @return array + */ + public function data_transform_values() { + return array( + 'rotate with a leading plus sign' => array( 'rotate: +1deg', 'rotate: +1deg;', 'rotate: +1deg' ), + 'rotate with no integer part' => array( 'rotate: .5turn', 'rotate: .5turn;', 'rotate: .5turn' ), + 'rotate with an uppercase unit' => array( 'rotate: 1DEG', 'rotate: 1DEG;', 'rotate: 1DEG' ), + 'rotate with an exponent' => array( 'rotate: 1e2deg', 'rotate: 1e2deg;', 'rotate: 1e2deg' ), + 'rotate with an escaped unit' => array( 'rotate: 1\64 eg', 'rotate: 1deg;', '' ), + 'rotate with a leading comment' => array( 'rotate: /**/45deg', 'rotate: 45deg;', '' ), + 'rotate with a comment in the number' => array( 'rotate: 4/**/5deg', '', '' ), + 'rotate with important' => array( 'rotate: 45deg !important', 'rotate: 45deg !important;', '' ), + 'rotate with a space before the unit' => array( 'rotate: 45 deg', '', '' ), + 'rotate with zero written 0.0' => array( 'rotate: 0.0', '', '' ), + 'translate with an uppercase unit' => array( 'translate: 1PX 50%', 'translate: 1PX 50%;', 'translate: 1PX 50%' ), + 'translate with an exponent' => array( 'translate: 1e1px', 'translate: 1e1px;', 'translate: 1e1px' ), + 'translate with an escaped unit' => array( 'translate: 1\70 x', 'translate: 1px;', '' ), + 'translate with two spaces' => array( 'translate: 1px 2px', 'translate: 1px 2px;', 'translate: 1px 2px' ), + 'translate with a tab' => array( "translate: 1px\t2px", 'translate: 1px 2px;', '' ), + 'translate with a newline' => array( "translate: 1px\n2px", 'translate: 1px 2px;', '' ), + 'translate with a form feed' => array( "translate: 1px\f2px", 'translate: 1px 2px;', '' ), + 'translate with a vertical tab' => array( "translate: 1px\v2px", '', '' ), + 'translate with a no-break space' => array( "translate: 1px\u{00A0}2px", '', '' ), + 'translate separated by a comment' => array( 'translate: 1px/**/2px', '', '' ), + 'translate separated by a comment and space' => array( 'translate: 1px /**/ 2px', 'translate: 1px 2px;', '' ), + 'translate with no space after a percentage' => array( 'translate: 50%2px', '', '' ), + 'translate with a comma' => array( 'translate: 1px, 2px', '', '' ), + 'translate with three lengths' => array( 'translate: 1px 2px 3px', '', '' ), + 'translate with zero written 0.0' => array( 'translate: 0.0', '', '' ), + 'translate with no value' => array( 'translate:', '', '' ), + ); + } + /** * @ticket 65738 */ diff --git a/tests/phpunit/tests/style-engine/wpStyleEngineValueTypes.php b/tests/phpunit/tests/style-engine/wpStyleEngineValueTypes.php new file mode 100644 index 0000000000000..66c6fba476570 --- /dev/null +++ b/tests/phpunit/tests/style-engine/wpStyleEngineValueTypes.php @@ -0,0 +1,153 @@ +assertSame( $expected, WP_Style_Engine::is_angle( $value ) ); + } + + /** + * @return array + */ + public function data_angles() { + return array( + 'degrees' => array( '45deg', true ), + 'negative' => array( '-15deg', true ), + 'leading plus sign' => array( '+1deg', true ), + 'no integer part' => array( '.5turn', true ), + 'negative with no integer part' => array( '-.5turn', true ), + 'radians' => array( '1.5rad', true ), + 'gradians' => array( '100grad', true ), + 'uppercase unit' => array( '1DEG', true ), + 'mixed case unit' => array( '100GrAd', true ), + 'zero with unit' => array( '0deg', true ), + 'number 0' => array( '0', true ), + 'exponent' => array( '1e2deg', true ), + 'uppercase exponent and unit' => array( '1E2DEG', true ), + 'negative exponent' => array( '1e-1turn', true ), + 'hex escape in unit' => array( '1\64 eg', true ), + 'identity escape in unit' => array( '1de\g', true ), + 'number other than 0' => array( '45', false ), + 'zero with a decimal point' => array( '0.0', false ), + 'negative zero' => array( '-0', false ), + 'length' => array( '1px', false ), + 'percentage' => array( '50%', false ), + 'unit after a trailing dot' => array( '1.deg', false ), + 'space before the unit' => array( '45 deg', false ), + 'leading space' => array( ' 45deg', false ), + 'trailing space' => array( '45deg ', false ), + 'trailing newline' => array( "45deg\n", false ), + 'leading comment' => array( '/**/45deg', false ), + 'trailing comment' => array( '45deg/**/', false ), + 'escape that decodes to a space' => array( '1deg\20 ', false ), + 'axis and angle' => array( 'x 45deg', false ), + 'two angles' => array( '1deg 2deg', false ), + 'calc()' => array( 'calc(10deg + 5deg)', false ), + 'var()' => array( 'var(--angle)', false ), + 'keyword' => array( 'none', false ), + 'unit with a trailing null byte' => array( "1deg\0", false ), + 'empty string' => array( '', false ), + 'integer' => array( 45, false ), + 'null' => array( null, false ), + 'array' => array( array( '45deg' ), false ), + ); + } + + /** + * @ticket 66468 + * @covers ::is_angle + */ + public function test_is_angle_accepts_a_token() { + $processor = new WP_CSS_Token_Processor( '45deg' ); + $processor->next_token(); + + $this->assertTrue( WP_Style_Engine::is_angle( $processor->get_token() ) ); + } + + /** + * @ticket 66467 + * @covers ::is_length_percentage + * @dataProvider data_lengths_and_percentages + * + * @param mixed $value Value to check. + * @param bool $expected Whether the value is a single length or percentage. + */ + public function test_is_length_percentage( $value, $expected ) { + $this->assertSame( $expected, WP_Style_Engine::is_length_percentage( $value ) ); + } + + /** + * @return array + */ + public function data_lengths_and_percentages() { + return array( + 'pixels' => array( '10px', true ), + 'negative percentage' => array( '-50%', true ), + 'leading plus sign' => array( '+.5em', true ), + 'no integer part' => array( '.5em', true ), + 'uppercase unit' => array( '1PX', true ), + 'dynamic viewport unit' => array( '10DVH', true ), + 'small viewport unit' => array( '1svmax', true ), + 'container unit' => array( '2cqi', true ), + 'root font unit' => array( '1rcap', true ), + 'quarter millimeters' => array( '1Q', true ), + 'number 0' => array( '0', true ), + 'exponent' => array( '1e1px', true ), + 'uppercase exponent and unit' => array( '1E1PX', true ), + 'hex escape in unit' => array( '1\70 x', true ), + 'number other than 0' => array( '10', false ), + 'zero with a decimal point' => array( '0.0', false ), + 'angle' => array( '5deg', false ), + 'resolution' => array( '1dppx', false ), + 'flex' => array( '1fr', false ), + 'two lengths' => array( '1px 2px', false ), + 'percentage then length' => array( '50%2px', false ), + 'leading space' => array( ' 1px', false ), + 'leading no-break space' => array( "\u{00A0}1px", false ), + 'fullwidth unit' => array( "1\u{FF50}\u{FF58}", false ), + 'comment' => array( '1px/**/', false ), + 'calc()' => array( 'calc(1px + 2px)', false ), + 'keyword' => array( 'none', false ), + 'empty string' => array( '', false ), + 'integer' => array( 0, false ), + 'null' => array( null, false ), + ); + } + + /** + * The style engine output writes an escaped unit as plain text. + * + * @ticket 66468 + */ + public function test_escaped_unit_is_written_decoded() { + $this->assertSame( + 'rotate:1deg;', + wp_style_engine_get_styles( array( 'transform' => array( 'rotate' => '1\64 eg' ) ) )['css'] + ); + $this->assertSame( + 'translate:1px 0;', + wp_style_engine_get_styles( array( 'transform' => array( 'translate' => array( 'x' => '1\70 x' ) ) ) )['css'] + ); + } +} From 9c9d05355b9896f2ebca296c69ce59b691322ea5 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 17 Nov 2026 16:55:31 +0400 Subject: [PATCH 2/5] Check transform values with WP_CSS_Token methods Replace WP_Style_Engine::is_angle() and is_length_percentage() with WP_CSS_Token::is_angle() and is_length(). Value-type checks belong to the CSS API, which the style engine can build on, so the style engine no longer has public value-type methods or unit tables. A `` check is is_length() or a percentage token, written at each of its three call sites. Callers that hold text, the style engine, WP_Theme_JSON and wp_fill_translate_axes(), need the one token the text is. The private _wp_style_engine_get_single_css_token() returns it, or null when the text is not a string of exactly one token. Leading or trailing whitespace is a token, so values with it are still rejected. The legacy kses path tokenizes its trimmed value for `rotate` as it did for `translate`, and checks for a single angle token. The token methods change two results: - The number 0 is not an angle, so `rotate: 0` is rejected. CSS Values 4 allows a bare 0 angle only in some legacy grammars, and Chrome rejects it for `rotate`. - Every number token with the value zero is a length, so `0.0`, `-0` and `.0` are accepted as `translate` axes, as `0` was. The style engine value tests test wp_style_engine_get_styles() output in place of the removed methods. The CSS API tests cover the token methods. --- src/wp-includes/block-supports/transform.php | 3 +- src/wp-includes/class-wp-theme-json.php | 7 +- src/wp-includes/kses.php | 28 +-- src/wp-includes/style-engine.php | 27 +++ .../style-engine/class-wp-style-engine.php | 196 +----------------- .../style-engine/wpStyleEngineValueTypes.php | 192 +++++++---------- 6 files changed, 132 insertions(+), 321 deletions(-) diff --git a/src/wp-includes/block-supports/transform.php b/src/wp-includes/block-supports/transform.php index 970ae0c271421..f069371e601af 100644 --- a/src/wp-includes/block-supports/transform.php +++ b/src/wp-includes/block-supports/transform.php @@ -60,7 +60,8 @@ function wp_fill_translate_axes( $translate, ...$fallbacks ) { } foreach ( $fallbacks as $fallback ) { $value = is_array( $fallback ) ? ( $fallback[ $axis ] ?? null ) : null; - if ( WP_Style_Engine::is_length_percentage( $value ) ) { + $token = _wp_style_engine_get_single_css_token( $value ); + if ( null !== $token && ( $token->is_length() || WP_CSS_Token_Processor::TOKEN_PERCENTAGE === $token->get_type() ) ) { $translate[ $axis ] = $value; break; } diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index cfb39ed4c7bf3..2021af1beb9e8 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -3292,8 +3292,11 @@ protected static function compute_style_properties( $styles, $settings = array() } // Mirrors the style engine, which outputs a single angle only. - if ( 'rotate' === $css_property && ! WP_Style_Engine::is_angle( $value ) ) { - continue; + if ( 'rotate' === $css_property ) { + $token = _wp_style_engine_get_single_css_token( $value ); + if ( null === $token || ! $token->is_angle() ) { + continue; + } } // Calculates fluid typography rules where available. diff --git a/src/wp-includes/kses.php b/src/wp-includes/kses.php index 0954278b1ff8e..6d8faf2f1f8c0 100644 --- a/src/wp-includes/kses.php +++ b/src/wp-includes/kses.php @@ -4210,19 +4210,19 @@ function _safecss_filter_attr_legacy( $css, $allowed_attr ) { */ $allow_css = 0 === preg_match( '%[\\\(&=}]|/\*%', $css_test_string ); - // Only allow a single angle for `rotate`, mirroring the style engine. - if ( $allow_css && $angle_attr ) { - $allow_css = WP_Style_Engine::is_angle( trim( $parts[1] ) ); - } - - // Only allow one or two lengths or percentages for `translate`, mirroring the style engine. - if ( $allow_css && $translate_attr ) { + /* + * Only allow a single angle for `rotate`, and one or two lengths or + * percentages for `translate`, mirroring the style engine. + */ + if ( $allow_css && ( $angle_attr || $translate_attr ) ) { $processor = new WP_CSS_Token_Processor( trim( $parts[1] ) ); $tokens = array(); while ( $processor->next_token() ) { $tokens[] = $processor->get_token(); } - $allow_css = _safecss_filter_attr_is_translate_value( $tokens ); + $allow_css = $angle_attr + ? 1 === count( $tokens ) && $tokens[0]->is_angle() + : _safecss_filter_attr_is_translate_value( $tokens ); } /** @@ -4277,7 +4277,7 @@ function _safecss_filter_attr_legacy( $css, $allowed_attr ) { * - The value has no `&` delimiter at any depth. The output contains no * `&`: other tokens escape it, and no property value uses a bare `&`. * - The value has no `\` or control character delimiter at any depth. - * - A `rotate` value is a single angle, as WP_Style_Engine::is_angle() + * - A `rotate` value is a single angle, as WP_CSS_Token::is_angle() * checks, and a `translate` value one or two lengths or percentages * separated by whitespace, as _safecss_filter_attr_is_translate_value() * checks. @@ -4339,7 +4339,7 @@ function _safecss_filter_attr_declarations( $css, $allowed_attr ) { } // Only allow a single angle for `rotate`, mirroring the style engine. - if ( 'rotate' === $name && ( 1 !== count( $tokens ) || ! WP_Style_Engine::is_angle( $tokens[0] ) ) ) { + if ( 'rotate' === $name && ( 1 !== count( $tokens ) || ! $tokens[0]->is_angle() ) ) { continue; } @@ -4366,7 +4366,7 @@ function _safecss_filter_attr_declarations( $css, $allowed_attr ) { * @ignore * @internal * - * @see WP_Style_Engine::is_length_percentage() + * @see WP_CSS_Token::is_length() * * @param WP_CSS_Token[] $tokens Value tokens, without leading or trailing whitespace. * @return bool Whether the tokens are an allowed `translate` value. @@ -4385,7 +4385,11 @@ function _safecss_filter_attr_is_translate_value( $tokens ) { } // A second length must follow whitespace, and a third is not allowed. - if ( ! $after_space || ++$count > 2 || ! WP_Style_Engine::is_length_percentage( $token ) ) { + if ( + ! $after_space || + ++$count > 2 || + ! ( $token->is_length() || WP_CSS_Token_Processor::TOKEN_PERCENTAGE === $token->get_type() ) + ) { return false; } $after_space = false; diff --git a/src/wp-includes/style-engine.php b/src/wp-includes/style-engine.php index 835f1c3f1b723..b6288b9cfbb4e 100644 --- a/src/wp-includes/style-engine.php +++ b/src/wp-includes/style-engine.php @@ -198,3 +198,30 @@ function wp_style_engine_get_stylesheet_from_css_rules( $css_rules, $options = a function wp_style_engine_get_stylesheet_from_context( $context, $options = array() ) { return WP_Style_Engine::compile_stylesheet_from_css_rules( WP_Style_Engine::get_store( $context )->get_all_rules(), $options ); } + +/** + * Returns the CSS token a style value is, if the value is exactly one token. + * + * Whitespace and comments are tokens, so a value with whitespace or a + * comment before or after it is not one token. + * + * @since 7.3.0 + * @access private + * @ignore + * + * @param mixed $value Style value. + * @return WP_CSS_Token|null The token, or null when the value is not a string of exactly one token. + */ +function _wp_style_engine_get_single_css_token( $value ): ?WP_CSS_Token { + if ( ! is_string( $value ) ) { + return null; + } + + $processor = new WP_CSS_Token_Processor( $value ); + if ( ! $processor->next_token() ) { + return null; + } + + $token = $processor->get_token(); + return $processor->next_token() ? null : $token; +} diff --git a/src/wp-includes/style-engine/class-wp-style-engine.php b/src/wp-includes/style-engine/class-wp-style-engine.php index c974637ccca7a..a64609978d254 100644 --- a/src/wp-includes/style-engine/class-wp-style-engine.php +++ b/src/wp-includes/style-engine/class-wp-style-engine.php @@ -48,81 +48,6 @@ final class WP_Style_Engine { */ const VALID_BACKGROUND_CLIP_VALUES = array( 'border-box', 'padding-box', 'content-box', 'text' ); - /** - * Angle units, lowercase. - * - * @since 7.3.0 - * @ignore - * - * @link https://www.w3.org/TR/css-values-4/#angles - * - * @var string[] - */ - private const ANGLE_UNITS = array( 'deg', 'grad', 'rad', 'turn' ); - - /** - * Length units, lowercase. - * - * @since 7.3.0 - * @ignore - * - * @link https://www.w3.org/TR/css-values-4/#lengths - * @link https://www.w3.org/TR/css-contain-3/#container-lengths - * - * @var string[] - */ - private const LENGTH_UNITS = array( - 'px', - 'cm', - 'mm', - 'q', - 'in', - 'pt', - 'pc', - 'em', - 'rem', - 'ex', - 'rex', - 'ch', - 'rch', - 'cap', - 'rcap', - 'ic', - 'ric', - 'lh', - 'rlh', - 'vw', - 'vh', - 'vi', - 'vb', - 'vmin', - 'vmax', - 'svw', - 'svh', - 'svi', - 'svb', - 'svmin', - 'svmax', - 'lvw', - 'lvh', - 'lvi', - 'lvb', - 'lvmin', - 'lvmax', - 'dvw', - 'dvh', - 'dvi', - 'dvb', - 'dvmin', - 'dvmax', - 'cqw', - 'cqh', - 'cqi', - 'cqb', - 'cqmin', - 'cqmax', - ); - /** * Style definitions that contain the instructions to parse/output valid Gutenberg styles from a block's attributes. * @@ -951,121 +876,6 @@ protected static function get_background_clip_css_declarations( $style_value, $s return $css_declarations; } - /** - * Checks whether a value is a single CSS angle. - * - * An angle is a dimension token with a `deg`, `grad`, `rad` or `turn` - * unit in any ASCII case, or the number token `0`. A string must - * tokenize as that one token, so whitespace, comments, lists and - * functions such as `calc()` are rejected. - * - * Example: - * - * WP_Style_Engine::is_angle( '-.5turn' ); // true - * WP_Style_Engine::is_angle( '1E2DEG' ); // true - * WP_Style_Engine::is_angle( '45' ); // false - * WP_Style_Engine::is_angle( ' 45deg' ); // false - * - * @since 7.3.0 - * - * @link https://www.w3.org/TR/css-values-4/#angles - * - * @param mixed $value CSS text, or a token read by WP_CSS_Token_Processor. - * @return bool Whether the value is a single angle. - */ - public static function is_angle( $value ): bool { - $token = static::get_single_token( $value ); - if ( null === $token ) { - return false; - } - - if ( WP_CSS_Token_Processor::TOKEN_DIMENSION === $token->get_type() ) { - return in_array( strtolower( (string) $token->get_unit() ), self::ANGLE_UNITS, true ); - } - - return static::is_zero( $token ); - } - - /** - * Checks whether a value is a single CSS length or percentage. - * - * A length is a dimension token with a length unit in any ASCII case, or - * the number token `0`. A percentage is a percentage token. A string must - * tokenize as that one token, so whitespace, comments, lists and - * functions such as `calc()` are rejected. - * - * Example: - * - * WP_Style_Engine::is_length_percentage( '-50%' ); // true - * WP_Style_Engine::is_length_percentage( '10DVH' ); // true - * WP_Style_Engine::is_length_percentage( '10' ); // false - * WP_Style_Engine::is_length_percentage( '5deg' ); // false - * - * @since 7.3.0 - * - * @link https://www.w3.org/TR/css-values-4/#typedef-length-percentage - * - * @param mixed $value CSS text, or a token read by WP_CSS_Token_Processor. - * @return bool Whether the value is a single length or percentage. - */ - public static function is_length_percentage( $value ): bool { - $token = static::get_single_token( $value ); - if ( null === $token ) { - return false; - } - - switch ( $token->get_type() ) { - case WP_CSS_Token_Processor::TOKEN_PERCENTAGE: - return true; - - case WP_CSS_Token_Processor::TOKEN_DIMENSION: - return in_array( strtolower( (string) $token->get_unit() ), self::LENGTH_UNITS, true ); - - default: - return static::is_zero( $token ); - } - } - - /** - * Returns the token a value is, if it is exactly one token. - * - * @since 7.3.0 - * @ignore - * - * @param mixed $value CSS text, or a token read by WP_CSS_Token_Processor. - * @return WP_CSS_Token|null The token, or null when the value is not a string of exactly one token or a token. - */ - private static function get_single_token( $value ): ?WP_CSS_Token { - if ( $value instanceof WP_CSS_Token ) { - return $value; - } - - if ( ! is_string( $value ) ) { - return null; - } - - $processor = new WP_CSS_Token_Processor( $value ); - if ( ! $processor->next_token() ) { - return null; - } - - $token = $processor->get_token(); - return $processor->next_token() ? null : $token; - } - - /** - * Checks whether a token is the number `0`, which is accepted as a zero length or angle. - * - * @since 7.3.0 - * @ignore - * - * @param WP_CSS_Token $token Token. - * @return bool Whether the token is the number `0`. - */ - private static function is_zero( WP_CSS_Token $token ): bool { - return WP_CSS_Token_Processor::TOKEN_NUMBER === $token->get_type() && '0' === $token->get_value(); - } - /** * Style value parser that returns a CSS definition array for a property that takes a single angle. * @@ -1078,7 +888,8 @@ private static function is_zero( WP_CSS_Token $token ): bool { * @return array An associative array of CSS definitions, mapping properties to values. */ protected static function get_angle_css_declarations( $style_value, $style_definition ) { - if ( ! static::is_angle( $style_value ) ) { + $token = _wp_style_engine_get_single_css_token( $style_value ); + if ( null === $token || ! $token->is_angle() ) { return array(); } @@ -1106,7 +917,8 @@ protected static function get_translate_css_declarations( $style_value, $style_d $axes = array(); foreach ( array( 'x', 'y' ) as $axis ) { $axis_value = $style_value[ $axis ] ?? '0'; - if ( ! is_string( $axis_value ) || ! static::is_length_percentage( $axis_value ) ) { + $token = _wp_style_engine_get_single_css_token( $axis_value ); + if ( null === $token || ! ( $token->is_length() || WP_CSS_Token_Processor::TOKEN_PERCENTAGE === $token->get_type() ) ) { return array(); } $axes[] = $axis_value; diff --git a/tests/phpunit/tests/style-engine/wpStyleEngineValueTypes.php b/tests/phpunit/tests/style-engine/wpStyleEngineValueTypes.php index 66c6fba476570..cc161d8671110 100644 --- a/tests/phpunit/tests/style-engine/wpStyleEngineValueTypes.php +++ b/tests/phpunit/tests/style-engine/wpStyleEngineValueTypes.php @@ -8,146 +8,110 @@ */ /** - * Tests for WP_Style_Engine::is_angle() and WP_Style_Engine::is_length_percentage(). + * Tests the `rotate` and `translate` values the style engine outputs. * * @group style-engine * - * @coversDefaultClass WP_Style_Engine + * @covers ::wp_style_engine_get_styles + * @covers ::_wp_style_engine_get_single_css_token */ class Tests_Style_Engine_wpStyleEngineValueTypes extends WP_UnitTestCase { /** * @ticket 66468 - * @covers ::is_angle - * @dataProvider data_angles + * @dataProvider data_rotate_values * - * @param mixed $value Value to check. - * @param bool $expected Whether the value is a single angle. + * @param mixed $value `rotate` value. + * @param string $expected Expected CSS. */ - public function test_is_angle( $value, $expected ) { - $this->assertSame( $expected, WP_Style_Engine::is_angle( $value ) ); + public function test_rotate_value( $value, $expected ) { + $this->assertSame( + $expected, + wp_style_engine_get_styles( array( 'transform' => array( 'rotate' => $value ) ) )['css'] ?? '' + ); } /** - * @return array + * @return array */ - public function data_angles() { + public function data_rotate_values() { return array( - 'degrees' => array( '45deg', true ), - 'negative' => array( '-15deg', true ), - 'leading plus sign' => array( '+1deg', true ), - 'no integer part' => array( '.5turn', true ), - 'negative with no integer part' => array( '-.5turn', true ), - 'radians' => array( '1.5rad', true ), - 'gradians' => array( '100grad', true ), - 'uppercase unit' => array( '1DEG', true ), - 'mixed case unit' => array( '100GrAd', true ), - 'zero with unit' => array( '0deg', true ), - 'number 0' => array( '0', true ), - 'exponent' => array( '1e2deg', true ), - 'uppercase exponent and unit' => array( '1E2DEG', true ), - 'negative exponent' => array( '1e-1turn', true ), - 'hex escape in unit' => array( '1\64 eg', true ), - 'identity escape in unit' => array( '1de\g', true ), - 'number other than 0' => array( '45', false ), - 'zero with a decimal point' => array( '0.0', false ), - 'negative zero' => array( '-0', false ), - 'length' => array( '1px', false ), - 'percentage' => array( '50%', false ), - 'unit after a trailing dot' => array( '1.deg', false ), - 'space before the unit' => array( '45 deg', false ), - 'leading space' => array( ' 45deg', false ), - 'trailing space' => array( '45deg ', false ), - 'trailing newline' => array( "45deg\n", false ), - 'leading comment' => array( '/**/45deg', false ), - 'trailing comment' => array( '45deg/**/', false ), - 'escape that decodes to a space' => array( '1deg\20 ', false ), - 'axis and angle' => array( 'x 45deg', false ), - 'two angles' => array( '1deg 2deg', false ), - 'calc()' => array( 'calc(10deg + 5deg)', false ), - 'var()' => array( 'var(--angle)', false ), - 'keyword' => array( 'none', false ), - 'unit with a trailing null byte' => array( "1deg\0", false ), - 'empty string' => array( '', false ), - 'integer' => array( 45, false ), - 'null' => array( null, false ), - 'array' => array( array( '45deg' ), false ), + 'degrees' => array( '45deg', 'rotate:45deg;' ), + 'negative' => array( '-15deg', 'rotate:-15deg;' ), + 'leading plus sign' => array( '+1deg', 'rotate:+1deg;' ), + 'no integer part' => array( '.5turn', 'rotate:.5turn;' ), + 'radians' => array( '1.5rad', 'rotate:1.5rad;' ), + 'gradians' => array( '100grad', 'rotate:100grad;' ), + 'uppercase unit' => array( '1DEG', 'rotate:1DEG;' ), + 'zero with unit' => array( '0deg', 'rotate:0deg;' ), + 'exponent' => array( '1e2deg', 'rotate:1e2deg;' ), + 'hex escape in unit' => array( '1\64 eg', 'rotate:1deg;' ), + 'identity escape in unit' => array( '1de\g', 'rotate:1deg;' ), + 'number other than 0' => array( '45', '' ), + 'length' => array( '1px', '' ), + 'percentage' => array( '50%', '' ), + 'unit after a trailing dot' => array( '1.deg', '' ), + 'space before the unit' => array( '45 deg', '' ), + 'leading space' => array( ' 45deg', '' ), + 'trailing space' => array( '45deg ', '' ), + 'trailing newline' => array( "45deg\n", '' ), + 'leading comment' => array( '/**/45deg', '' ), + 'trailing comment' => array( '45deg/**/', '' ), + 'escape that decodes to a space' => array( '1deg\20 ', '' ), + 'axis and angle' => array( 'x 45deg', '' ), + 'two angles' => array( '1deg 2deg', '' ), + 'calc()' => array( 'calc(10deg + 5deg)', '' ), + 'var()' => array( 'var(--angle)', '' ), + 'keyword' => array( 'none', '' ), + 'unit with a trailing null byte' => array( "1deg\0", '' ), + 'empty string' => array( '', '' ), + 'integer' => array( 45, '' ), + 'null' => array( null, '' ), + 'array' => array( array( '45deg' ), '' ), ); } - /** - * @ticket 66468 - * @covers ::is_angle - */ - public function test_is_angle_accepts_a_token() { - $processor = new WP_CSS_Token_Processor( '45deg' ); - $processor->next_token(); - - $this->assertTrue( WP_Style_Engine::is_angle( $processor->get_token() ) ); - } - /** * @ticket 66467 - * @covers ::is_length_percentage - * @dataProvider data_lengths_and_percentages + * @dataProvider data_translate_axis_values * - * @param mixed $value Value to check. - * @param bool $expected Whether the value is a single length or percentage. - */ - public function test_is_length_percentage( $value, $expected ) { - $this->assertSame( $expected, WP_Style_Engine::is_length_percentage( $value ) ); - } - - /** - * @return array + * @param mixed $value `translate` x axis value. + * @param string $expected Expected CSS. */ - public function data_lengths_and_percentages() { - return array( - 'pixels' => array( '10px', true ), - 'negative percentage' => array( '-50%', true ), - 'leading plus sign' => array( '+.5em', true ), - 'no integer part' => array( '.5em', true ), - 'uppercase unit' => array( '1PX', true ), - 'dynamic viewport unit' => array( '10DVH', true ), - 'small viewport unit' => array( '1svmax', true ), - 'container unit' => array( '2cqi', true ), - 'root font unit' => array( '1rcap', true ), - 'quarter millimeters' => array( '1Q', true ), - 'number 0' => array( '0', true ), - 'exponent' => array( '1e1px', true ), - 'uppercase exponent and unit' => array( '1E1PX', true ), - 'hex escape in unit' => array( '1\70 x', true ), - 'number other than 0' => array( '10', false ), - 'zero with a decimal point' => array( '0.0', false ), - 'angle' => array( '5deg', false ), - 'resolution' => array( '1dppx', false ), - 'flex' => array( '1fr', false ), - 'two lengths' => array( '1px 2px', false ), - 'percentage then length' => array( '50%2px', false ), - 'leading space' => array( ' 1px', false ), - 'leading no-break space' => array( "\u{00A0}1px", false ), - 'fullwidth unit' => array( "1\u{FF50}\u{FF58}", false ), - 'comment' => array( '1px/**/', false ), - 'calc()' => array( 'calc(1px + 2px)', false ), - 'keyword' => array( 'none', false ), - 'empty string' => array( '', false ), - 'integer' => array( 0, false ), - 'null' => array( null, false ), + public function test_translate_axis_value( $value, $expected ) { + $this->assertSame( + $expected, + wp_style_engine_get_styles( array( 'transform' => array( 'translate' => array( 'x' => $value ) ) ) )['css'] ?? '' ); } /** - * The style engine output writes an escaped unit as plain text. - * - * @ticket 66468 + * @return array */ - public function test_escaped_unit_is_written_decoded() { - $this->assertSame( - 'rotate:1deg;', - wp_style_engine_get_styles( array( 'transform' => array( 'rotate' => '1\64 eg' ) ) )['css'] - ); - $this->assertSame( - 'translate:1px 0;', - wp_style_engine_get_styles( array( 'transform' => array( 'translate' => array( 'x' => '1\70 x' ) ) ) )['css'] + public function data_translate_axis_values() { + return array( + 'pixels' => array( '10px', 'translate:10px 0;' ), + 'negative percentage' => array( '-50%', 'translate:-50% 0;' ), + 'leading plus sign' => array( '+.5em', 'translate:+.5em 0;' ), + 'uppercase unit' => array( '1PX', 'translate:1PX 0;' ), + 'dynamic viewport unit' => array( '10DVH', 'translate:10DVH 0;' ), + 'container unit' => array( '2cqi', 'translate:2cqi 0;' ), + 'quarter millimeters' => array( '1Q', 'translate:1Q 0;' ), + 'number 0' => array( '0', 'translate:0 0;' ), + 'exponent' => array( '1e1px', 'translate:1e1px 0;' ), + 'hex escape in unit' => array( '1\70 x', 'translate:1px 0;' ), + 'number other than 0' => array( '10', '' ), + 'angle' => array( '5deg', '' ), + 'resolution' => array( '1dppx', '' ), + 'two lengths' => array( '1px 2px', '' ), + 'percentage then length' => array( '50%2px', '' ), + 'leading space' => array( ' 1px', '' ), + 'leading no-break space' => array( "\u{00A0}1px", '' ), + 'fullwidth unit' => array( "1\u{FF50}\u{FF58}", '' ), + 'comment' => array( '1px/**/', '' ), + 'calc()' => array( 'calc(1px + 2px)', '' ), + 'keyword' => array( 'none', '' ), + 'empty string' => array( '', '' ), + 'integer' => array( 0, '' ), ); } } From 70debc48f454a9c8380665d0ffd3d3f91bbe38b2 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 17 Nov 2026 16:56:13 +0400 Subject: [PATCH 3/5] Tests: Expect `rotate: 0` to be rejected WP_CSS_Token::is_angle() does not accept the number 0. CSS Values 4 allows a bare 0 angle only in some legacy grammars, and says future uses of `` will not allow it: https://drafts.csswg.org/css-values-4/#angles. Chrome 157 reports CSS.supports( 'rotate', '0' ) as false and CSS.supports( 'rotate', '0deg' ) as true. The kses data case for `rotate: 0` now expects empty output. The block states and theme.json tests that output a zero rotation under a media query use `0deg`, so they still test the media query output. New cases check that the style engine, theme.json and both kses paths reject the number 0 for `rotate`. --- tests/phpunit/tests/block-supports/states.php | 4 ++-- tests/phpunit/tests/kses.php | 2 +- tests/phpunit/tests/kses/safecssFilterAttr.php | 1 + tests/phpunit/tests/style-engine/wpStyleEngineValueTypes.php | 3 +++ tests/phpunit/tests/theme/wpThemeJson.php | 5 +++-- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/tests/phpunit/tests/block-supports/states.php b/tests/phpunit/tests/block-supports/states.php index 557c218406972..560caa1f48e0c 100644 --- a/tests/phpunit/tests/block-supports/states.php +++ b/tests/phpunit/tests/block-supports/states.php @@ -1792,8 +1792,8 @@ public function data_rotation_state_generates_rotate_css() { 'viewport' => array( 'test/transform-state', '
Caption
', - array( '@mobile' => array( 'transform' => array( 'rotate' => '0' ) ) ), - '@media (width <= 480px){.%s img{rotate:0 !important;}}', + array( '@mobile' => array( 'transform' => array( 'rotate' => '0deg' ) ) ), + '@media (width <= 480px){.%s img{rotate:0deg !important;}}', ), ); } diff --git a/tests/phpunit/tests/kses.php b/tests/phpunit/tests/kses.php index a3083ce48cb17..5fafca68bdbed 100644 --- a/tests/phpunit/tests/kses.php +++ b/tests/phpunit/tests/kses.php @@ -2027,7 +2027,7 @@ public static function data_safecss_filter_attr() { ), array( 'css' => 'rotate: 0', - 'expected' => 'rotate: 0', + 'expected' => '', ), array( 'css' => 'rotate: 45', diff --git a/tests/phpunit/tests/kses/safecssFilterAttr.php b/tests/phpunit/tests/kses/safecssFilterAttr.php index 08e4f76b6ef83..38ca021364095 100644 --- a/tests/phpunit/tests/kses/safecssFilterAttr.php +++ b/tests/phpunit/tests/kses/safecssFilterAttr.php @@ -521,6 +521,7 @@ public function data_transform_values() { 'rotate with a comment in the number' => array( 'rotate: 4/**/5deg', '', '' ), 'rotate with important' => array( 'rotate: 45deg !important', 'rotate: 45deg !important;', '' ), 'rotate with a space before the unit' => array( 'rotate: 45 deg', '', '' ), + 'rotate with the number 0' => array( 'rotate: 0', '', '' ), 'rotate with zero written 0.0' => array( 'rotate: 0.0', '', '' ), 'translate with an uppercase unit' => array( 'translate: 1PX 50%', 'translate: 1PX 50%;', 'translate: 1PX 50%' ), 'translate with an exponent' => array( 'translate: 1e1px', 'translate: 1e1px;', 'translate: 1e1px' ), diff --git a/tests/phpunit/tests/style-engine/wpStyleEngineValueTypes.php b/tests/phpunit/tests/style-engine/wpStyleEngineValueTypes.php index cc161d8671110..6d6218ac5fe91 100644 --- a/tests/phpunit/tests/style-engine/wpStyleEngineValueTypes.php +++ b/tests/phpunit/tests/style-engine/wpStyleEngineValueTypes.php @@ -46,6 +46,9 @@ public function data_rotate_values() { 'exponent' => array( '1e2deg', 'rotate:1e2deg;' ), 'hex escape in unit' => array( '1\64 eg', 'rotate:1deg;' ), 'identity escape in unit' => array( '1de\g', 'rotate:1deg;' ), + 'number 0' => array( '0', '' ), + 'zero with a decimal point' => array( '0.0', '' ), + 'negative zero' => array( '-0', '' ), 'number other than 0' => array( '45', '' ), 'length' => array( '1px', '' ), 'percentage' => array( '50%', '' ), diff --git a/tests/phpunit/tests/theme/wpThemeJson.php b/tests/phpunit/tests/theme/wpThemeJson.php index e8a9f5af0c8f8..f339e4886f929 100644 --- a/tests/phpunit/tests/theme/wpThemeJson.php +++ b/tests/phpunit/tests/theme/wpThemeJson.php @@ -3233,7 +3233,7 @@ public function test_get_stylesheet_outputs_rotate_for_block_styles_states_and_v 'test/rotate-feature' => array( 'transform' => array( 'rotate' => '0.25turn' ), '@mobile' => array( - 'transform' => array( 'rotate' => '0' ), + 'transform' => array( 'rotate' => '0deg' ), ), 'variations' => array( 'tilted' => array( @@ -3269,7 +3269,7 @@ public function test_get_stylesheet_outputs_rotate_for_block_styles_states_and_v unregister_block_type( 'test/rotate-feature' ); $this->assertStringContainsString( ':root :where(.wp-block-test-rotate-feature img){rotate: 0.25turn;}', $css ); - $this->assertStringContainsString( '@media (width <= 480px){:root :where(.wp-block-test-rotate-feature img){rotate: 0;}}', $css ); + $this->assertStringContainsString( '@media (width <= 480px){:root :where(.wp-block-test-rotate-feature img){rotate: 0deg;}}', $css ); $this->assertStringContainsString( ':root :where(.wp-block-test-rotate-feature.is-style-tilted img){rotate: -5deg;}', $css ); $this->assertStringContainsString( ':root :where(.wp-block-button .wp-block-button__link:hover){rotate: 3deg;}', $css ); $this->assertStringContainsString( '@media (width <= 480px){:root :where(.wp-block-button .wp-block-button__link:hover){rotate: 6deg;}}', $css ); @@ -3311,6 +3311,7 @@ public function test_get_stylesheet_skips_invalid_rotate_values( $rotate ) { public function data_get_stylesheet_skips_invalid_rotate_values() { return array( 'bare number' => array( '45' ), + 'number 0' => array( '0' ), 'calc()' => array( 'calc(10deg + 5deg)' ), 'var()' => array( 'var(--angle)' ), 'axis list' => array( 'x 45deg' ), From d12b49128c636e47de724a6d4280a4e69a6f07a7 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 17 Nov 2026 16:56:58 +0400 Subject: [PATCH 4/5] Tests: Accept zero lengths written other than `0` WP_CSS_Token::is_length() accepts every number token with the value zero, as CSS Values 4 allows a zero length without a unit: https://drafts.csswg.org/css-values-4/#lengths. Chrome 157 reports CSS.supports( 'translate', v ) as true for `0.0`, `-0`, `+0`, `.0` and `0e3`. The kses case for `translate: 0.0` now expects the declaration on both paths. New cases cover the other zero forms in kses, the style engine and the block supports axis fill, and check that `0.1` is still rejected. --- .../block-supports/wpRenderTransformSupport.php | 17 +++++++++++++++++ tests/phpunit/tests/kses/safecssFilterAttr.php | 5 ++++- .../style-engine/wpStyleEngineValueTypes.php | 6 ++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/block-supports/wpRenderTransformSupport.php b/tests/phpunit/tests/block-supports/wpRenderTransformSupport.php index 5550622d01a3a..f047e17cfd330 100644 --- a/tests/phpunit/tests/block-supports/wpRenderTransformSupport.php +++ b/tests/phpunit/tests/block-supports/wpRenderTransformSupport.php @@ -330,6 +330,23 @@ public function test_translation_axis_is_not_filled_from_an_invalid_global_style ); } + /** + * A zero written with a decimal point and sign is a valid axis to fill from. + * + * @ticket 66467 + */ + public function test_translation_axis_is_filled_from_a_zero_global_styles_axis() { + $this->register_transform_block( array( 'translate' => true ) ); + $this->set_global_block_styles( + array( 'transform' => array( 'translate' => array( 'x' => '-0.0' ) ) ) + ); + + $this->assertSame( + '
', + $this->render_transformed_block( array( 'translate' => array( 'y' => '1rem' ) ) ) + ); + } + /** * @ticket 66467 * diff --git a/tests/phpunit/tests/kses/safecssFilterAttr.php b/tests/phpunit/tests/kses/safecssFilterAttr.php index 38ca021364095..89c40741fa261 100644 --- a/tests/phpunit/tests/kses/safecssFilterAttr.php +++ b/tests/phpunit/tests/kses/safecssFilterAttr.php @@ -537,7 +537,10 @@ public function data_transform_values() { 'translate with no space after a percentage' => array( 'translate: 50%2px', '', '' ), 'translate with a comma' => array( 'translate: 1px, 2px', '', '' ), 'translate with three lengths' => array( 'translate: 1px 2px 3px', '', '' ), - 'translate with zero written 0.0' => array( 'translate: 0.0', '', '' ), + 'translate with zero written 0.0' => array( 'translate: 0.0', 'translate: 0.0;', 'translate: 0.0' ), + 'translate with negative zero' => array( 'translate: -0 +0', 'translate: -0 +0;', 'translate: -0 +0' ), + 'translate with zero with no integer part' => array( 'translate: .0 0e3', 'translate: .0 0e3;', 'translate: .0 0e3' ), + 'translate with a nonzero number' => array( 'translate: 0.1', '', '' ), 'translate with no value' => array( 'translate:', '', '' ), ); } diff --git a/tests/phpunit/tests/style-engine/wpStyleEngineValueTypes.php b/tests/phpunit/tests/style-engine/wpStyleEngineValueTypes.php index 6d6218ac5fe91..4c0c265161b33 100644 --- a/tests/phpunit/tests/style-engine/wpStyleEngineValueTypes.php +++ b/tests/phpunit/tests/style-engine/wpStyleEngineValueTypes.php @@ -100,6 +100,12 @@ public function data_translate_axis_values() { 'container unit' => array( '2cqi', 'translate:2cqi 0;' ), 'quarter millimeters' => array( '1Q', 'translate:1Q 0;' ), 'number 0' => array( '0', 'translate:0 0;' ), + 'zero with a decimal' => array( '0.0', 'translate:0.0 0;' ), + 'negative zero' => array( '-0', 'translate:-0 0;' ), + 'zero with a plus sign' => array( '+0', 'translate:+0 0;' ), + 'zero with no integer' => array( '.0', 'translate:.0 0;' ), + 'zero with an exponent' => array( '0e3', 'translate:0e3 0;' ), + 'number 0.1' => array( '0.1', '' ), 'exponent' => array( '1e1px', 'translate:1e1px 0;' ), 'hex escape in unit' => array( '1\70 x', 'translate:1px 0;' ), 'number other than 0' => array( '10', '' ), From b55df1246823033d118acf9f28f50b33c41d5936 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Wed, 18 Nov 2026 11:50:03 +0400 Subject: [PATCH 5/5] Style Engine: Check rotate and translate values with parse_single_token(). WP_CSS_Token_Processor::parse_single_token() accepts exactly one token and rejects whitespace or comments around it, as the private _wp_style_engine_get_single_css_token() did, so the helper is removed. Callers pass only strings to it. No test expectations change. --- src/wp-includes/block-supports/transform.php | 2 +- src/wp-includes/class-wp-theme-json.php | 2 +- src/wp-includes/style-engine.php | 27 ------------------- .../style-engine/class-wp-style-engine.php | 4 +-- .../style-engine/wpStyleEngineValueTypes.php | 2 +- 5 files changed, 5 insertions(+), 32 deletions(-) diff --git a/src/wp-includes/block-supports/transform.php b/src/wp-includes/block-supports/transform.php index f069371e601af..565d518ff4678 100644 --- a/src/wp-includes/block-supports/transform.php +++ b/src/wp-includes/block-supports/transform.php @@ -60,7 +60,7 @@ function wp_fill_translate_axes( $translate, ...$fallbacks ) { } foreach ( $fallbacks as $fallback ) { $value = is_array( $fallback ) ? ( $fallback[ $axis ] ?? null ) : null; - $token = _wp_style_engine_get_single_css_token( $value ); + $token = is_string( $value ) ? WP_CSS_Token_Processor::parse_single_token( $value ) : null; if ( null !== $token && ( $token->is_length() || WP_CSS_Token_Processor::TOKEN_PERCENTAGE === $token->get_type() ) ) { $translate[ $axis ] = $value; break; diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 2021af1beb9e8..23e8bc67e4434 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -3293,7 +3293,7 @@ protected static function compute_style_properties( $styles, $settings = array() // Mirrors the style engine, which outputs a single angle only. if ( 'rotate' === $css_property ) { - $token = _wp_style_engine_get_single_css_token( $value ); + $token = is_string( $value ) ? WP_CSS_Token_Processor::parse_single_token( $value ) : null; if ( null === $token || ! $token->is_angle() ) { continue; } diff --git a/src/wp-includes/style-engine.php b/src/wp-includes/style-engine.php index b6288b9cfbb4e..835f1c3f1b723 100644 --- a/src/wp-includes/style-engine.php +++ b/src/wp-includes/style-engine.php @@ -198,30 +198,3 @@ function wp_style_engine_get_stylesheet_from_css_rules( $css_rules, $options = a function wp_style_engine_get_stylesheet_from_context( $context, $options = array() ) { return WP_Style_Engine::compile_stylesheet_from_css_rules( WP_Style_Engine::get_store( $context )->get_all_rules(), $options ); } - -/** - * Returns the CSS token a style value is, if the value is exactly one token. - * - * Whitespace and comments are tokens, so a value with whitespace or a - * comment before or after it is not one token. - * - * @since 7.3.0 - * @access private - * @ignore - * - * @param mixed $value Style value. - * @return WP_CSS_Token|null The token, or null when the value is not a string of exactly one token. - */ -function _wp_style_engine_get_single_css_token( $value ): ?WP_CSS_Token { - if ( ! is_string( $value ) ) { - return null; - } - - $processor = new WP_CSS_Token_Processor( $value ); - if ( ! $processor->next_token() ) { - return null; - } - - $token = $processor->get_token(); - return $processor->next_token() ? null : $token; -} diff --git a/src/wp-includes/style-engine/class-wp-style-engine.php b/src/wp-includes/style-engine/class-wp-style-engine.php index a64609978d254..86a1e68aa3e4b 100644 --- a/src/wp-includes/style-engine/class-wp-style-engine.php +++ b/src/wp-includes/style-engine/class-wp-style-engine.php @@ -888,7 +888,7 @@ protected static function get_background_clip_css_declarations( $style_value, $s * @return array An associative array of CSS definitions, mapping properties to values. */ protected static function get_angle_css_declarations( $style_value, $style_definition ) { - $token = _wp_style_engine_get_single_css_token( $style_value ); + $token = is_string( $style_value ) ? WP_CSS_Token_Processor::parse_single_token( $style_value ) : null; if ( null === $token || ! $token->is_angle() ) { return array(); } @@ -917,7 +917,7 @@ protected static function get_translate_css_declarations( $style_value, $style_d $axes = array(); foreach ( array( 'x', 'y' ) as $axis ) { $axis_value = $style_value[ $axis ] ?? '0'; - $token = _wp_style_engine_get_single_css_token( $axis_value ); + $token = is_string( $axis_value ) ? WP_CSS_Token_Processor::parse_single_token( $axis_value ) : null; if ( null === $token || ! ( $token->is_length() || WP_CSS_Token_Processor::TOKEN_PERCENTAGE === $token->get_type() ) ) { return array(); } diff --git a/tests/phpunit/tests/style-engine/wpStyleEngineValueTypes.php b/tests/phpunit/tests/style-engine/wpStyleEngineValueTypes.php index 4c0c265161b33..321701ebe5d99 100644 --- a/tests/phpunit/tests/style-engine/wpStyleEngineValueTypes.php +++ b/tests/phpunit/tests/style-engine/wpStyleEngineValueTypes.php @@ -13,7 +13,7 @@ * @group style-engine * * @covers ::wp_style_engine_get_styles - * @covers ::_wp_style_engine_get_single_css_token + * @covers WP_CSS_Token_Processor::parse_single_token */ class Tests_Style_Engine_wpStyleEngineValueTypes extends WP_UnitTestCase { /**