這是更先進的JohnyFree的變體,很少有dirty
修改。
這種變異禮包整個CSS成單行線, 刪除所有CSS註釋,以及,如果$partDetails[1]
在年底實際isset檢查,而且比去內爆*(最後else
)
給JohnyFree更多聲望人,他應得的。
ps:我已經在我進行修改的地方添加了0。
function getPrefixedCss($css,$prefix)
{
# Wipe all block comments
$css = preg_replace('!/\*.*?\*/!s', '', $css);
$parts = explode('}', $css);
$mediaQueryStarted = false;
foreach($parts as &$part)
{
$part = trim($part); # Wht not trim immediately .. ?
if(empty($part)) continue;
else # This else is also required
{
$partDetails = explode('{', $part);
if(substr_count($part, "{")==2)
{
$mediaQuery = $partDetails[0]."{";
$partDetails[0] = $partDetails[1];
$mediaQueryStarted = true;
}
$subParts = explode(',', $partDetails[0]);
foreach($subParts as &$subPart)
{
if(trim($subPart)==="@font-face") continue;
else $subPart = $prefix . ' ' . trim($subPart);
}
if(substr_count($part,"{")==2)
{
$part = $mediaQuery."\n".implode(', ', $subParts)."{".$partDetails[2];
}
elseif(empty($part[0]) && $mediaQueryStarted)
{
$mediaQueryStarted = false;
$part = implode(', ', $subParts)."{".$partDetails[2]."}\n"; //finish media query
}
else
{
if(isset($partDetails[1]))
{ # Sometimes, without this check,
# there is an error-notice, we don't need that..
$part = implode(', ', $subParts)."{".$partDetails[1];
}
}
unset($partDetails, $mediaQuery, $subParts); # Kill those three ..
} unset($part); # Kill this one as well
}
# Finish with the whole new prefixed string/file in one line
return(preg_replace('/\s+/',' ',implode("} ", $parts)));
}
每個CSS規則都會在一行上嗎? –