Limit what zeroes can be truncated

This commit is contained in:
Matthias Mullie 2014-12-07 18:35:07 +01:00
parent 609c6f4cfc
commit e13143b35c
2 changed files with 16 additions and 5 deletions

View File

@ -525,18 +525,21 @@ class CSS extends Minify
protected function shortenZeroes($content)
{
// strip 0-digits (.0 -> 0, 50.00 -> 50)
// no risk of stripping selectors, a class name can't start with a digit
$content = preg_replace('/(?<![0-9])\.0+(?![1-9])/', '0', $content);
$content = preg_replace('/\.0+(?![1-9])/', '', $content);
// truncate zeroes (00 -> 0) (can't be preceded by # for #000 colors)
$content = preg_replace('/(?<![#0-9])0+(?![1-9])/', '0', $content);
// strip negative zeroes (-0 -> 0)
$content = preg_replace('/(?<![0-9])-0(?![\.0-9])/', '0', $content);
$content = preg_replace('/(?<![0-9])-0+(?![\.1-9])/', '0', $content);
// strip units after zeroes (0px -> 0)
$units = array('em', 'ex', '%', 'px', 'cm', 'mm', 'in', 'pt', 'pc', 'ch', 'rem', 'vh', 'vw', 'vmin', 'vmax', 'vm');
$content = preg_replace('/(?<![0-9])0(' . implode('|', $units) . ')/', '0', $content);
$content = preg_replace('/(?<![0-9])0+(' . implode('|', $units) . ')/', '0', $content);
// truncate zeroes (00 -> 0) (can't be preceded by # for #000 colors)
// only before & after certain characters, to ensure we don't truncate
// zeroes in e.g. #000 or selectors
$content = preg_replace('/(?<=[ :\(])0+(?=[ :\)])/', '0', $content);
return $content;
}

View File

@ -219,6 +219,14 @@ margin-left: -0.3125rem;
'p { margin: .0; }',
'p{margin:0}',
);
$tests[] = array(
'p { margin: 00px; }',
'p{margin:0}',
);
$tests[] = array(
'p.class00 { background-color: #000000; color: #000; }',
'p.class00{background-color:#000;color:#000}',
);
// https://github.com/matthiasmullie/minify/issues/24
$tests[] = array(