Skip to content

Commit 1a15254

Browse files
Shorten font weights
As suggested on issue #109
1 parent dcdedb0 commit 1a15254

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/CSS.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ public function execute($path = null, $parents = array())
314314
$css = $this->stripWhitespace($css);
315315
$css = $this->shortenHex($css);
316316
$css = $this->shortenZeroes($css);
317+
$css = $this->shortenFontWeights($css);
317318
$css = $this->stripEmptyTags($css);
318319

319320
// restore the string we've extracted earlier
@@ -489,6 +490,27 @@ protected function shortenHex($content)
489490
return $content;
490491
}
491492

493+
/**
494+
* Shorten CSS font weights.
495+
*
496+
* @param string $content The CSS content to shorten the font weights for.
497+
*
498+
* @return string
499+
*/
500+
protected function shortenFontWeights($content)
501+
{
502+
$weights = array(
503+
'normal' => 400,
504+
'bold' => 700,
505+
);
506+
507+
$callback = function ($match) use ($weights) {
508+
return $match[1] . $weights[$match[2]];
509+
};
510+
511+
return preg_replace_callback('/(font-weight\s*:\s*)('.implode('|', array_keys($weights)).')(?=[;}])/', $callback, $content);
512+
}
513+
492514
/**
493515
* Shorthand 0 values to plain 0, instead of e.g. -0em.
494516
*

tests/css/CSSTest.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,18 @@ public function dataProvider()
423423
'@import "https://font.googleapis.com/css?family=RobotoDraft:regular&lang=en";',
424424
);
425425

426+
// https://github.com/matthiasmullie/minify/issues/109
427+
$tests[] = array(
428+
'p{font-weight:bold;}',
429+
'p{font-weight:700}',
430+
);
431+
$tests[] = array(
432+
'p {
433+
font-weight : normal
434+
}',
435+
'p{font-weight:400}',
436+
);
437+
426438
return $tests;
427439
}
428440

@@ -481,19 +493,19 @@ public function dataProviderPaths()
481493
$source.'/issue39.css',
482494
null, // no output file
483495
// relative paths should remain untouched
484-
"@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}",
496+
"@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}",
485497
);
486498
$tests[] = array(
487499
$source.'/issue39.css',
488500
$target.'/issue39.css',
489501
// relative paths should remain untouched
490-
"@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}",
502+
"@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}",
491503
);
492504
$tests[] = array(
493505
$source.'/issue39.css',
494506
$target.'/target/issue39.css',
495507
// relative paths should have changed
496-
"@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}",
508+
"@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}",
497509
);
498510

499511
// https://github.com/forkcms/forkcms/issues/1121

0 commit comments

Comments
 (0)