Skip to content

Commit

Permalink
Rebasedw
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonjd committed Jul 5, 2022
1 parent 0918a1e commit 365a6dd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
19 changes: 16 additions & 3 deletions packages/style-engine/class-wp-style-engine-css-declarations.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ class WP_Style_Engine_CSS_Declarations {
protected $styles = array();

/**
* Contructor for this object.
* A white list of CSS custom properties.
* Used to bypass safecss_filter_attr().
*/
const VALID_CUSTOM_PROPERTIES = array( '--wp--style--block-gap' => 'blockGap' );

/**
* Constructor for this object.
*
* If a `$styles` array is passed, it will be used to populate
* the initial $styles prop of the object by calling add_declarations().
Expand Down Expand Up @@ -92,15 +98,22 @@ public function get_styles() {
/**
* Get the CSS styles.
*
* @param boolean $should_prettify Whether to format the output with indents.
*
* @return string The CSS styles.
*/
public function get_styles_string() {
public function get_styles_string( $should_prettify = false ) {
$styles_array = $this->get_styles();
$styles = '';
$indent = $should_prettify ? "\t" : '';
$newline = $should_prettify ? "\n" : ' ';
foreach ( $styles_array as $property => $value ) {
$css = esc_html( safecss_filter_attr( "{$property}: {$value}" ) );
// @TODO The safecss_filter_attr_allow_css filter in safecss_filter_attr (kses.php) does not let through CSS custom variables.
// So we have to be strict about them here.
$css = isset( static::VALID_CUSTOM_PROPERTIES[ $property ] ) ? esc_html( "{$property}: {$value}" ) : esc_html( safecss_filter_attr( "{$property}: {$value}" ) );
if ( $css ) {
$styles .= $css . '; ';
$styles .= "$indent$css;$newline";
}
}
return rtrim( $styles );
Expand Down
15 changes: 5 additions & 10 deletions packages/style-engine/class-wp-style-engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ class WP_Style_Engine {
*/
private static $instance = null;

/**
* A white list of CSS custom properties.
* Used to bypass safecss_filter_attr().
*/
const VALID_CUSTOM_PROPERTIES = array(
'--wp--style--block-gap' => array( 'spacing', 'blockGap' ),
);

/**
* Style definitions that contain the instructions to
* parse/output valid Gutenberg styles from a block's attributes.
Expand Down Expand Up @@ -484,14 +476,17 @@ public function generate( $block_styles, $options ) {

// The return object.
$styles_output = array();
$css = $style_rules->get_styles_string();
$css = $style_rules->get_styles_string( $should_prettify );

// Return css, if any.
if ( ! empty( $css ) ) {
$styles_output['css'] = $css;
// Return an entire rule if there is a selector.
if ( $css_selector ) {
$styles_output['css'] = $css_selector . ' { ' . $css . ' }';
$new_line = $should_prettify ? "\n" : '';
$spacer = $should_prettify ? '' : ' ';
// @TODO this is just weird. Simplify?
$styles_output['css'] = "$css_selector {{$spacer}{$new_line}{$css}{$new_line}{$spacer}}$new_line";
}
}

Expand Down

0 comments on commit 365a6dd

Please sign in to comment.