Skip to content

Commit

Permalink
Duotone: Use the style engine to generate CSS for Duotone (#48281)
Browse files Browse the repository at this point in the history
* Duotone: Use the style engine to generate CSS for Duotone

* filter for kses to allow filter css rule with url if url is of wp-duotone format

* comment for how style engine call works and match expression for filter in css string

* Update lib/block-supports/duotone.php

---------

Co-authored-by: Andrei Draganescu <andrei.draganescu@automattic.com>
  • Loading branch information
scruffian and draganescu committed Feb 22, 2023
1 parent e0a413d commit 2fbe9a0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
29 changes: 20 additions & 9 deletions lib/block-supports/duotone.php
Original file line number Diff line number Diff line change
Expand Up @@ -462,15 +462,26 @@ function gutenberg_render_duotone_support( $block_content, $block ) {
}
$selector = implode( ', ', $scoped );

// !important is needed because these styles render before global styles,
// and they should be overriding the duotone filters set by global styles.
$filter_style = SCRIPT_DEBUG
? $selector . " {\n\tfilter: " . $filter_property . " !important;\n}\n"
: $selector . '{filter:' . $filter_property . ' !important;}';

wp_register_style( $filter_id, false, array(), true, true );
wp_add_inline_style( $filter_id, $filter_style );
wp_enqueue_style( $filter_id );
// Calling gutenberg_style_engine_get_stylesheet_from_css_rules ensures that
// the styles are rendered in an inline for block supports because we're
// using the `context` option to instruct it so.
gutenberg_style_engine_get_stylesheet_from_css_rules(
array(
array(
'selector' => $selector,
'declarations' => array(
// !important is needed because these styles
// render before global styles,
// and they should be overriding the duotone
// filters set by global styles.
'filter' => $filter_property . ' !important',
),
),
),
array(
'context' => 'block-supports',
)
);

if ( 'unset' !== $colors ) {
$filter_svg = gutenberg_get_duotone_filter_svg( $filter_preset );
Expand Down
22 changes: 22 additions & 0 deletions lib/experimental/kses.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,25 @@ function gutenberg_override_core_kses_init_filters() {
// The 'kses_init_filters' is usually initialized with default priority. Use higher priority to override.
add_action( 'init', 'gutenberg_override_core_kses_init_filters', 20 );
add_action( 'set_current_user', 'gutenberg_override_core_kses_init_filters' );

/**
* See https://github.com/WordPress/wordpress-develop/pull/4108
*
* Mark CSS safe if it contains a "filter: url('#wp-duotone-...')" rule.
*
* This function should not be backported to core.
*
* @param bool $allow_css Whether the CSS is allowed.
* @param string $css_test_string The CSS to test.
*/
function allow_filter_in_styles( $allow_css, $css_test_string ) {
if ( preg_match(
"/^filter: url\('#wp-duotone-[-a-zA-Z0-9]+'\) \!important$/",
$css_test_string
) ) {
return true;
}
return $allow_css;
}

add_filter( 'safecss_filter_attr_allow_css', 'allow_filter_in_styles', 10, 2 );

1 comment on commit 2fbe9a0

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 2fbe9a0.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4242917902
📝 Reported issues:

Please sign in to comment.