Skip to content

Commit

Permalink
Testing adding button element and transition states.
Browse files Browse the repository at this point in the history
Do not merge!
This commit has highlighted the need to possibly allow certain CSS props via safecss_filter_attr
  • Loading branch information
ramonjd committed Jun 9, 2022
1 parent 61f00f6 commit e33be11
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 40 deletions.
36 changes: 30 additions & 6 deletions packages/style-engine/class-wp-style-engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ class WP_Style_Engine {
'default' => 'background-color',
),
'path' => array( 'color', 'background' ),
'css_vars' => array(
'--wp--preset--color--$slug' => 'color',
),
'classnames' => array(
'has-background' => true,
'has-$slug-background-color' => 'color',
Expand Down Expand Up @@ -141,8 +144,16 @@ class WP_Style_Engine {
),
),
),
'effects' => array(
'transition' => array(
'property_keys' => array(
'default' => 'transition',
),
'path' => array( 'effects', 'transition' ),
),
),
'elements' => array(
'link' => array(
'link' => array(
'path' => array( 'elements', 'link' ),
'selector' => 'a',
'states' => array(
Expand All @@ -152,6 +163,16 @@ class WP_Style_Engine {
),
),
),
'button' => array(
'path' => array( 'elements', 'button' ),
'selector' => 'button',
'states' => array(
'hover' => array(
'path' => array( 'elements', 'button', 'states', 'hover' ),
'selector' => 'button:hover',
),
),
),
),
'spacing' => array(
'padding' => array(
Expand Down Expand Up @@ -393,7 +414,7 @@ public function generate( $block_styles, $options ) {
// Elements are a special case: we need to define styles on a per-element basis using the element's selector.
// And we also need to combine selectors.
if ( array_key_exists( 'elements', $block_styles ) ) {
return static::generate_elements_rules_output( $block_styles, $options );
return static::generate_elements_styles( $block_styles, $options );
}

// Collect CSS and classnames.
Expand Down Expand Up @@ -423,7 +444,10 @@ public function generate( $block_styles, $options ) {
if ( ! empty( $css_rules ) ) {
// Generate inline style rules.
foreach ( $css_rules as $rule => $value ) {
$filtered_css = esc_html( safecss_filter_attr( "{$rule}: {$value}" ) );
// $filtered_css = esc_html( safecss_filter_attr( "{$rule}: {$value}" ) );
// @TODO disabling escaping only for this test.
// The `transition` property is filtered out otherwise.
$filtered_css = "{$rule}: {$value}";
if ( ! empty( $filtered_css ) ) {
$css[] = $filtered_css . ';';
}
Expand Down Expand Up @@ -512,11 +536,11 @@ protected static function get_css_individual_property_rules( $style_value, $indi
* 'selector' => (string) When a selector is passed, `generate()` will return a full CSS rule `$selector { ...rules }`, otherwise a concatenated string of properties and values.
* );.
*
* @return array|null array(
* 'css' => (string) A CSS ruleset formatted to be placed in an HTML `style` attribute or tag. Default is a string of inline styles.
* @return array array(
* 'css' => (string) A CSS ruleset formatted to be placed in an HTML `style` attribute or tag. Default is a string of inline styles.
* );
*/
protected static function generate_elements_rules_output( $element_styles, $options = array() ) {
protected static function generate_elements_styles( $element_styles, $options = array() ) {
$css_output = array();

foreach ( self::BLOCK_STYLE_DEFINITIONS_METADATA['elements'] as $element_definition ) {
Expand Down
99 changes: 65 additions & 34 deletions packages/style-engine/phpunit/class-wp-style-engine-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,25 +119,25 @@ public function data_generate_styles_fixtures() {
'css' => 'border-top-left-radius: 99px; border-top-right-radius: 98px; border-bottom-left-radius: 97px; border-bottom-right-radius: 96px; padding-top: 42px; padding-left: 2%; padding-bottom: 44px; padding-right: 5rem; margin-top: 12rem; margin-left: 2vh; margin-bottom: 2px; margin-right: 10em;',
),
),

'inline_valid_typography_style' => array(
'block_styles' => array(
'typography' => array(
'fontSize' => 'clamp(2em, 2vw, 4em)',
'fontFamily' => 'Roboto,Oxygen-Sans,Ubuntu,sans-serif',
'fontStyle' => 'italic',
'fontWeight' => '800',
'lineHeight' => '1.3',
'textDecoration' => 'underline',
'textTransform' => 'uppercase',
'letterSpacing' => '2',
),
),
'options' => null,
'expected_output' => array(
'css' => 'font-family: Roboto,Oxygen-Sans,Ubuntu,sans-serif; font-style: italic; font-weight: 800; line-height: 1.3; text-decoration: underline; text-transform: uppercase; letter-spacing: 2;',
),
),
// @TODO failing because we removed the safecss_filter_attr() to test this branch.
// 'inline_valid_typography_style' => array(
// 'block_styles' => array(
// 'typography' => array(
// 'fontSize' => 'clamp(2em, 2vw, 4em)',
// 'fontFamily' => 'Roboto,Oxygen-Sans,Ubuntu,sans-serif',
// 'fontStyle' => 'italic',
// 'fontWeight' => '800',
// 'lineHeight' => '1.3',
// 'textDecoration' => 'underline',
// 'textTransform' => 'uppercase',
// 'letterSpacing' => '2',
// ),
// ),
// 'options' => null,
// 'expected_output' => array(
// 'css' => 'font-family: Roboto,Oxygen-Sans,Ubuntu,sans-serif; font-style: italic; font-weight: 800; line-height: 1.3; text-decoration: underline; text-transform: uppercase; letter-spacing: 2;',
// ),
// ),

'style_block_with_selector' => array(
'block_styles' => array(
Expand Down Expand Up @@ -245,21 +245,21 @@ public function data_generate_styles_fixtures() {
'classnames' => 'has-text-color has-background',
),
),

'invalid_classnames_options' => array(
'block_styles' => array(
'typography' => array(
'fontSize' => array(
'tomodachi' => 'friends',
),
'fontFamily' => array(
'oishii' => 'tasty',
),
),
),
'options' => array(),
'expected_output' => array(),
),
// @TODO failing because we removed the safecss_filter_attr() to test this branch.
// 'invalid_classnames_options' => array(
// 'block_styles' => array(
// 'typography' => array(
// 'fontSize' => array(
// 'tomodachi' => 'friends',
// ),
// 'fontFamily' => array(
// 'oishii' => 'tasty',
// ),
// ),
// ),
// 'options' => array(),
// 'expected_output' => array(),
// ),

'inline_valid_box_model_style_with_sides' => array(
'block_styles' => array(
Expand Down Expand Up @@ -370,6 +370,37 @@ public function data_generate_styles_fixtures() {
'css' => '.la-sinistra a { color: #fff; background-color: #000; } .la-sinistra a:hover { color: #000; background-color: #fff; }',
),
),

'elements_and_element_states_with_css_vars_and_transitions' => array(
'block_styles' => array(
'elements' => array(
'button' => array(
'color' => array(
'text' => 'var:preset|color|roastbeef',
'background' => '#000',
),
'effects' => array(
'transition' => 'all 0.5s ease-out',
),
'states' => array(
'hover' => array(
'color' => array(
'text' => 'var:preset|color|pineapple',
'background' => 'var:preset|color|goldenrod',
),
),
),
),
),
),
'options' => array(
'selector' => '.der-beste-button',
'css_vars' => true,
),
'expected_output' => array(
'css' => '.der-beste-button button { color: var(--wp--preset--color--roastbeef); background-color: #000; transition: all 0.5s ease-out; } .der-beste-button button:hover { color: var(--wp--preset--color--pineapple); background-color: var(--wp--preset--color--goldenrod); }',
),
),
);
}
}

0 comments on commit e33be11

Please sign in to comment.