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

Prevent unquoted font family names that match color names from being … #50

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
45 changes: 40 additions & 5 deletions src/Minifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,30 @@ class Minifier
const COMMENT_TOKEN_START = '_CSSMIN_CMT_';
const RULE_BODY_TOKEN = '_CSSMIN_RBT_%d_';
const PRESERVED_TOKEN = '_CSSMIN_PTK_%d_';

const UNQUOTED_FONT_TOKEN = '_CSSMIN_UFT_%d_';

// Token lists
private $comments = array();
private $ruleBodies = array();
private $preservedTokens = array();

private $unquotedFontTokens = array();

// Output options
private $keepImportantComments = true;
private $keepSourceMapComment = false;
private $linebreakPosition = 0;

// PHP ini limits
private $raisePhpLimits;
private $memoryLimit;
private $maxExecutionTime = 60; // 1 min
private $pcreBacktrackLimit;
private $pcreRecursionLimit;

// Color maps
private $hexToNamedColorsMap;
private $namedToHexColorsMap;

// Regexes
private $numRegex;
private $charsetRegex = '/@charset [^;]+;/Si';
Expand All @@ -62,6 +64,7 @@ class Minifier
private $shortenThreeZeroesRegex;
private $shortenFourZeroesRegex;
private $unitsGroupRegex = '(?:ch|cm|em|ex|gd|in|mm|px|pt|pc|q|rem|vh|vmax|vmin|vw|%)';
private $unquotedFontsRegex = '/(font-family:|font:)([^\'"]+?)[^};]*/Si';

/**
* @param bool|int $raisePhpLimits If true, PHP settings will be raised if needed
Expand Down Expand Up @@ -287,6 +290,17 @@ private function registerRuleBodyToken($body)
return $tokenId;
}

private function registerUnquotedFontToken($body)
{
if (empty($body)) {
return '';
}

$tokenId = sprintf(self::UNQUOTED_FONT_TOKEN, count($this->unquotedFontTokens));
$this->unquotedFontTokens[$tokenId] = $body;
return $tokenId;
}

/**
* Parses & minifies the given input CSS string
* @param string $css
Expand Down Expand Up @@ -600,6 +614,14 @@ private function processRuleBody($body)
$body
);

// Tokenize unquoted font names in order to hide them from
// color name replacements.
$body = preg_replace_callback(
$this->unquotedFontsRegex,
array($this, 'preserveUnquotedFontTokens'),
$body
);

// Shorten long named colors with a shorter HEX counterpart: white -> #fff.
// Run at least 2 times to cover most cases
$body = preg_replace_callback(
Expand All @@ -608,6 +630,9 @@ private function processRuleBody($body)
$body
);

// Restore unquoted font tokens now after colors have been changed.
$body = $this->restoreUnquotedFontTokens($body);

// Replace positive sign from numbers before the leading space is removed.
// +1.2em to 1.2em, +.8px to .8px, +2% to 2%
$body = preg_replace('/([ :,(])\+(\.?\d+)/S', '$1$2', $body);
Expand Down Expand Up @@ -695,6 +720,16 @@ private function processRuleBody($body)
return $body;
}

private function preserveUnquotedFontTokens($matches)
{
return $this->registerUnquotedFontToken($matches[0]);
}

private function restoreUnquotedFontTokens($body)
{
return strtr($body, $this->unquotedFontTokens);
}

/**
* Compresses At-rules and selectors.
* @param string $css the whole stylesheet with rule bodies tokenized.
Expand Down
5 changes: 5 additions & 0 deletions tests/MinifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ public function testFontWeightProperty()
$this->execTest('font-weight');
}

public function testFontFamilyColor()
{
$this->execTest('font-family-color');
}

public function testImportantRule()
{
$this->execTest('important');
Expand Down
1 change: 1 addition & 0 deletions tests/expectations/font-family-color.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tests/fixtures/font-family-color.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.