From 05b190388b6d98985e944932260429f25034a376 Mon Sep 17 00:00:00 2001 From: ramonjd Date: Tue, 6 Sep 2022 19:56:09 +1000 Subject: [PATCH] Changes from https://github.com/WordPress/gutenberg/pull/43886 --- src/wp-includes/style-engine.php | 12 ++++++++--- .../style-engine/class-wp-style-engine.php | 8 +++++-- .../tests/style-engine/styleEngine.php | 4 ++-- .../style-engine/wpStyleEngineProcessor.php | 21 ++++++++++++------- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/wp-includes/style-engine.php b/src/wp-includes/style-engine.php index 1c7a93de928cb..11d24cb716772 100644 --- a/src/wp-includes/style-engine.php +++ b/src/wp-includes/style-engine.php @@ -89,6 +89,8 @@ function wp_style_engine_get_styles( $block_styles, $options = array() ) { * @param array $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. @@ -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 ); } /** @@ -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 ); } diff --git a/src/wp-includes/style-engine/class-wp-style-engine.php b/src/wp-includes/style-engine/class-wp-style-engine.php index 0f6fac2d38e62..256109ea5260f 100644 --- a/src/wp-includes/style-engine/class-wp-style-engine.php +++ b/src/wp-includes/style-engine/class-wp-style-engine.php @@ -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 ); } } diff --git a/tests/phpunit/tests/style-engine/styleEngine.php b/tests/phpunit/tests/style-engine/styleEngine.php index 6ec8109b32e75..d110dc5bf8b60 100644 --- a/tests/phpunit/tests/style-engine/styleEngine.php +++ b/tests/phpunit/tests/style-engine/styleEngine.php @@ -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 ); } @@ -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 ); } } diff --git a/tests/phpunit/tests/style-engine/wpStyleEngineProcessor.php b/tests/phpunit/tests/style-engine/wpStyleEngineProcessor.php index 3c7a2be72c590..cedac5a53f770 100644 --- a/tests/phpunit/tests/style-engine/wpStyleEngineProcessor.php +++ b/tests/phpunit/tests/style-engine/wpStyleEngineProcessor.php @@ -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 ) ) ); } @@ -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 ) ) ); } @@ -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' ); @@ -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 ) ) ); } @@ -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, + ) + ) ); } @@ -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 ) ) ); } /** @@ -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', @@ -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 ) ) ); } }