Same name and namespace in other branches
  1. 8.x-2.x advagg_css_minify/yui/CSSMin.inc \CSSmin::minify()
  2. 8.x-3.x advagg_css_minify/yui/CSSMin.inc \CSSmin::minify()

Minifies the given input CSS string

Paramètres

string $css:

int|bool $linebreakPos:

Return value

string

1 call to CSSmin::minify()
CSSmin::run dans advagg_css_compress/yui/CSSMin.inc
Minifies a string of CSS

Fichier

advagg_css_compress/yui/CSSMin.inc, line 323

Classe

CSSmin

Code

private function minify($css, $linebreakPos) {
    // Restore preserved at rule blocks
    for ($i = 0, $max = count($this->atRuleBlocks); $i < $max; $i++) {
        $css = preg_replace($this->getAtRuleBlockPlaceholderRegexById($i), $this->escapeReplacementString($this->atRuleBlocks[$i]), $css, 1);
    }
    // strings are safe, now wrestle the comments
    for ($i = 0, $max = count($this->comments); $i < $max; $i++) {
        $comment = $this->comments[$i];
        $commentPlaceholder = $this->getCommentPlaceholderById($i);
        $commentPlaceholderRegex = $this->getCommentPlaceholderRegexById($i);
        // ! in the first position of the comment means preserve
        // so push to the preserved tokens keeping the !
        if (preg_match('/^!/', $comment)) {
            $preservedTokenPlaceholder = $this->registerPreservedToken($comment);
            $css = preg_replace($commentPlaceholderRegex, $preservedTokenPlaceholder, $css, 1);
            // Preserve new lines for /*! important comments
            $css = preg_replace('/\\R+\\s*(\\/\\*' . $preservedTokenPlaceholder . ')/', self::NL . '$1', $css);
            $css = preg_replace('/(' . $preservedTokenPlaceholder . '\\*\\/)\\s*\\R+/', '$1' . self::NL, $css);
            continue;
        }
        // \ in the last position looks like hack for Mac/IE5
        // shorten that to /*\*/ and the next one to /**/
        if (preg_match('/\\\\$/', $comment)) {
            $preservedTokenPlaceholder = $this->registerPreservedToken('\\');
            $css = preg_replace($commentPlaceholderRegex, $preservedTokenPlaceholder, $css, 1);
            $i = $i + 1;
            // attn: advancing the loop
            $preservedTokenPlaceholder = $this->registerPreservedToken('');
            $css = preg_replace($this->getCommentPlaceholderRegexById($i), $preservedTokenPlaceholder, $css, 1);
            continue;
        }
        // keep empty comments after child selectors (IE7 hack)
        // e.g. html >/**/ body
        if (strlen($comment) === 0) {
            $startIndex = $this->indexOf($css, $commentPlaceholder);
            if ($startIndex > 2) {
                if (substr($css, $startIndex - 3, 1) === '>') {
                    $preservedTokenPlaceholder = $this->registerPreservedToken('');
                    $css = preg_replace($commentPlaceholderRegex, $preservedTokenPlaceholder, $css, 1);
                    continue;
                }
            }
        }
        // in all other cases kill the comment
        $css = preg_replace('/\\/\\*' . $commentPlaceholder . '\\*\\//', '', $css, 1);
    }
    // Normalize all whitespace strings to single spaces. Easier to work with that way.
    $css = preg_replace('/\\s+/', ' ', $css);
    // Remove spaces before & after newlines
    $css = preg_replace('/\\s*' . self::NL . '\\s*/', self::NL, $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,
        'processOldIeSpecificMatrixDefinition',
    ), $css);
    // Shorten & preserve calculations calc(...) since spaces are important
    $css = preg_replace_callback('/calc(\\(((?:[^()]+|(?1))*)\\))/i', array(
        $this,
        'processCalc',
    ), $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,
        'processColon',
    ), $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,
        'lowercasePseudoFirst',
    ), $css);
    // no space after the end of a preserved comment
    $css = preg_replace('/\\*\\/ /', '*/', $css);
    // lowercase some popular @directives
    $css = preg_replace_callback('/@(document|font-face|import|(?:-(?:atsc|khtml|moz|ms|o|wap|webkit)-)?keyframes|media|namespace|page|' . 'supports|viewport)/i', array(
        $this,
        'lowercaseDirectives',
    ), $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,
        'lowercasePseudoElements',
    ), $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,
        'lowercaseCommonFunctions',
    ), $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|-webkit-gradient|' . '(?:-(?:atsc|khtml|moz|ms|o|wap|webkit)-)?(?:calc|max|min|(?:repeating-)?(?:linear|radial)-gradient))/iS', array(
        $this,
        'lowercaseCommonFunctionsValues',
    ), $css);
    // Put the space back in some cases, to support stuff like
    // @media screen and (-webkit-min-device-pixel-ratio:0){
    $css = preg_replace_callback('/(\\s|\\)\\s)(and|not|or)\\(/i', array(
        $this,
        'processAtRulesOperators',
    ), $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);
    // Shorten zero values for safe properties only
    $css = $this->shortenZeroValues($css);
    // Shorten font-weight values
    $css = preg_replace('/(font-weight:)bold\\b/i', '${1}700', $css);
    $css = preg_replace('/(font-weight:)normal\\b/i', '${1}400', $css);
    // Shorten suitable shorthand properties with repeated non-zero values
    $css = preg_replace('/(margin|padding):(' . $this->numRegex . ') (' . $this->numRegex . ') (?:\\2) (?:\\3)(;|\\}| !)/i', '$1:$2 $3$4', $css);
    $css = preg_replace('/(margin|padding):(' . $this->numRegex . ') (' . $this->numRegex . ') (' . $this->numRegex . ') (?:\\3)(;|\\}| !)/i', '$1:$2 $3 $4$5', $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,
        'rgbToHex',
    ), $css);
    $css = preg_replace_callback('/hsl\\s*\\(\\s*([0-9,\\s\\-.%]+)\\s*\\)(.{1})/i', array(
        $this,
        'hslToHex',
    ), $css);
    // Shorten colors from #AABBCC to #ABC or shorter color name.
    $css = $this->shortenHexColors($css);
    // Shorten long named colors: white -> #fff.
    $css = $this->shortenNamedColors($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);
    // Patch new lines to avoid being removed when followed by empty rules cases
    $css = preg_replace('/' . self::NL . '/', self::NL . '}', $css);
    // Remove empty rules.
    $css = preg_replace('/[^{};\\/]+\\{\\}/S', '', $css);
    // Restore new lines for /*! important comments
    $css = preg_replace('/' . self::NL . '}/', "\n", $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);
    // Lowercase all uppercase properties
    $css = preg_replace_callback('/(\\{|;)([A-Z\\-]+)(:)/', array(
        $this,
        'lowercaseProperties',
    ), $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 ($linebreakPos !== false && (int) $linebreakPos >= 0) {
        $linebreakPos = (int) $linebreakPos;
        for ($startIndex = $i = 1, $l = strlen($css); $i < $l; $i++) {
            if ($css[$i - 1] === '}' && $i - $startIndex > $linebreakPos) {
                $css = $this->strSlice($css, 0, $i) . "\n" . $this->strSlice($css, $i);
                $l = strlen($css);
                $startIndex = $i;
            }
        }
    }
    // restore preserved comments and strings in reverse order
    for ($i = count($this->preservedTokens) - 1; $i >= 0; $i--) {
        $css = preg_replace($this->getPreservedTokenPlaceholderRegexById($i), $this->escapeReplacementString($this->preservedTokens[$i]), $css, 1);
    }
    // Trim the final string for any leading or trailing white space but respect newlines!
    $css = preg_replace('/(^ | $)/', '', $css);
    return $css;
}