matthiasmullie / minify

CSS & JavaScript minifier, in PHP. Removes whitespace, strips comments, combines files (incl. @import statements and small assets in CSS files), and optimizes/shortens a few common programming patterns.
https://matthiasmullie.github.io/minify/
MIT License
1.98k stars 311 forks source link

Numbers with decimals break processing of rgb() function in legacy format #435

Open EreMaijala opened 4 hours ago

EreMaijala commented 4 hours ago

Version tested: 1.3.73

Looks like decimals cause something to go wrong when processing an rgb() function with decimal numbers in legacy format.

Almost self-sufficient example:

<?php

include __DIR__ . '/vendor/autoload.php';

use MatthiasMullie\Minify\CSS;

$src = '.btn-link{color:rgb(40,86,136);background-color:rgba(0,0,0,0)}';
$minifier = new CSS($src);
echo "SRC: $src" . PHP_EOL;
echo 'OK:  ' . $minifier->execute() . PHP_EOL;
echo PHP_EOL;

$src = '.btn-link{color:rgb(40.75,86.75,136.5);background-color:rgba(0,0,0,0)}';
$minifier = new CSS($src);
echo "SRC: $src" . PHP_EOL;
echo 'BAD: ' . $minifier->execute() . PHP_EOL;

This outputs:

SRC: .btn-link{color:rgb(40,86,136);background-color:rgba(0,0,0,0)}
OK:  .btn-link{color:#285688;background-color:#fff0}

SRC: .btn-link{color:rgb(40.75,86.75,136.5);background-color:rgba(0,0,0,0)}
BAD: .btn-link{color:#fff0}

The second output (BAD) is obviously not correct.

As far as I can see the decimals are valid in CSS, also in the legacy rgb syntax.

EreMaijala commented 4 hours ago

Looks like the regex's in cleanupModernColors are a bit too eager and run past the closing parenthesis. Perhaps it would be enough to replace [^\s] with [^\s)], though I'm not quite sure about that.