-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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: add support for nested CSS rules to wp_style_engine_get_stylesheet_from_css_rule()
#58918
Draft
ramonjd
wants to merge
8
commits into
trunk
Choose a base branch
from
update/style-engine-storing-and-processing-rules-groups
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Style engine: add support for nested CSS rules to wp_style_engine_get_stylesheet_from_css_rule()
#58918
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c530b85
Initial commit
ramonjd 149106a
Rolled back store changes.
ramonjd d2ab8fa
updating rules_group class tests
ramonjd 166752a
Changing group_rule to group_rules
ramonjd b788750
updating vars in processor
ramonjd 7a07ab4
Updating docs
ramonjd 7a4e32a
Update packages/style-engine/README.md
ramonjd 8a7f3dc
Removed unnecessary declarations processing. Was a hangover from an e…
ramonjd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,26 +33,25 @@ class WP_Style_Engine_CSS_Rule { | |
protected $declarations; | ||
|
||
/** | ||
* The CSS nested @rule, such as `@media (min-width: 80rem)` or `@layer module`. | ||
* A parent CSS selector in the case of nested CSS, or a CSS nested @rule, such as `@media (min-width: 80rem)` or `@layer module`.. | ||
* | ||
* @var string | ||
*/ | ||
protected $at_rule; | ||
|
||
protected $rules_group; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param string $selector The CSS selector. | ||
* @param string[]|WP_Style_Engine_CSS_Declarations $declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ), | ||
* or a WP_Style_Engine_CSS_Declarations object. | ||
* @param string $at_rule A CSS nested @rule, such as `@media (min-width: 80rem)` or `@layer module`. | ||
* @param string $rules_group A parent CSS selector in the case of nested CSS, or a CSS nested @rule, such as `@media (min-width: 80rem)` or `@layer module`. | ||
* | ||
*/ | ||
public function __construct( $selector = '', $declarations = array(), $at_rule = '' ) { | ||
public function __construct( $selector = '', $declarations = array(), $rules_group = '' ) { | ||
$this->set_selector( $selector ); | ||
$this->add_declarations( $declarations ); | ||
$this->set_at_rule( $at_rule ); | ||
$this->set_rules_group( $rules_group ); | ||
} | ||
|
||
/** | ||
|
@@ -92,17 +91,26 @@ public function add_declarations( $declarations ) { | |
} | ||
|
||
/** | ||
* Sets the at_rule. | ||
* Sets the rules group. | ||
* | ||
* @param string $at_rule A CSS nested @rule, such as `@media (min-width: 80rem)` or `@layer module`. | ||
* @param string $rules_group A parent CSS selector in the case of nested CSS, or a CSS nested @rule, such as `@media (min-width: 80rem)` or `@layer module`. | ||
* | ||
* @return WP_Style_Engine_CSS_Rule Returns the object to allow chaining of methods. | ||
*/ | ||
public function set_at_rule( $at_rule ) { | ||
$this->at_rule = $at_rule; | ||
public function set_rules_group( $rules_group ) { | ||
$this->rules_group = $rules_group; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Gets the rules group. | ||
* | ||
* @return string | ||
*/ | ||
public function get_rules_group() { | ||
return $this->rules_group ?? null; | ||
} | ||
|
||
/** | ||
* Gets the declarations object. | ||
* | ||
|
@@ -121,15 +129,6 @@ public function get_selector() { | |
return $this->selector; | ||
} | ||
|
||
/** | ||
* Gets the at_rule. | ||
* | ||
* @return string | ||
*/ | ||
public function get_at_rule() { | ||
return $this->at_rule; | ||
} | ||
|
||
/** | ||
* Gets the CSS. | ||
* | ||
|
@@ -139,28 +138,19 @@ public function get_at_rule() { | |
* @return string | ||
*/ | ||
public function get_css( $should_prettify = false, $indent_count = 0 ) { | ||
$rule_indent = $should_prettify ? str_repeat( "\t", $indent_count ) : ''; | ||
$nested_rule_indent = $should_prettify ? str_repeat( "\t", $indent_count + 1 ) : ''; | ||
$declarations_indent = $should_prettify ? $indent_count + 1 : 0; | ||
$nested_declarations_indent = $should_prettify ? $indent_count + 2 : 0; | ||
$suffix = $should_prettify ? "\n" : ''; | ||
$spacer = $should_prettify ? ' ' : ''; | ||
$rule_indent = $should_prettify ? str_repeat( "\t", $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. Just reverting to what is was before #58867 |
||
$declarations_indent = $should_prettify ? $indent_count + 1 : 0; | ||
$suffix = $should_prettify ? "\n" : ''; | ||
$spacer = $should_prettify ? ' ' : ''; | ||
// Trims any multiple selectors strings. | ||
$selector = $should_prettify ? implode( ',', array_map( 'trim', explode( ',', $this->get_selector() ) ) ) : $this->get_selector(); | ||
$selector = $should_prettify ? str_replace( array( ',' ), ",\n", $selector ) : $selector; | ||
$at_rule = $this->get_at_rule(); | ||
$has_at_rule = ! empty( $at_rule ); | ||
$css_declarations = $this->declarations->get_declarations_string( $should_prettify, $has_at_rule ? $nested_declarations_indent : $declarations_indent ); | ||
$css_declarations = ! empty( $this->declarations ) ? $this->declarations->get_declarations_string( $should_prettify, $declarations_indent ) : ''; | ||
|
||
if ( empty( $css_declarations ) ) { | ||
return ''; | ||
} | ||
|
||
if ( $has_at_rule ) { | ||
$selector = "{$rule_indent}{$at_rule}{$spacer}{{$suffix}{$nested_rule_indent}{$selector}{$spacer}{{$suffix}{$css_declarations}{$suffix}{$nested_rule_indent}}{$suffix}{$rule_indent}}"; | ||
return $selector; | ||
} | ||
|
||
return "{$rule_indent}{$selector}{$spacer}{{$suffix}{$css_declarations}{$suffix}{$rule_indent}}"; | ||
} | ||
} | ||
|
153 changes: 153 additions & 0 deletions
153
packages/style-engine/class-wp-style-engine-css-rules-group.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
<?php | ||
/** | ||
* WP_Style_Engine_CSS_Rules_Group | ||
* | ||
* A container for WP_Style_Engine_CSS_Rule objects. | ||
* | ||
* @package Gutenberg | ||
*/ | ||
|
||
if ( ! class_exists( 'WP_Style_Engine_CSS_Rules_Group' ) ) { | ||
/** | ||
* Holds, sanitizes, processes and prints nested CSS rules for the Style Engine. | ||
* | ||
* @access private | ||
*/ | ||
class WP_Style_Engine_CSS_Rules_Group { | ||
/** | ||
* The group's CSS selector in the case of nested CSS, or a CSS nested @rule, such as `@media (min-width: 80rem)` or `@layer module`. | ||
* | ||
* @var string | ||
*/ | ||
protected $rules_group; | ||
|
||
/** | ||
* The container declarations. | ||
* | ||
* Contains a WP_Style_Engine_CSS_Rule object. | ||
* | ||
* @var WP_Style_Engine_CSS_Rule[] | ||
*/ | ||
protected $rules = array(); | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param string $rules_group 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 $rules Optional. A WP_Style_Engine_CSS_Rule object. | ||
*/ | ||
public function __construct( $rules_group, $rules = array() ) { | ||
$this->set_rules_group( $rules_group ); | ||
$this->add_rules( $rules ); | ||
} | ||
|
||
/** | ||
* Sets the rules group. | ||
* | ||
* @param string $rules_group The group's CSS selector in the case of nested CSS, or a CSS nested @rule, such as `@media (min-width: 80rem)` or `@layer module`. | ||
* | ||
* @return WP_Style_Engine_CSS_Rule Returns the object to allow chaining of methods. | ||
*/ | ||
public function set_rules_group( $rules_group ) { | ||
$this->rules_group = $rules_group; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Gets the rules group. | ||
* | ||
* @return string | ||
*/ | ||
public function get_rules_group() { | ||
return $this->rules_group; | ||
} | ||
|
||
/** | ||
* 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_Group 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( | ||
__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; | ||
} | ||
|
||
if ( $this->rules_group !== $rule->get_rules_group() ) { | ||
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; | ||
$new_line = $should_prettify ? "\n" : ''; | ||
$spacer = $should_prettify ? ' ' : ''; | ||
|
||
foreach ( $this->rules as $rule ) { | ||
$css .= $rule->get_css( $should_prettify, $indent_count ); | ||
$css .= $should_prettify ? "\n" : ''; | ||
} | ||
|
||
if ( empty( $css ) ) { | ||
return $css; | ||
} | ||
|
||
return "{$this->rules_group}{$spacer}{{$new_line}{$css}}"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Most of the changes in this PR are renaming
$at_rule
to$rules_group