diff --git a/packages/style-engine/class-wp-style-engine.php b/packages/style-engine/class-wp-style-engine.php index 0c170a11b13158..d3eaef4784ec47 100644 --- a/packages/style-engine/class-wp-style-engine.php +++ b/packages/style-engine/class-wp-style-engine.php @@ -145,15 +145,11 @@ class WP_Style_Engine { ), ), 'elements' => array( - 'link' => array( + 'link' => array( 'path' => array( 'elements', 'link' ), 'selector' => 'a', 'states' => array( ':hover' ), ), - 'button' => array( - 'path' => array( 'elements', 'button' ), - 'selector' => 'button', - ), ), 'spacing' => array( 'padding' => array( @@ -542,29 +538,20 @@ protected static function generate_elements_styles( $element_styles, $options = $css_output[] = $generated_elements_styles['css']; } - // Handle states if the given element supports pseudo selector "states" - // in its allow list. if ( ! empty( $element_definition['states'] ) ) { - - foreach ( $element_definition['states'] as $state_pseudo_selector ) { - - // Dynamically generate the state definitions based on the hard coded - // "allow list" of psuedo selectors for the given element. - $state_definition = array( - 'path' => array_merge( $element_definition['path'], array( $state_pseudo_selector ) ), - 'selector' => $element_definition['selector'] . $state_pseudo_selector, - ); - - $state_styles = _wp_array_get( $element_styles, $state_definition['path'], null ); + foreach ( $element_definition['states'] as $state_selector ) { + // Try to fetch "state" styles in the incoming element's style object. + $state_styles = _wp_array_get( $element_styles, array_merge( $element_definition['path'], array( $state_selector ) ), null ); if ( empty( $state_styles ) ) { continue; } - $state_options = array_merge( + $element_state_selector = "{$element_definition['selector']}$state_selector"; + $state_options = array_merge( $options, array( - 'selector' => isset( $options['selector'] ) ? "{$options['selector']} {$state_definition['selector']}" : $state_definition['selector'], + 'selector' => isset( $options['selector'] ) ? "{$options['selector']} $element_state_selector" : $element_state_selector, ) ); diff --git a/packages/style-engine/phpunit/class-wp-style-engine-test.php b/packages/style-engine/phpunit/class-wp-style-engine-test.php index e1bf4e5518dec4..0f39ffc11f7117 100644 --- a/packages/style-engine/phpunit/class-wp-style-engine-test.php +++ b/packages/style-engine/phpunit/class-wp-style-engine-test.php @@ -119,42 +119,42 @@ 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;', ), ), - // @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( - 'spacing' => array( - 'padding' => array( - 'top' => '42px', - 'left' => '2%', - 'bottom' => '44px', - 'right' => '5rem', - ), - ), + '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' => array( 'selector' => '.wp-selector > p' ), - 'expected_output' => array( - 'css' => '.wp-selector > p { padding-top: 42px; padding-left: 2%; padding-bottom: 44px; padding-right: 5rem; }', + ), + '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( + 'spacing' => array( + 'padding' => array( + 'top' => '42px', + 'left' => '2%', + 'bottom' => '44px', + 'right' => '5rem', + ), ), ), + 'options' => array( 'selector' => '.wp-selector > p' ), + 'expected_output' => array( + 'css' => '.wp-selector > p { padding-top: 42px; padding-left: 2%; padding-bottom: 44px; padding-right: 5rem; }', + ), + ), 'with_valid_css_value_preset_style_property' => array( 'block_styles' => array( @@ -245,51 +245,51 @@ public function data_generate_styles_fixtures() { 'classnames' => 'has-text-color has-background', ), ), - // @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( - 'border' => array( - 'top' => array( - 'color' => '#fe1', - 'width' => '1.5rem', - 'style' => 'dashed', - ), - 'right' => array( - 'color' => '#fe2', - 'width' => '1.4rem', - 'style' => 'solid', - ), - 'bottom' => array( - 'color' => '#fe3', - 'width' => '1.3rem', - ), - 'left' => array( - 'color' => 'var:preset|color|swampy-yellow', - 'width' => '0.5rem', - 'style' => 'dotted', - ), + 'invalid_classnames_options' => array( + 'block_styles' => array( + 'typography' => array( + 'fontSize' => array( + 'tomodachi' => 'friends', + ), + 'fontFamily' => array( + 'oishii' => 'tasty', ), ), - 'options' => array(), - 'expected_output' => array( - 'css' => 'border-top-color: #fe1; border-top-width: 1.5rem; border-top-style: dashed; border-right-color: #fe2; border-right-width: 1.4rem; border-right-style: solid; border-bottom-color: #fe3; border-bottom-width: 1.3rem; border-left-color: var(--wp--preset--color--swampy-yellow); border-left-width: 0.5rem; border-left-style: dotted;', + ), + 'options' => array(), + 'expected_output' => array(), + ), + + 'inline_valid_box_model_style_with_sides' => array( + 'block_styles' => array( + 'border' => array( + 'top' => array( + 'color' => '#fe1', + 'width' => '1.5rem', + 'style' => 'dashed', + ), + 'right' => array( + 'color' => '#fe2', + 'width' => '1.4rem', + 'style' => 'solid', + ), + 'bottom' => array( + 'color' => '#fe3', + 'width' => '1.3rem', + ), + 'left' => array( + 'color' => 'var:preset|color|swampy-yellow', + 'width' => '0.5rem', + 'style' => 'dotted', + ), ), ), + 'options' => array(), + 'expected_output' => array( + 'css' => 'border-top-color: #fe1; border-top-width: 1.5rem; border-top-style: dashed; border-right-color: #fe2; border-right-width: 1.4rem; border-right-style: solid; border-bottom-color: #fe3; border-bottom-width: 1.3rem; border-left-color: var(--wp--preset--color--swampy-yellow); border-left-width: 0.5rem; border-left-style: dotted;', + ), + ), 'inline_invalid_box_model_style_with_sides' => array( 'block_styles' => array( @@ -407,7 +407,7 @@ public function data_generate_styles_fixtures() { 'text' => '#fff', 'background' => '#000', ), - // Not a supported pseudo selector (yet!) + // Not a supported pseudo selector (yet!). ':focus' => array( 'color' => array( 'text' => '#000', @@ -423,33 +423,6 @@ public function data_generate_styles_fixtures() { 'css' => 'a { color: #fff; background-color: #000; }', ), ), - - 'unsupported_elements_with_states' => array( - 'block_styles' => array( - 'elements' => array( - // Only `link` has state support at this time. - 'button' => array( - 'color' => array( - 'text' => '#fff', - 'background' => '#000', - ), - ':hover' => array( - // Should be ignored. - 'color' => array( - 'text' => '#000', - 'background' => '#fff', - ), - ), - ), - ), - ), - 'options' => array(), - 'expected_output' => array( - // Should not contain the `:hover` rule. - 'css' => 'button { color: #fff; background-color: #000; }', - ), - ), - ); } }