Skip to content

Commit

Permalink
Shorten font weights
Browse files Browse the repository at this point in the history
As suggested on issue #109
  • Loading branch information
matthiasmullie committed Jun 9, 2016
1 parent dcdedb0 commit 1a15254
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
22 changes: 22 additions & 0 deletions src/CSS.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ public function execute($path = null, $parents = array())
$css = $this->stripWhitespace($css);
$css = $this->shortenHex($css);
$css = $this->shortenZeroes($css);
$css = $this->shortenFontWeights($css);
$css = $this->stripEmptyTags($css);

// restore the string we've extracted earlier
Expand Down Expand Up @@ -489,6 +490,27 @@ protected function shortenHex($content)
return $content;
}

/**
* Shorten CSS font weights.
*
* @param string $content The CSS content to shorten the font weights for.
*
* @return string
*/
protected function shortenFontWeights($content)
{
$weights = array(
'normal' => 400,
'bold' => 700,
);

$callback = function ($match) use ($weights) {
return $match[1] . $weights[$match[2]];
};

return preg_replace_callback('/(font-weight\s*:\s*)('.implode('|', array_keys($weights)).')(?=[;}])/', $callback, $content);
}

/**
* Shorthand 0 values to plain 0, instead of e.g. -0em.
*
Expand Down
18 changes: 15 additions & 3 deletions tests/css/CSSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,18 @@ public function dataProvider()
'@import "https://font.googleapis.com/css?family=RobotoDraft:regular&lang=en";',
);

// https://github.com/matthiasmullie/minify/issues/109
$tests[] = array(
'p{font-weight:bold;}',
'p{font-weight:700}',
);
$tests[] = array(
'p {
font-weight : normal
}',
'p{font-weight:400}',
);

return $tests;
}

Expand Down Expand Up @@ -481,19 +493,19 @@ public function dataProviderPaths()
$source.'/issue39.css',
null, // no output file
// relative paths should remain untouched
"@font-face{font-family:'blackcat';src:url(../webfont/blackcat.eot);src:url(../webfont/blackcat.eot?#iefix) format('embedded-opentype'),url(../webfont/blackcat.svg#blackcat) format('svg'),url(../webfont/blackcat.woff) format('woff'),url(../webfont/blackcat.ttf) format('truetype');font-weight:normal;font-style:normal}",
"@font-face{font-family:'blackcat';src:url(../webfont/blackcat.eot);src:url(../webfont/blackcat.eot?#iefix) format('embedded-opentype'),url(../webfont/blackcat.svg#blackcat) format('svg'),url(../webfont/blackcat.woff) format('woff'),url(../webfont/blackcat.ttf) format('truetype');font-weight:400;font-style:normal}",
);
$tests[] = array(
$source.'/issue39.css',
$target.'/issue39.css',
// relative paths should remain untouched
"@font-face{font-family:'blackcat';src:url(../webfont/blackcat.eot);src:url(../webfont/blackcat.eot?#iefix) format('embedded-opentype'),url(../webfont/blackcat.svg#blackcat) format('svg'),url(../webfont/blackcat.woff) format('woff'),url(../webfont/blackcat.ttf) format('truetype');font-weight:normal;font-style:normal}",
"@font-face{font-family:'blackcat';src:url(../webfont/blackcat.eot);src:url(../webfont/blackcat.eot?#iefix) format('embedded-opentype'),url(../webfont/blackcat.svg#blackcat) format('svg'),url(../webfont/blackcat.woff) format('woff'),url(../webfont/blackcat.ttf) format('truetype');font-weight:400;font-style:normal}",
);
$tests[] = array(
$source.'/issue39.css',
$target.'/target/issue39.css',
// relative paths should have changed
"@font-face{font-family:'blackcat';src:url(../../webfont/blackcat.eot);src:url(../../webfont/blackcat.eot?#iefix) format('embedded-opentype'),url(../../webfont/blackcat.svg#blackcat) format('svg'),url(../../webfont/blackcat.woff) format('woff'),url(../../webfont/blackcat.ttf) format('truetype');font-weight:normal;font-style:normal}",
"@font-face{font-family:'blackcat';src:url(../../webfont/blackcat.eot);src:url(../../webfont/blackcat.eot?#iefix) format('embedded-opentype'),url(../../webfont/blackcat.svg#blackcat) format('svg'),url(../../webfont/blackcat.woff) format('woff'),url(../../webfont/blackcat.ttf) format('truetype');font-weight:400;font-style:normal}",
);

// https://github.com/forkcms/forkcms/issues/1121
Expand Down

0 comments on commit 1a15254

Please sign in to comment.