-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Style engine: support nested CSS rules (or CSS containers) #58797
Changes from all commits
4d77e49
17afecc
794d4d9
5bea1a6
08211cc
cafcbc6
63ef366
0514b53
332bc86
63b29de
e6be2cc
66063d3
081de82
5276e36
eb5797c
4dcdc8b
b10ef34
1737f72
84f0f4c
e69c10d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
/** | ||
* WP_Style_Engine_CSS_Rules_Container | ||
* | ||
* A container for WP_Style_Engine_CSS_Rule objects. | ||
* | ||
* @package Gutenberg | ||
*/ | ||
|
||
if ( ! class_exists( 'WP_Style_Engine_CSS_Rules_Container' ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New class with a similar interface to This is intentional so that the relevant getter class methods such as Also so There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO - I'd say the former to maintain separation of concerns. Reason being, with nested CSS, the container rule should also be able to have declarations. This can be handled by the existing API, indicated by passing a container and not a selector: wp_style_engine_get_stylesheet_from_css_rules(
array(
'container' => 'fieldset',
'declarations' => array( .... )
),
array(
'container' => 'fieldset',
'container' => 'textarea',
'declarations' => array( .... )
)
)
/*
Output:
fieldset {
...declarations;
textarea {
...declarations;
}
}
*/ |
||
/** | ||
* Holds, sanitizes, processes and prints nested CSS rules for the Style Engine. | ||
* | ||
* @access private | ||
*/ | ||
class WP_Style_Engine_CSS_Rules_Container extends WP_Style_Engine_CSS_Rule { | ||
/** | ||
* The container declarations. | ||
* | ||
* Contains a WP_Style_Engine_CSS_Rule object. | ||
* | ||
* @var WP_Style_Engine_CSS_Rule[] | ||
*/ | ||
protected $rules = array(); | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param string $selector A parent CSS selector in the case of nested CSS, or a CSS nested @rule, | ||
* such as `@media (min-width: 80rem)` or `@layer module`. | ||
* @param WP_Style_Engine_CSS_Rule[]|WP_Style_Engine_CSS_Rule $rule Optional. A WP_Style_Engine_CSS_Rule object. | ||
*/ | ||
public function __construct( $selector = '', $rule = array() ) { | ||
$this->set_selector( $selector ); | ||
$this->add_rules( $rule ); | ||
} | ||
|
||
/** | ||
* Gets all nested rules. | ||
* | ||
* @return WP_Style_Engine_CSS_Rule[] | ||
*/ | ||
public function get_rules() { | ||
return $this->rules; | ||
} | ||
|
||
/** | ||
* Gets a stored nested rules. | ||
* | ||
* @return WP_Style_Engine_CSS_Rule | ||
*/ | ||
public function get_rule( $selector ) { | ||
return $this->rules[ $selector ] ?? null; | ||
} | ||
|
||
/** | ||
* Adds the rules. | ||
* | ||
* @param WP_Style_Engine_CSS_Rule|WP_Style_Engine_CSS_Rule[] $container_rules An array of declarations (property => value pairs), | ||
* or a WP_Style_Engine_CSS_Declarations object. | ||
* | ||
* @return WP_Style_Engine_CSS_Rules_Container Returns the object to allow chaining of methods. | ||
*/ | ||
public function add_rules( $rules ) { | ||
if ( empty( $rules ) ) { | ||
return $this; | ||
} | ||
|
||
if ( ! is_array( $rules ) ) { | ||
$rules = array( $rules ); | ||
} | ||
|
||
foreach ( $rules as $rule ) { | ||
if ( ! $rule instanceof WP_Style_Engine_CSS_Rule ) { | ||
_doing_it_wrong( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't seem to unit test this using |
||
__METHOD__, | ||
__( 'Rules passed to WP_Style_Engine_CSS_Rules_Container must be an instance of WP_Style_Engine_CSS_Rule', 'default' ), | ||
'6.6.0' | ||
); | ||
continue; | ||
} | ||
|
||
$selector = $rule->get_selector(); | ||
|
||
if ( isset( $this->rules[ $selector ] ) ) { | ||
$this->rules[ $selector ]->add_declarations( $rule->get_declarations() ); | ||
} else { | ||
$this->rules[ $selector ] = $rule; | ||
} | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Gets the nested CSS. | ||
* | ||
* @param bool $should_prettify Whether to add spacing, new lines and indents. | ||
* @param number $indent_count The number of tab indents to apply to the rule. Applies if `prettify` is `true`. | ||
* | ||
* @return string | ||
*/ | ||
public function get_css( $should_prettify = false, $indent_count = 0 ) { | ||
$css = ''; | ||
$indent_count = $should_prettify ? $indent_count + 1 : $indent_count; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bumps the indent count for nested rules |
||
$new_line = $should_prettify ? "\n" : ''; | ||
$spacer = $should_prettify ? ' ' : ''; | ||
$css .= ! empty( $this->declarations ) ? $this->declarations->get_declarations_string( $should_prettify, $indent_count ) : ''; | ||
$css .= $should_prettify && $css ? "\n" : ''; | ||
|
||
foreach ( $this->rules as $rule ) { | ||
$css .= $rule->get_css( $should_prettify, $indent_count ); | ||
$css .= $should_prettify ? "\n" : ''; | ||
} | ||
|
||
if ( empty( $css ) ) { | ||
return $css; | ||
} | ||
|
||
return "{$this->selector}{$spacer}{{$new_line}{$css}}"; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aside from this copy update, these changes just revert those in #58867