Skip to content
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

Fix inline block styles minification issues with calc() #29554

Merged
merged 7 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,9 @@ function( $a, $b ) {
*/
function gutenberg_get_minified_styles( $styles ) {
$re1 = '(?sx)("(?:[^"\\\\]++|\\\\.)*+"|\'(?:[^\'\\\\]++|\\\\.)*+\')|/\\* (?> .*? \\*/ )';
$re2 = '(?six)("(?:[^"\\\\]++|\\\\.)*+"|\'(?:[^\'\\\\]++|\\\\.)*+\')|\\s*+ ; \\s*+ ( } ) \\s*+|\\s*+ ( [*$~^|]?+= | [{};,>~+-] | !important\\b ) \\s*+|( [[(:] ) \\s++|\\s++ ( [])] )|\\s++ ( : ) \\s*+(?!(?>[^{}"\']++|"(?:[^"\\\\]++|\\\\.)*+"|\'(?:[^\'\\\\]++|\\\\.)*+\')*+{)|^ \\s++ | \\s++ \\z|(\\s)\\s+';
$re2 = '(?six)("(?:[^"\\\\]++|\\\\.)*+"|\'(?:[^\'\\\\]++|\\\\.)*+\')|\\s*+ ; \\s*+ ( } ) \\s*+|\\s*+ ( [*$~^|]?+= | [{};,>~] | !important\\b ) \\s*+|\\s*([+-])\\s*(?=[^}]*{)|( [[(:] ) \\s++|\\s++ ( [])] )|\\s+(:)(?![^\\}]*\\{)|^ \\s++ | \\s++ \\z|(\\s)\\s+';

$styles = preg_replace( "%$re1%", '$1', $styles );
return preg_replace( "%$re2%", '$1$2$3$4$5$6$7', $styles );
return preg_replace( array( "%{$re1}%", "%{$re2}%" ), array( '$1', '$1$2$3$4$5$6$7$8' ), $styles );
}

/**
Expand Down
20 changes: 20 additions & 0 deletions phpunit/class-gutenberg-utils-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,26 @@ public function test_gutenberg_get_minified_styles() {
}',
'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 ) {
Expand Down