-
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
Experiment: Style Engine Rules & Store objects #42222
Merged
+428
−0
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
4ca1d9b
Add a CSS_Rule object
aristath df15281
Add a rules store
aristath da2c433
some inline docs & cleanup on the store
aristath f6176f7
typos & fixes
aristath 0a6b4ef
remove parent relations for rules
aristath 7c48c5e
escape the selector
aristath ca30f7e
Allow for multiple named instances of the store
aristath 9de86d7
Use a single declarations object per rule
aristath 59dbf67
Allow chaining methods
aristath 94a141a
Remove esc_html
aristath be06402
Add a default value for store_name
aristath d4a2104
use static()
aristath 4104e21
Adding tests
ramonjd 8deefb8
rename get_rule to add_rule
aristath c3bfa2f
add a remove_rule method
aristath a5f46e9
minor performance improvement
aristath f85c3bd
cleanup & added a return doc
aristath 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
115 changes: 115 additions & 0 deletions
115
packages/style-engine/class-wp-style-engine-css-rule.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,115 @@ | ||
<?php | ||
/** | ||
* WP_Style_Engine_CSS_Rule | ||
* | ||
* An object for CSS rules. | ||
* | ||
* @package Gutenberg | ||
*/ | ||
|
||
if ( class_exists( 'WP_Style_Engine_CSS_Rule' ) ) { | ||
return; | ||
} | ||
|
||
/** | ||
* Holds, sanitizes, processes and prints CSS declarations for the style engine. | ||
* | ||
* @access private | ||
*/ | ||
class WP_Style_Engine_CSS_Rule { | ||
|
||
/** | ||
* The selector. | ||
* | ||
* @var string | ||
*/ | ||
protected $selector; | ||
|
||
/** | ||
* The selector declarations. | ||
* | ||
* Contains a WP_Style_Engine_CSS_Declarations object. | ||
* | ||
* @var WP_Style_Engine_CSS_Declarations | ||
*/ | ||
protected $declarations; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param string $selector The CSS selector. | ||
* @param array|WP_Style_Engine_CSS_Declarations $declarations An array of declarations (property => value pairs), | ||
* or a WP_Style_Engine_CSS_Declarations object. | ||
*/ | ||
public function __construct( $selector = '', $declarations = array() ) { | ||
$this->set_selector( $selector ); | ||
$this->set_declarations( $declarations ); | ||
} | ||
|
||
/** | ||
* Set the selector. | ||
* | ||
* @param string $selector The CSS selector. | ||
* | ||
* @return WP_Style_Engine_CSS_Rule Returns the object to allow chaining of methods. | ||
*/ | ||
public function set_selector( $selector ) { | ||
if ( empty( $selector ) ) { | ||
return; | ||
} | ||
$this->selector = $selector; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Set the declarations. | ||
* | ||
* @param array|WP_Style_Engine_CSS_Declarations $declarations An array of declarations (property => value pairs), | ||
* or a WP_Style_Engine_CSS_Declarations object. | ||
* | ||
* @return WP_Style_Engine_CSS_Rule Returns the object to allow chaining of methods. | ||
*/ | ||
public function set_declarations( $declarations ) { | ||
$is_declarations_object = ! is_array( $declarations ); | ||
$declarations_array = $is_declarations_object ? $declarations->get_declarations() : $declarations; | ||
|
||
if ( null === $this->declarations && $is_declarations_object ) { | ||
$this->declarations = $declarations; | ||
return $this; | ||
} | ||
if ( null === $this->declarations ) { | ||
$this->declarations = new WP_Style_Engine_CSS_Declarations( $declarations_array ); | ||
} | ||
$this->declarations->add_declarations( $declarations_array ); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get the declarations object. | ||
* | ||
* @return WP_Style_Engine_CSS_Declarations | ||
*/ | ||
public function get_declarations() { | ||
return $this->declarations; | ||
} | ||
|
||
/** | ||
* Get the full selector. | ||
* | ||
* @return string | ||
*/ | ||
public function get_selector() { | ||
return $this->selector; | ||
} | ||
|
||
/** | ||
* Get the CSS. | ||
* | ||
* @return string | ||
*/ | ||
public function get_css() { | ||
return $this->get_selector() . ' {' . $this->declarations->get_declarations_string() . '}'; | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
packages/style-engine/class-wp-style-engine-css-rules-store.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,94 @@ | ||
<?php | ||
/** | ||
* WP_Style_Engine_CSS_Rules_Store | ||
* | ||
* A store for WP_Style_Engine_CSS_Rule objects. | ||
* | ||
* @package Gutenberg | ||
*/ | ||
|
||
if ( class_exists( 'WP_Style_Engine_CSS_Rules_Store' ) ) { | ||
return; | ||
} | ||
|
||
/** | ||
* Holds, sanitizes, processes and prints CSS declarations for the style engine. | ||
* | ||
* @access private | ||
*/ | ||
class WP_Style_Engine_CSS_Rules_Store { | ||
|
||
/** | ||
* An array of named WP_Style_Engine_CSS_Rules_Store objects. | ||
* | ||
* @static | ||
* | ||
* @var WP_Style_Engine_CSS_Rules_Store[] | ||
*/ | ||
protected static $stores = array(); | ||
|
||
/** | ||
* An array of CSS Rules objects assigned to the store. | ||
* | ||
* @var WP_Style_Engine_CSS_Rule[] | ||
*/ | ||
protected $rules = array(); | ||
|
||
/** | ||
* Get an instance of the store. | ||
* | ||
* @param string $store_name The name of the store. | ||
* | ||
* @return WP_Style_Engine_CSS_Rules_Store | ||
*/ | ||
public static function get_store( $store_name = 'default' ) { | ||
if ( ! isset( static::$stores[ $store_name ] ) ) { | ||
static::$stores[ $store_name ] = new static(); | ||
} | ||
return static::$stores[ $store_name ]; | ||
} | ||
/** | ||
* Get an array of all rules. | ||
* | ||
* @return WP_Style_Engine_CSS_Rule[] | ||
*/ | ||
public function get_all_rules() { | ||
return $this->rules; | ||
} | ||
|
||
/** | ||
* Get a WP_Style_Engine_CSS_Rule object by its selector. | ||
* If the rule does not exist, it will be created. | ||
* | ||
* @param string $selector The CSS selector. | ||
* | ||
* @return WP_Style_Engine_CSS_Rule|null Returns a WP_Style_Engine_CSS_Rule object, or null if the selector is empty. | ||
*/ | ||
public function add_rule( $selector ) { | ||
|
||
$selector = trim( $selector ); | ||
|
||
// Bail early if there is no selector. | ||
if ( empty( $selector ) ) { | ||
return; | ||
} | ||
|
||
// Create the rule if it doesn't exist. | ||
if ( empty( $this->rules[ $selector ] ) ) { | ||
$this->rules[ $selector ] = new WP_Style_Engine_CSS_Rule( $selector ); | ||
} | ||
|
||
return $this->rules[ $selector ]; | ||
} | ||
|
||
/** | ||
* Remove a selector from the store. | ||
* | ||
* @param string $selector The CSS selector. | ||
* | ||
* @return void | ||
*/ | ||
public function remove_rule( $selector ) { | ||
unset( $this->rules[ $selector ] ); | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
packages/style-engine/phpunit/class-wp-style-engine-css-rule-test.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,96 @@ | ||
<?php | ||
/** | ||
* Tests the Style Engine CSS Rule class. | ||
* | ||
* @package Gutenberg | ||
* @subpackage style-engine | ||
*/ | ||
|
||
require __DIR__ . '/../class-wp-style-engine-css-rule.php'; | ||
require __DIR__ . '/../class-wp-style-engine-css-declarations.php'; | ||
|
||
/** | ||
* Tests for registering, storing and generating CSS declarations. | ||
*/ | ||
class WP_Style_Engine_CSS_Rule_Test extends WP_UnitTestCase { | ||
/** | ||
* Should set declarations on instantiation. | ||
*/ | ||
public function test_instantiate_with_selector_and_rules() { | ||
$selector = '.law-and-order'; | ||
$input_declarations = array( | ||
'margin-top' => '10px', | ||
'font-size' => '2rem', | ||
); | ||
$css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations ); | ||
$css_rule = new WP_Style_Engine_CSS_Rule( $selector, $css_declarations ); | ||
|
||
$this->assertSame( $selector, $css_rule->get_selector() ); | ||
|
||
$expected = "$selector {{$css_declarations->get_declarations_string()}}"; | ||
$this->assertSame( $expected, $css_rule->get_css() ); | ||
} | ||
|
||
/** | ||
* Test dedupe declaration properties. | ||
*/ | ||
public function test_dedupe_properties_in_rules() { | ||
$selector = '.taggart'; | ||
$first_declaration = array( | ||
'font-size' => '2rem', | ||
); | ||
$overwrite_first_declaration = array( | ||
'font-size' => '4px', | ||
); | ||
$css_rule = new WP_Style_Engine_CSS_Rule( $selector, $first_declaration ); | ||
$css_rule->set_declarations( new WP_Style_Engine_CSS_Declarations( $overwrite_first_declaration ) ); | ||
|
||
$expected = '.taggart {font-size: 4px;}'; | ||
$this->assertSame( $expected, $css_rule->get_css() ); | ||
} | ||
|
||
/** | ||
* Should set selector and rules on instantiation. | ||
*/ | ||
public function test_set_declarations() { | ||
// Declarations using a WP_Style_Engine_CSS_Declarations object. | ||
$some_css_declarations = new WP_Style_Engine_CSS_Declarations( array( 'margin-top' => '10px' ) ); | ||
// Declarations using a property => value array. | ||
$some_more_css_declarations = array( 'font-size' => '1rem' ); | ||
$css_rule = new WP_Style_Engine_CSS_Rule( '.hill-street-blues', $some_css_declarations ); | ||
$css_rule->set_declarations( $some_more_css_declarations ); | ||
|
||
$expected = '.hill-street-blues {margin-top: 10px; font-size: 1rem;}'; | ||
$this->assertSame( $expected, $css_rule->get_css() ); | ||
} | ||
|
||
/** | ||
* Should set selector and rules on instantiation. | ||
*/ | ||
public function test_set_selector() { | ||
$selector = '.taggart'; | ||
$css_rule = new WP_Style_Engine_CSS_Rule( $selector ); | ||
|
||
$this->assertSame( $selector, $css_rule->get_selector() ); | ||
|
||
$css_rule->set_selector( '.law-and-order' ); | ||
|
||
$this->assertSame( '.law-and-order', $css_rule->get_selector() ); | ||
} | ||
|
||
/** | ||
* Should set selector and rules on instantiation. | ||
*/ | ||
public function test_get_css() { | ||
$selector = '.chips'; | ||
$input_declarations = array( | ||
'margin-top' => '10px', | ||
'font-size' => '2rem', | ||
); | ||
$css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations ); | ||
$css_rule = new WP_Style_Engine_CSS_Rule( $selector, $css_declarations ); | ||
$expected = "$selector {{$css_declarations->get_declarations_string()}}"; | ||
|
||
$this->assertSame( $expected, $css_rule->get_css() ); | ||
} | ||
} |
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.
Why would we always want to create and store a new rule here? Should it be explicit through a
set_rule
method or something like that?It might end up creating unnecessary records. I could be missing something though!
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.
Oh I see 🤦 This is the setter and getter.
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.
Yeah... it's a 2-in-1 method. Which is why we should probably rename it... 😅