Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonjd committed Sep 9, 2022
1 parent 8d87cd1 commit 05b1903
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
12 changes: 9 additions & 3 deletions src/wp-includes/style-engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ function wp_style_engine_get_styles( $block_styles, $options = array() ) {
* @param array<string> $options array(
* 'context' => (string|null) An identifier describing the origin of the style object, e.g., 'block-supports' or 'global-styles'. Default is 'block-supports'.
* When set, the style engine will attempt to store the CSS rules.
* 'optimize' => (boolean) Whether to optimize the CSS output, e.g., combine rules.
* 'prettify' => (boolean) Whether to add new lines to output.
* );.
*
* @return string A compiled CSS string.
Expand Down Expand Up @@ -122,7 +124,7 @@ function wp_style_engine_get_stylesheet_from_css_rules( $css_rules, $options = a
return '';
}

return WP_Style_Engine::compile_stylesheet_from_css_rules( $css_rule_objects );
return WP_Style_Engine::compile_stylesheet_from_css_rules( $css_rule_objects, $options );
}

/**
Expand All @@ -132,13 +134,17 @@ function wp_style_engine_get_stylesheet_from_css_rules( $css_rules, $options = a
* @since 6.1.0
*
* @param string $store_name A valid store name.
* @param array $options array(
* 'optimize' => (boolean) Whether to optimize the CSS output, e.g., combine rules.
* 'prettify' => (boolean) Whether to add new lines to output.
* );.
*
* @return string A compiled CSS string.
*/
function wp_style_engine_get_stylesheet_from_context( $store_name ) {
function wp_style_engine_get_stylesheet_from_context( $store_name, $options = array() ) {
if ( ! class_exists( 'WP_Style_Engine' ) || empty( $store_name ) ) {
return '';
}

return WP_Style_Engine::compile_stylesheet_from_css_rules( WP_Style_Engine::get_store( $store_name )->get_all_rules() );
return WP_Style_Engine::compile_stylesheet_from_css_rules( WP_Style_Engine::get_store( $store_name )->get_all_rules(), $options );
}
8 changes: 6 additions & 2 deletions src/wp-includes/style-engine/class-wp-style-engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -528,12 +528,16 @@ public static function compile_css( $css_declarations, $css_selector ) {
* @since 6.1.0
*
* @param WP_Style_Engine_CSS_Rule[] $css_rules An array of WP_Style_Engine_CSS_Rule objects from a store or otherwise.
* @param array $options array(
* 'optimize' => (boolean) Whether to optimize the CSS output, e.g., combine rules.
* 'prettify' => (boolean) Whether to add new lines to output.
* );.
*
* @return string A compiled stylesheet from stored CSS rules.
*/
public static function compile_stylesheet_from_css_rules( $css_rules ) {
public static function compile_stylesheet_from_css_rules( $css_rules, $options = array() ) {
$processor = new WP_Style_Engine_Processor();
$processor->add_rules( $css_rules );
return $processor->get_css();
return $processor->get_css( $options );
}
}
4 changes: 2 additions & 2 deletions tests/phpunit/tests/style-engine/styleEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ public function test_wp_style_engine_get_stylesheet_from_css_rules() {
),
);

$compiled_stylesheet = wp_style_engine_get_stylesheet_from_css_rules( $css_rules );
$compiled_stylesheet = wp_style_engine_get_stylesheet_from_css_rules( $css_rules, array( 'prettify' => false ) );
$this->assertSame( '.saruman{color:white;height:100px;border-style:solid;align-self:unset;}.gandalf{color:grey;height:90px;border-style:dotted;align-self:safe center;}.radagast{color:brown;height:60px;border-style:dashed;align-self:stretch;}', $compiled_stylesheet );
}

Expand Down Expand Up @@ -662,7 +662,7 @@ public function test_wp_style_engine_get_stylesheet_from_css_rules_dedupes_and_m
),
);

