diff --git a/src/wp-includes/block-supports/transform.php b/src/wp-includes/block-supports/transform.php index 72f6fd08622dc..565d518ff4678 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 ( is_string( $value ) && preg_match( WP_Style_Engine::VALID_LENGTH_PERCENTAGE_PATTERN, $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 27d1a31d74935..23e8bc67e4434 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 && ( ! is_string( $value ) || ! preg_match( WP_Style_Engine::VALID_ANGLE_PATTERN, $value ) ) ) { - continue; + if ( 'rotate' === $css_property ) { + $token = is_string( $value ) ? WP_CSS_Token_Processor::parse_single_token( $value ) : null; + 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 82c375da4dded..6d8faf2f1f8c0 100644 --- a/src/wp-includes/kses.php +++ b/src/wp-includes/kses.php @@ -4210,20 +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 = 1 === preg_match( WP_Style_Engine::VALID_ANGLE_PATTERN, 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; - } + /* + * 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 = $angle_attr + ? 1 === count( $tokens ) && $tokens[0]->is_angle() + : _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_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. * * 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 ) || ! $tokens[0]->is_angle() ) ) { 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,48 @@ 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_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. + */ +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 || + ! ( $token->is_length() || WP_CSS_Token_Processor::TOKEN_PERCENTAGE === $token->get_type() ) + ) { + 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..86a1e68aa3e4b 100644 --- a/src/wp-includes/style-engine/class-wp-style-engine.php +++ b/src/wp-includes/style-engine/class-wp-style-engine.php @@ -48,22 +48,6 @@ 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`. - * - * @since 7.3.0 - * @var string - */ - const VALID_ANGLE_PATTERN = '/^(?:[+-]?(?:\d+(?:\.\d+)?|\.\d+)(?:deg|grad|rad|turn)|0)$/iD'; - - /** - * Matches a single CSS length or percentage: a number with a length unit or `%`, or `0`. - * - * @since 7.3.0 - * @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'; - /** * Style definitions that contain the instructions to parse/output valid Gutenberg styles from a block's attributes. * @@ -904,7 +888,8 @@ 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 ) ) { + $token = is_string( $style_value ) ? WP_CSS_Token_Processor::parse_single_token( $style_value ) : null; + if ( null === $token || ! $token->is_angle() ) { return array(); } @@ -932,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 ) || ! preg_match( static::VALID_LENGTH_PERCENTAGE_PATTERN, $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(); } $axes[] = $axis_value; 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/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.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 584231a802368..89c40741fa261 100644 --- a/tests/phpunit/tests/kses/safecssFilterAttr.php +++ b/tests/phpunit/tests/kses/safecssFilterAttr.php @@ -483,6 +483,68 @@ 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 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' ), + '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: 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:', '', '' ), + ); + } + /** * @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..321701ebe5d99 --- /dev/null +++ b/tests/phpunit/tests/style-engine/wpStyleEngineValueTypes.php @@ -0,0 +1,126 @@ +assertSame( + $expected, + wp_style_engine_get_styles( array( 'transform' => array( 'rotate' => $value ) ) )['css'] ?? '' + ); + } + + /** + * @return array + */ + public function data_rotate_values() { + return array( + '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 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%', '' ), + '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 66467 + * @dataProvider data_translate_axis_values + * + * @param mixed $value `translate` x axis value. + * @param string $expected Expected CSS. + */ + public function test_translate_axis_value( $value, $expected ) { + $this->assertSame( + $expected, + wp_style_engine_get_styles( array( 'transform' => array( 'translate' => array( 'x' => $value ) ) ) )['css'] ?? '' + ); + } + + /** + * @return array + */ + 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;' ), + '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', '' ), + '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, '' ), + ); + } +} 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' ),