Skip to content

Commit

Permalink
Fix 0px minification in calc()
Browse files Browse the repository at this point in the history
Fixes issue #137
  • Loading branch information
matthiasmullie committed Sep 20, 2016
1 parent 64f7d8a commit 325e9b9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/CSS.php
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,24 @@ protected function shortenZeroes($content)
// strip negative zeroes (-0 -> 0) & truncate zeroes (00 -> 0)
$content = preg_replace('/'.$before.'-?0+'.$units.'?'.$after.'/', '0\\1', $content);

// remove zeroes where they make no sense in calc: e.g. calc(100px - 0)
// the 0 doesn't have any effect, and this isn't even valid without unit
// strip all `+ 0` or `- 0` occurrences: calc(10% + 0) -> calc(10%)
// looped because there may be multiple 0s inside 1 group of parentheses
do {
$previous = $content;
$content = preg_replace('/\(([^\(\)]+)\s+[\+\-]\s+0(\s+[^\(\)]+)?\)/', '(\\1\\2)', $content);
} while ( $content !== $previous );
// strip all `0 +` occurrences: calc(0 + 10%) -> calc(10%)
$content = preg_replace('/\(\s*0\s+\+\s+([^\(\)]+)\)/', '(\\1)', $content);
// strip all `0 -` occurrences: calc(0 - 10%) -> calc(-10%)
$content = preg_replace('/\(\s*0\s+\-\s+([^\(\)]+)\)/', '(-\\1)', $content);
// I'm not going to attempt to optimize away `x * 0` instances:
// it's dumb enough code already that it likely won't occur, and it's
// too complex to do right (order of operations would have to be
// respected etc)
// what I cared about most here was fixing incorrectly truncated units

return $content;
}

Expand Down
30 changes: 30 additions & 0 deletions tests/css/CSSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,36 @@ public function dataProvider()
'p{color:khaki}',
);

// https://github.com/matthiasmullie/minify/issues/137
$tests[] = array(
'p{width: calc(35% - 0px);}',
'p{width:calc(35%)}',
);
$tests[] = array(
'p{width: calc(0px + 35%);}',
'p{width:calc(35%)}',
);
$tests[] = array(
'p{width: calc(0px - 35%);}',
'p{width:calc(-35%)}',
);
$tests[] = array(
'p{width: calc(0px + 35% - 0px);}',
'p{width:calc(35%)}',
);
$tests[] = array(
'p{width: calc(5% + 0px + 35% - 0px + 5%);}',
'p{width:calc(5% + 35% + 5%)}',
);
$tests[] = array(
'p{width:calc(35% + (10% + 0px))}',
'p{width:calc(35% + (10%))}',
);
$tests[] = array(
'p{width:calc(35% + (10% + 0px + 10%))}',
'p{width:calc(35% + (10% + 10%))}',
);

return $tests;
}

Expand Down

0 comments on commit 325e9b9

Please sign in to comment.