Skip to content

Commit a133b6d

Browse files
KSES: Allow assigning values to CSS variables.
The `safecss_filter_attr()` function allows using custom CSS variables like `color: var(--color)`. However, it did not allow assigning values to CSS variables like `--color: #F00`, which is common in Global Styles and Gutenberg. This commit adds support for assigning values to CSS variables, so that the function can be used consistently in Global Styles and the future Style Engine in Gutenberg. Follow-up to [50923], [54100]. Props aristath, ramonopoly, SergeyBiryukov. Fixes #56353. git-svn-id: https://develop.svn.wordpress.org/trunk@54117 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 2a8e205 commit a133b6d

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/wp-includes/kses.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -2229,7 +2229,7 @@ function kses_init() {
22292229
* @since 5.7.1 Added support for `object-position`.
22302230
* @since 5.8.0 Added support for `calc()` and `var()` values.
22312231
* @since 6.1.0 Added support for `min()`, `max()`, `minmax()`, `clamp()`,
2232-
* and nested `var()` values.
2232+
* nested `var()` values, and assigning values to CSS variables.
22332233
* Added support for `gap`, `column-gap`, `row-gap`, and `flex-wrap`.
22342234
* Extended `margin-*` and `padding-*` support for logical properties.
22352235
*
@@ -2391,6 +2391,9 @@ function safecss_filter_attr( $css, $deprecated = '' ) {
23912391
'object-position',
23922392
'overflow',
23932393
'vertical-align',
2394+
2395+
// Custom CSS properties.
2396+
'--*',
23942397
)
23952398
);
23962399

@@ -2436,18 +2439,31 @@ function safecss_filter_attr( $css, $deprecated = '' ) {
24362439
$found = false;
24372440
$url_attr = false;
24382441
$gradient_attr = false;
2442+
$is_custom_var = false;
24392443

24402444
if ( strpos( $css_item, ':' ) === false ) {
24412445
$found = true;
24422446
} else {
24432447
$parts = explode( ':', $css_item, 2 );
24442448
$css_selector = trim( $parts[0] );
24452449

2450+
// Allow assigning values to CSS variables.
2451+
if ( in_array( '--*', $allowed_attr, true ) && preg_match( '/^--[a-zA-Z0-9-_]+$/', $css_selector ) ) {
2452+
$allowed_attr[] = $css_selector;
2453+
$is_custom_var = true;
2454+
}
2455+
24462456
if ( in_array( $css_selector, $allowed_attr, true ) ) {
24472457
$found = true;
24482458
$url_attr = in_array( $css_selector, $css_url_data_types, true );
24492459
$gradient_attr = in_array( $css_selector, $css_gradient_data_types, true );
24502460
}
2461+
2462+
if ( $is_custom_var ) {
2463+
$css_value = trim( $parts[1] );
2464+
$url_attr = str_starts_with( $css_value, 'url(' );
2465+
$gradient_attr = str_contains( $css_value, '-gradient(' );
2466+
}
24512467
}
24522468

24532469
if ( $found && $url_attr ) {

tests/phpunit/tests/kses.php

+17
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,23 @@ public function data_test_safecss_filter_attr() {
12471247
'css' => 'margin-block-start: 1px;margin-block-end: 2px;margin-inline-start: 3px;margin-inline-end: 4px;padding-block-start: 1px;padding-block-end: 2px;padding-inline-start: 3px;padding-inline-end: 4px',
12481248
'expected' => 'margin-block-start: 1px;margin-block-end: 2px;margin-inline-start: 3px;margin-inline-end: 4px;padding-block-start: 1px;padding-block-end: 2px;padding-inline-start: 3px;padding-inline-end: 4px',
12491249
),
1250+
// Assigning values to CSS variables introduced in 6.1.
1251+
array(
1252+
'css' => '--wp--medium-width: 100px; --var_with_underscores: #cccccc;',
1253+
'expected' => '--wp--medium-width: 100px;--var_with_underscores: #cccccc',
1254+
),
1255+
array(
1256+
'css' => '--miXeD-CAse: red; --with-numbers-3_56: red; --with-url-value: url("foo.jpg");',
1257+
'expected' => '--miXeD-CAse: red;--with-numbers-3_56: red;--with-url-value: url("foo.jpg")',
1258+
),
1259+
array(
1260+
'css' => '--with-gradient: repeating-linear-gradient(135deg,rgba(6,147,227,1) 0%,rgb(155,81,224) 100%);',
1261+
'expected' => '--with-gradient: repeating-linear-gradient(135deg,rgba(6,147,227,1) 0%,rgb(155,81,224) 100%)',
1262+
),
1263+
array(
1264+
'css' => '--?><.%-not-allowed: red;',
1265+
'expected' => '',
1266+
),
12501267
);
12511268
}
12521269

0 commit comments

Comments
 (0)