-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Style Engine: Add a WP_Style_Engine_Processor object #42463
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
725b2fb
Add a WP_Style_Engine_Renderer object
aristath 64ed041
Better use of the engine objects
aristath 317457e
Added tests
ramonjd 5baaf51
Updating method name to add_declarations after rebase
ramonjd 73c3036
no need to use md5
aristath ff058f7
No need for an extra method
aristath f89d1ba
renaming stuff
aristath ab0e6e5
micro-optimization
aristath 19a0d4d
rename Renderer to Processor
aristath a4c3cbc
:facepalm: forgot to rename files on inclusion
aristath f8fa57d
some inline comments
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 hidden or 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 hidden or 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 hidden or 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,83 @@ | ||
| <?php | ||
| /** | ||
| * WP_Style_Engine_Renderer | ||
| * | ||
| * Compiles and renders styles from a store of CSS rules. | ||
| * | ||
| * @package Gutenberg | ||
| */ | ||
|
|
||
| if ( class_exists( 'WP_Style_Engine_Renderer' ) ) { | ||
| return; | ||
| } | ||
|
|
||
| /** | ||
| * Compiles and renders styles from a store of CSS rules. | ||
| * | ||
| * @access private | ||
| */ | ||
| class WP_Style_Engine_Renderer { | ||
|
|
||
| /** | ||
| * The store of CSS rules. | ||
| * | ||
| * @var WP_Style_Engine_CSS_Rules_Store | ||
| */ | ||
| protected $store; | ||
| /** | ||
| * Constructor. | ||
| * | ||
| * @param WP_Style_Engine_CSS_Rules_Store $store The store to render. | ||
| */ | ||
| public function __construct( WP_Style_Engine_CSS_Rules_Store $store ) { | ||
| $this->store = $store; | ||
| } | ||
|
|
||
| /** | ||
| * Get the CSS rules as a string. | ||
| * | ||
| * @return string The rendered CSS. | ||
| */ | ||
| public function get_css() { | ||
| $css = ''; | ||
| $rules = $this->combine_rules_selectors(); | ||
| foreach ( $rules as $selector => $rule ) { | ||
| if ( empty( $selector ) || ! $rule instanceof WP_Style_Engine_CSS_Rule ) { | ||
| continue; | ||
| } | ||
| $css .= $selector . ' {' . $rule->get_declarations()->get_declarations_string() . '}'; | ||
| } | ||
| return $css; | ||
| } | ||
|
|
||
| /** | ||
| * Combines selectors from the $styles_array | ||
| * when they have the same styles. | ||
| * | ||
| * @return array | ||
| */ | ||
| private function combine_rules_selectors() { | ||
| $rules = $this->store->get_all_rules(); | ||
| $selector_hashes = array(); | ||
| foreach ( $rules as $selector => $rule ) { | ||
| $selector_hashes[ $selector ] = $rule->get_declarations()->get_hash(); | ||
| } | ||
|
|
||
| // Combine selectors that have the same styles. | ||
| $selector_rules = array(); | ||
| foreach ( $selector_hashes as $selector => $hash ) { | ||
| // Get selectors that use the same styles. | ||
| $duplicates = array_keys( $selector_hashes, $hash, true ); | ||
| // Add item directly if there are no duplicates. | ||
| if ( 1 === count( $duplicates ) ) { | ||
| $selector_rules[ $selector ] = $rules[ $selector ]; | ||
| continue; | ||
| } | ||
| foreach ( $duplicates as $key ) { | ||
| unset( $selector_hashes[ $key ] ); | ||
| } | ||
| $selector_rules[ implode( ',', $duplicates ) ] = $rules[ $selector ]; | ||
| } | ||
| return $selector_rules; | ||
| } | ||
| } | ||
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.