From 6a281e283917531acb22d96018dac99cc7d71362 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Mon, 8 Mar 2021 15:37:56 +0200 Subject: [PATCH] Remove the gutenberg_get_minified_styles function --- lib/blocks.php | 31 +--------------- phpunit/class-gutenberg-utils-test.php | 51 -------------------------- 2 files changed, 2 insertions(+), 80 deletions(-) diff --git a/lib/blocks.php b/lib/blocks.php index 45b12709b897ca..d7141b8aa226ad 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -222,22 +222,10 @@ function gutenberg_maybe_inline_styles() { // Build an array of styles that have a path defined. foreach ( $wp_styles->queue as $handle ) { if ( wp_styles()->get_data( $handle, 'path' ) && file_exists( $wp_styles->registered[ $handle ]->extra['path'] ) ) { - $block_styles = false; - $styles_size = filesize( $wp_styles->registered[ $handle ]->extra['path'] ); - - // Minify styles and get their minified size if SCRIPT_DEBUG is not enabled. - if ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ) { - // Get the styles and minify them by removing comments & whitespace. - $block_styles = gutenberg_get_minified_styles( file_get_contents( $wp_styles->registered[ $handle ]->extra['path'] ) ); - // Get the styles size. - $styles_size = strlen( $block_styles ); - } - $styles[] = array( 'handle' => $handle, 'path' => $wp_styles->registered[ $handle ]->extra['path'], - 'size' => $styles_size, - 'css' => $block_styles, + 'size' => filesize( $wp_styles->registered[ $handle ]->extra['path'] ), ); } } @@ -268,7 +256,7 @@ function( $a, $b ) { } // Get the styles if we don't already have them. - $style['css'] = $style['css'] ? $style['css'] : file_get_contents( $style['path'] ); + $style['css'] = file_get_contents( $style['path'] ); // Set `src` to `false` and add styles inline. $wp_styles->registered[ $style['handle'] ]->src = false; @@ -281,21 +269,6 @@ function( $a, $b ) { } add_action( 'wp_head', 'gutenberg_maybe_inline_styles', 1 ); -/** - * Minify styles. - * - * Removes inline comments and whitespace. - * - * @param string $styles The styles to be minified. - * @return string - */ -function gutenberg_get_minified_styles( $styles ) { - $re1 = '(?sx)("(?:[^"\\\\]++|\\\\.)*+"|\'(?:[^\'\\\\]++|\\\\.)*+\')|/\\* (?> .*? \\*/ )'; - $re2 = '(?six)("(?:[^"\\\\]++|\\\\.)*+"|\'(?:[^\'\\\\]++|\\\\.)*+\')|\\s*+ ; \\s*+ ( } ) \\s*+|\\s*+ ( [*$~^|]?+= | [{};,>~] | !important\\b ) \\s*+|\\s*([+-])\\s*(?=[^}]*{)|( [[(:] ) \\s++|\\s++ ( [])] )|\\s+(:)(?![^\\}]*\\{)|^ \\s++ | \\s++ \\z|(\\s)\\s+'; - - return preg_replace( array( "%{$re1}%", "%{$re2}%" ), array( '$1', '$1$2$3$4$5$6$7$8' ), $styles ); -} - /** * Complements the implementation of block type `core/social-icon`, whether it * be provided by core or the plugin, with derived block types for each diff --git a/phpunit/class-gutenberg-utils-test.php b/phpunit/class-gutenberg-utils-test.php index 58d13d28547e4f..bac88678ea8263 100644 --- a/phpunit/class-gutenberg-utils-test.php +++ b/phpunit/class-gutenberg-utils-test.php @@ -127,55 +127,4 @@ public function test_invalid_parameters_set() { array( 'a' => 2 ) ); } - - /** - * Test gutenberg_get_minified_styles(). - */ - public function test_gutenberg_get_minified_styles() { - $cases = array( - array( - 'in' => ' -/** - * Comment - */ - .foo { - bar: 1; - } - ', - 'out' => '.foo{bar:1}', - ), - array( - 'in' => '/* Comment */#foo{content:" "; bar: 0; - }', - 'out' => '#foo{content:" ";bar:0}', - ), - array( - 'in' => '.foo { width: calc(50% - .625em) }', - 'out' => '.foo{width:calc(50% - .625em)}', // Preserve spaces inside calc(). - ), - array( - 'in' => '@supports (-webkit-overflow-scrolling: touch) { .foo { background-attachment: scroll; } }', - 'out' => '@supports (-webkit-overflow-scrolling:touch){.foo{background-attachment:scroll}}', - ), - array( - 'in' => '@media (prefers-reduced-motion: reduce) { .foo { background: linear-gradient(-135deg, #7adcb4 0%, #00d082 100%); background-attachment: scroll; animation: components-animate__appear-animation 0.1s cubic-bezier(0, 0, 0.2, 1) 0s; } }', - 'out' => '@media (prefers-reduced-motion:reduce){.foo{background:linear-gradient(-135deg,#7adcb4 0%,#00d082 100%);background-attachment:scroll;animation:components-animate__appear-animation 0.1s cubic-bezier(0,0,0.2,1) 0s}}', - ), - array( - 'in' => '@keyframes components-animate__appear-animation { from { transform: translateY(-2em) scaleY(0) scaleX(0); } to { transform: translateY(0%) scaleY(1) scaleX(1); } }', - 'out' => '@keyframes components-animate__appear-animation{from{transform:translateY(-2em) scaleY(0) scaleX(0)}to{transform:translateY(0%) scaleY(1) scaleX(1)}}', - ), - array( - 'in' => '.selector { --foo: calc( var(--bar, calc(1em - 10px ) ); }', - 'out' => '.selector{--foo:calc(var(--bar,calc(1em - 10px))}', - ), - ); - - foreach ( $cases as $case ) { - $this->assertSame( - gutenberg_get_minified_styles( $case['in'] ), - $case['out'] - ); - } - } }