private function CSSmin::minify in Advanced CSS/JS Aggregation 8.2
Same name and namespace in other branches
- 8.3 advagg_css_minify/yui/CSSMin.inc \CSSmin::minify()
- 6 advagg_css_compress/yui/CSSMin.inc \CSSmin::minify()
- 7.2 advagg_css_compress/yui/CSSMin.inc \CSSmin::minify()
Does bulk of the minification
Parameters
string $css:
int|bool $linebreak_pos:
Return value
string
1 call to CSSmin::minify()
- CSSmin::run in advagg_css_minify/
yui/ CSSMin.inc - Minify a string of CSS
File
- advagg_css_minify/
yui/ CSSMin.inc, line 230
Class
Code
private function minify($css, $linebreak_pos) {
// strings are safe, now wrestle the comments
for ($i = 0, $max = count($this->comments); $i < $max; $i++) {
$token = $this->comments[$i];
$placeholder = '/' . self::COMMENT . $i . '___/';
// ! in the first position of the comment means preserve
// so push to the preserved tokens keeping the !
if (substr($token, 0, 1) === '!') {
$this->preserved_tokens[] = $token;
$token_tring = self::TOKEN . (count($this->preserved_tokens) - 1) . '___';
$css = preg_replace($placeholder, $token_tring, $css, 1);
// Preserve new lines for /*! important comments
$css = preg_replace('/\\s*[\\n\\r\\f]+\\s*(\\/\\*' . $token_tring . ')/S', self::NL . '$1', $css);
$css = preg_replace('/(' . $token_tring . '\\*\\/)\\s*[\\n\\r\\f]+\\s*/', '$1' . self::NL, $css);
continue;
}
// \ in the last position looks like hack for Mac/IE5
// shorten that to /*\*/ and the next one to /**/
if (substr($token, strlen($token) - 1, 1) === '\\') {
$this->preserved_tokens[] = '\\';
$css = preg_replace($placeholder, self::TOKEN . (count($this->preserved_tokens) - 1) . '___', $css, 1);
$i = $i + 1;
// attn: advancing the loop
$this->preserved_tokens[] = '';
$css = preg_replace('/' . self::COMMENT . $i . '___/', self::TOKEN . (count($this->preserved_tokens) - 1) . '___', $css, 1);
continue;
}
// keep empty comments after child selectors (IE7 hack)
// e.g. html >/**/ body
if (strlen($token) === 0) {
$start_index = $this
->index_of($css, $this
->str_slice($placeholder, 1, -1));
if ($start_index > 2) {
if (substr($css, $start_index - 3, 1) === '>') {
$this->preserved_tokens[] = '';
$css = preg_replace($placeholder, self::TOKEN . (count($this->preserved_tokens) - 1) . '___', $css, 1);
}
}
}
// in all other cases kill the comment
$css = preg_replace('/\\/\\*' . $this
->str_slice($placeholder, 1, -1) . '\\*\\//', '', $css, 1);
}
// Normalize all whitespace strings to single spaces. Easier to work with that way.
$css = preg_replace('/\\s+/', ' ', $css);
// Fix IE7 issue on matrix filters which browser accept whitespaces between Matrix parameters
$css = preg_replace_callback('/\\s*filter\\:\\s*progid:DXImageTransform\\.Microsoft\\.Matrix\\(([^\\)]+)\\)/', array(
$this,
'preserve_old_IE_specific_matrix_definition',
), $css);
// Shorten & preserve calculations calc(...) since spaces are important
$css = preg_replace_callback('/calc(\\(((?:[^\\(\\)]+|(?1))*)\\))/i', array(
$this,
'replace_calc',
), $css);
// Replace positive sign from numbers preceded by : or a white-space before the leading space is removed
// +1.2em to 1.2em, +.8px to .8px, +2% to 2%
$css = preg_replace('/((?<!\\\\)\\:|\\s)\\+(\\.?\\d+)/S', '$1$2', $css);
// Remove leading zeros from integer and float numbers preceded by : or a white-space
// 000.6 to .6, -0.8 to -.8, 0050 to 50, -01.05 to -1.05
$css = preg_replace('/((?<!\\\\)\\:|\\s)(\\-?)0+(\\.?\\d+)/S', '$1$2$3', $css);
// Remove trailing zeros from float numbers preceded by : or a white-space
// -6.0100em to -6.01em, .0100 to .01, 1.200px to 1.2px
$css = preg_replace('/((?<!\\\\)\\:|\\s)(\\-?)(\\d?\\.\\d+?)0+([^\\d])/S', '$1$2$3$4', $css);
// Remove trailing .0 -> -9.0 to -9
$css = preg_replace('/((?<!\\\\)\\:|\\s)(\\-?\\d+)\\.0([^\\d])/S', '$1$2$3', $css);
// Replace 0 length numbers with 0
$css = preg_replace('/((?<!\\\\)\\:|\\s)\\-?\\.?0+([^\\d])/S', '${1}0$2', $css);
// Remove the spaces before the things that should not have spaces before them.
// But, be careful not to turn "p :link {...}" into "p:link{...}"
// Swap out any pseudo-class colons with the token, and then swap back.
$css = preg_replace_callback('/(?:^|\\})[^\\{]*\\s+\\:/', array(
$this,
'replace_colon',
), $css);
// Remove spaces before the things that should not have spaces before them.
$css = preg_replace('/\\s+([\\!\\{\\}\\;\\:\\>\\+\\(\\)\\]\\~\\=,])/', '$1', $css);
// Restore spaces for !important
$css = preg_replace('/\\!important/i', ' !important', $css);
// bring back the colon
$css = preg_replace('/' . self::CLASSCOLON . '/', ':', $css);
// retain space for special IE6 cases
$css = preg_replace_callback('/\\:first\\-(line|letter)(\\{|,)/i', array(
$this,
'lowercase_pseudo_first',
), $css);
// no space after the end of a preserved comment
$css = preg_replace('/\\*\\/ /', '*/', $css);
// lowercase some popular @directives
$css = preg_replace_callback('/@(font-face|import|(?:-(?:atsc|khtml|moz|ms|o|wap|webkit)-)?keyframe|media|page|namespace)/i', array(
$this,
'lowercase_directives',
), $css);
// lowercase some more common pseudo-elements
$css = preg_replace_callback('/:(active|after|before|checked|disabled|empty|enabled|first-(?:child|of-type)|focus|hover|last-(?:child|of-type)|link|only-(?:child|of-type)|root|:selection|target|visited)/i', array(
$this,
'lowercase_pseudo_elements',
), $css);
// lowercase some more common functions
$css = preg_replace_callback('/:(lang|not|nth-child|nth-last-child|nth-last-of-type|nth-of-type|(?:-(?:moz|webkit)-)?any)\\(/i', array(
$this,
'lowercase_common_functions',
), $css);
// lower case some common function that can be values
// NOTE: rgb() isn't useful as we replace with #hex later, as well as and() is already done for us
$css = preg_replace_callback('/([:,\\( ]\\s*)(attr|color-stop|from|rgba|to|url|(?:-(?:atsc|khtml|moz|ms|o|wap|webkit)-)?(?:calc|max|min|(?:repeating-)?(?:linear|radial)-gradient)|-webkit-gradient)/iS', array(
$this,
'lowercase_common_functions_values',
), $css);
// Put the space back in some cases, to support stuff like
// @media screen and (-webkit-min-device-pixel-ratio:0){
$css = preg_replace('/\\band\\(/i', 'and (', $css);
// Remove the spaces after the things that should not have spaces after them.
$css = preg_replace('/([\\!\\{\\}\\:;\\>\\+\\(\\[\\~\\=,])\\s+/S', '$1', $css);
// remove unnecessary semicolons
$css = preg_replace('/;+\\}/', '}', $css);
// Fix for issue: #2528146
// Restore semicolon if the last property is prefixed with a `*` (lte IE7 hack)
// to avoid issues on Symbian S60 3.x browsers.
$css = preg_replace('/(\\*[a-z0-9\\-]+\\s*\\:[^;\\}]+)(\\})/', '$1;$2', $css);
// Replace 0 <length> and 0 <percentage> values with 0.
// <length> data type: https://developer.mozilla.org/en-US/docs/Web/CSS/length
// <percentage> data type: https://developer.mozilla.org/en-US/docs/Web/CSS/percentage
$css = preg_replace('/([^\\\\]\\:|\\s)0(?:em|ex|ch|rem|vw|vh|vm|vmin|cm|mm|in|px|pt|pc|%)/iS', '${1}0', $css);
// 0% step in a keyframe? restore the % unit
$css = preg_replace_callback('/(@[a-z\\-]*?keyframes[^\\{]+\\{)(.*?)(\\}\\})/iS', array(
$this,
'replace_keyframe_zero',
), $css);
// Replace 0 0; or 0 0 0; or 0 0 0 0; with 0.
$css = preg_replace('/\\:0(?: 0){1,3}(;|\\}| \\!)/', ':0$1', $css);
// Fix for issue: #2528142
// Replace text-shadow:0; with text-shadow:0 0 0;
$css = preg_replace('/(text-shadow\\:0)(;|\\}| \\!)/i', '$1 0 0$2', $css);
// Replace background-position:0; with background-position:0 0;
// same for transform-origin
// Changing -webkit-mask-position: 0 0 to just a single 0 will result in the second parameter defaulting to 50% (center)
$css = preg_replace('/(background|background\\-position|webkit\\-mask\\-position|(?:webkit|moz|o|ms|)\\-?transform\\-origin)\\:0(;|\\}| \\!)/iS', '$1:0 0$2', $css);
// Shorten colors from rgb(51,102,153) to #336699, rgb(100%,0%,0%) to #ff0000 (sRGB color space)
// Shorten colors from hsl(0, 100%, 50%) to #ff0000 (sRGB color space)
// This makes it more likely that it'll get further compressed in the next step.
$css = preg_replace_callback('/rgb\\s*\\(\\s*([0-9,\\s\\-\\.\\%]+)\\s*\\)(.{1})/i', array(
$this,
'rgb_to_hex',
), $css);
$css = preg_replace_callback('/hsl\\s*\\(\\s*([0-9,\\s\\-\\.\\%]+)\\s*\\)(.{1})/i', array(
$this,
'hsl_to_hex',
), $css);
// Shorten colors from #AABBCC to #ABC or short color name.
$css = $this
->compress_hex_colors($css);
// border: none to border:0, outline: none to outline:0
$css = preg_replace('/(border\\-?(?:top|right|bottom|left|)|outline)\\:none(;|\\}| \\!)/iS', '$1:0$2', $css);
// shorter opacity IE filter
$css = preg_replace('/progid\\:DXImageTransform\\.Microsoft\\.Alpha\\(Opacity\\=/i', 'alpha(opacity=', $css);
// Find a fraction that is used for Opera's -o-device-pixel-ratio query
// Add token to add the "\" back in later
$css = preg_replace('/\\(([a-z\\-]+):([0-9]+)\\/([0-9]+)\\)/i', '($1:$2' . self::QUERY_FRACTION . '$3)', $css);
// Remove empty rules.
$css = preg_replace('/[^\\};\\{\\/]+\\{\\}/S', '', $css);
// Add "/" back to fix Opera -o-device-pixel-ratio query
$css = preg_replace('/' . self::QUERY_FRACTION . '/', '/', $css);
// Replace multiple semi-colons in a row by a single one
// See SF bug #1980989
$css = preg_replace('/;;+/', ';', $css);
// Restore new lines for /*! important comments
$css = preg_replace('/' . self::NL . '/', "\n", $css);
// Lowercase all uppercase properties
$css = preg_replace_callback('/(\\{|\\;)([A-Z\\-]+)(\\:)/', array(
$this,
'lowercase_properties',
), $css);
// Some source control tools don't like it when files containing lines longer
// than, say 8000 characters, are checked in. The linebreak option is used in
// that case to split long lines after a specific column.
if ($linebreak_pos !== FALSE && (int) $linebreak_pos >= 0) {
$linebreak_pos = (int) $linebreak_pos;
$start_index = $i = 0;
while ($i < strlen($css)) {
$i++;
if ($css[$i - 1] === '}' && $i - $start_index > $linebreak_pos) {
$css = $this
->str_slice($css, 0, $i) . "\n" . $this
->str_slice($css, $i);
$start_index = $i;
}
}
}
// restore preserved comments and strings in reverse order
for ($i = count($this->preserved_tokens) - 1; $i >= 0; $i--) {
$css = preg_replace('/' . self::TOKEN . $i . '___/', $this->preserved_tokens[$i], $css, 1);
}
// Trim the final string (for any leading or trailing white spaces)
return trim($css);
}