Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/wp-includes/block-supports/transform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
7 changes: 5 additions & 2 deletions src/wp-includes/class-wp-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
88 changes: 61 additions & 27 deletions src/wp-includes/kses.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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.
*
Expand Down
22 changes: 4 additions & 18 deletions src/wp-includes/style-engine/class-wp-style-engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -904,7 +888,8 @@ protected static function get_background_clip_css_declarations( $style_value, $s
* @return array<string, string> 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();
}

Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/tests/block-supports/states.php
Original file line number Diff line number Diff line change
Expand Up @@ -1792,8 +1792,8 @@ public function data_rotation_state_generates_rotate_css() {
'viewport' => array(
'test/transform-state',
'<figure class="wp-block-test-transform-state"><img src="x.png" /><figcaption>Caption</figcaption></figure>',
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;}}',
),
);
}
Expand Down
17 changes: 17 additions & 0 deletions tests/phpunit/tests/block-supports/wpRenderTransformSupport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'<div class="wp-block-test-transform-block" style="color:red;translate:-0.0 1rem;"><img src="x.png" /></div>',
$this->render_transformed_block( array( 'translate' => array( 'y' => '1rem' ) ) )
);
}

/**
* @ticket 66467
*
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/kses.php
Original file line number Diff line number Diff line change
Expand Up @@ -2027,7 +2027,7 @@ public static function data_safecss_filter_attr() {
),
array(
'css' => 'rotate: 0',
'expected' => 'rotate: 0',
'expected' => '',
),
array(
'css' => 'rotate: 45',
Expand Down
62 changes: 62 additions & 0 deletions tests/phpunit/tests/kses/safecssFilterAttr.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, array{string, string, string}>
*/
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
*/
Expand Down
Loading