Skip to content

Commit

Permalink
Remove whitespace around + and &
Browse files Browse the repository at this point in the history
Fixes #259
  • Loading branch information
matthiasmullie committed Nov 26, 2018
1 parent cc0eb0c commit 75bb291
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
41 changes: 18 additions & 23 deletions src/CSS.php
Expand Up @@ -307,6 +307,7 @@ public function execute($path = null, $parents = array())
*/
$this->extractStrings();
$this->stripComments();
$this->extractCalcs();
$css = $this->replace($css);

$css = $this->stripWhitespace($css);
Expand Down Expand Up @@ -585,11 +586,7 @@ protected function shortenZeroes($content)
// `5px - 0px` is valid, but `5px - 0` is not
// `10px * 0` is valid (equates to 0), and so is `10 * 0px`, but
// `10 * 0` is invalid
// best to just leave `calc()`s alone, even if they could be optimized
// (which is a whole other undertaking, where units & order of
// operations all need to be considered...)
$calcs = $this->findCalcs($content);
$content = str_replace($calcs, array_keys($calcs), $content);
// we've extracted calcs earlier, so we don't need to worry about this

// reusable bits of code throughout these regexes:
// before & after are used to make sure we don't match lose unintended
Expand Down Expand Up @@ -626,9 +623,6 @@ protected function shortenZeroes($content)
$content = preg_replace('/flex:([0-9]+\s[0-9]+\s)0([;\}])/', 'flex:${1}0%${2}', $content);
$content = preg_replace('/flex-basis:0([;\}])/', 'flex-basis:0%${1}', $content);

// restore `calc()` expressions
$content = str_replace(array_keys($calcs), $calcs, $content);

return $content;
}

Expand Down Expand Up @@ -674,8 +668,8 @@ protected function stripWhitespace($content)
// remove whitespace around meta characters
// inspired by stackoverflow.com/questions/15195750/minify-compress-css-with-regex
$content = preg_replace('/\s*([\*$~^|]?+=|[{};,>~]|!important\b)\s*/', '$1', $content);
$content = preg_replace('/([\[(:])\s+/', '$1', $content);
$content = preg_replace('/\s+([\]\)])/', '$1', $content);
$content = preg_replace('/([\[(:>\+])\s+/', '$1', $content);
$content = preg_replace('/\s+([\]\)>\+])/', '$1', $content);
$content = preg_replace('/\s+(:)(?![^\}]*\{)/', '$1', $content);

// whitespace around + and - can only be stripped inside some pseudo-
Expand All @@ -692,18 +686,13 @@ protected function stripWhitespace($content)
}

/**
* Find all `calc()` occurrences.
*
* @param string $content The CSS content to find `calc()`s in.
*
* @return string[]
* Replace all `calc()` occurrences.
*/
protected function findCalcs($content)
protected function extractCalcs()
{
$results = array();
preg_match_all('/calc(\(.+?)(?=$|;|calc\()/', $content, $matches, PREG_SET_ORDER);

foreach ($matches as $match) {
// PHP only supports $this inside anonymous functions since 5.4
$minifier = $this;
$callback = function ($match) use ($minifier) {
$length = strlen($match[1]);
$expr = '';
$opened = 0;
Expand All @@ -717,11 +706,17 @@ protected function findCalcs($content)
break;
}
}
$rest = str_replace($expr, '', $match[1]);
$expr = trim(substr($expr, 1, -1));

This comment has been minimized.

Copy link
@wearymax

wearymax Dec 11, 2018

FYI: #267


$results['calc('.count($results).')'] = 'calc'.$expr;
}
$count = count($minifier->extracted);
$placeholder = 'calc('.$count.')';
$minifier->extracted[$placeholder] = 'calc('.$expr.')';

return $placeholder.$rest;
};

return $results;
$this->registerPattern('/calc(\(.+?)(?=$|;|calc\()/', $callback);
}

/**
Expand Down
22 changes: 22 additions & 0 deletions tests/css/CSSTest.php
Expand Up @@ -742,6 +742,28 @@ public function dataProvider()
'@import url(http://minify.dev/?a=1&b=some/*lala*/thing);p{color:red}body{font-family:sans-serif}',
);

// https://github.com/matthiasmullie/minify/issues/259
$tests[] = array(
'#layout-newsletter-change .unsubscribe :checked + label .circle, #layout-newsletter-change .pause : checked + label .circle { color: white }',
'#layout-newsletter-change .unsubscribe :checked+label .circle,#layout-newsletter-change .pause :checked+label .circle{color:white}',
);
$tests[] = array(
'input + label{color:white}',
'input+label{color:white}',
);
$tests[] = array(
'input +label{color:white}',
'input+label{color:white}',
);
$tests[] = array(
'input+ label{color:white}',
'input+label{color:white}',
);
$tests[] = array(
'div{width:calc(100px + 100px)}',
'div{width:calc(100px + 100px)}',
);

return $tests;
}

Expand Down

0 comments on commit 75bb291

Please sign in to comment.