$compiled_stylesheet = wp_style_engine_get_stylesheet_from_css_rules( $css_rules );
$compiled_stylesheet = wp_style_engine_get_stylesheet_from_css_rules( $css_rules, array( 'prettify' => false ) );
$this->assertSame( '.gandalf{color:white;height:190px;border-style:dotted;padding:10px;margin-bottom:100px;}.dumbledore,.rincewind{color:grey;height:90px;border-style:dotted;}', $compiled_stylesheet );
}
}
21 changes: 13 additions & 8 deletions tests/phpunit/tests/style-engine/wpStyleEngineProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function test_return_rules_as_css() {
$a_nice_processor->add_rules( array( $a_nice_css_rule, $a_nicer_css_rule ) );
$this->assertEquals(
'.a-nice-rule{color:var(--nice-color);background-color:purple;}.a-nicer-rule{font-family:Nice sans;font-size:1em;background-color:purple;}',
$a_nice_processor->get_css()
$a_nice_processor->get_css( array( 'prettify' => false ) )
);
}

Expand Down Expand Up @@ -108,7 +108,7 @@ public function test_return_store_rules_as_css() {
$a_nice_renderer->add_store( $a_nice_store );
$this->assertEquals(
'.a-nice-rule{color:var(--nice-color);background-color:purple;}.a-nicer-rule{font-family:Nice sans;font-size:1em;background-color:purple;}',
$a_nice_renderer->get_css()
$a_nice_renderer->get_css( array( 'prettify' => false ) )
);
}

Expand Down Expand Up @@ -137,7 +137,7 @@ public function test_dedupe_and_merge_css_declarations() {
$an_excellent_processor->add_rules( $another_excellent_rule );
$this->assertEquals(
'.an-excellent-rule{color:var(--excellent-color);border-style:dotted;border-color:brown;}',
$an_excellent_processor->get_css()
$an_excellent_processor->get_css( array( 'prettify' => false ) )
);

$yet_another_excellent_rule = new WP_Style_Engine_CSS_Rule( '.an-excellent-rule' );
Expand All @@ -151,7 +151,7 @@ public function test_dedupe_and_merge_css_declarations() {
$an_excellent_processor->add_rules( $yet_another_excellent_rule );
$this->assertEquals(
'.an-excellent-rule{color:var(--excellent-color);border-style:dashed;border-color:brown;border-width:2px;}',
$an_excellent_processor->get_css()
$an_excellent_processor->get_css( array( 'prettify' => false ) )
);
}

Expand Down Expand Up @@ -188,7 +188,12 @@ public function test_output_verbose_css_rules() {

$this->assertEquals(
'.a-sweet-rule{color:var(--sweet-color);background-color:purple;}#an-even-sweeter-rule > marquee{color:var(--sweet-color);background-color:purple;}.the-sweetest-rule-of-all a{color:var(--sweet-color);background-color:purple;}',
$a_sweet_processor->get_css( array( 'optimize' => false ) )
$a_sweet_processor->get_css(
array(
'optimize' => false,
'prettify' => false,
)
)
);
}

Expand Down Expand Up @@ -217,7 +222,7 @@ public function test_combine_css_rules() {

$this->assertEquals(
'.a-sweet-rule,#an-even-sweeter-rule > marquee{color:var(--sweet-color);background-color:purple;}',
$a_sweet_processor->get_css()
$a_sweet_processor->get_css( array( 'prettify' => false ) )
);
}
/**
Expand All @@ -239,7 +244,7 @@ public function test_combine_previously_added_css_rules() {
)
);
$a_lovely_processor->add_rules( $a_lovelier_rule );
$this->assertEquals( '.a-lovely-rule,.a-lovelier-rule{border-color:purple;}', $a_lovely_processor->get_css() );
$this->assertEquals( '.a-lovely-rule,.a-lovelier-rule{border-color:purple;}', $a_lovely_processor->get_css( array( 'prettify' => false ) ) );

$a_most_lovely_rule = new WP_Style_Engine_CSS_Rule(
'.a-most-lovely-rule',
Expand All @@ -259,7 +264,7 @@ public function test_combine_previously_added_css_rules() {

$this->assertEquals(
'.a-lovely-rule,.a-lovelier-rule,.a-most-lovely-rule,.a-perfectly-lovely-rule{border-color:purple;}',
$a_lovely_processor->get_css()
$a_lovely_processor->get_css( array( 'prettify' => false ) )
);
}
}

0 comments on commit 05b1903

Please sign in to comment.