| @@ 219-261 (lines=43) @@ | ||
| 216 | private function minify($css, $linebreak_pos) |
|
| 217 | { |
|
| 218 | // strings are safe, now wrestle the comments |
|
| 219 | for ($i = 0, $max = count($this->comments); $i < $max; $i++) { |
|
| 220 | ||
| 221 | $token = $this->comments[$i]; |
|
| 222 | $placeholder = '/' . self::COMMENT . $i . '___/'; |
|
| 223 | ||
| 224 | // ! in the first position of the comment means preserve |
|
| 225 | // so push to the preserved tokens keeping the ! |
|
| 226 | if (substr($token, 0, 1) === '!') { |
|
| 227 | $this->preserved_tokens[] = $token; |
|
| 228 | $token_tring = self::TOKEN . (count($this->preserved_tokens) - 1) . '___'; |
|
| 229 | $css = preg_replace($placeholder, $token_tring, $css, 1); |
|
| 230 | // Preserve new lines for /*! important comments |
|
| 231 | $css = preg_replace('/\s*[\n\r\f]+\s*(\/\*'. $token_tring .')/S', self::NL.'$1', $css); |
|
| 232 | $css = preg_replace('/('. $token_tring .'\*\/)\s*[\n\r\f]+\s*/', '$1'.self::NL, $css); |
|
| 233 | continue; |
|
| 234 | } |
|
| 235 | ||
| 236 | // \ in the last position looks like hack for Mac/IE5 |
|
| 237 | // shorten that to /*\*/ and the next one to /**/ |
|
| 238 | if (substr($token, (strlen($token) - 1), 1) === '\\') { |
|
| 239 | $this->preserved_tokens[] = '\\'; |
|
| 240 | $css = preg_replace($placeholder, self::TOKEN . (count($this->preserved_tokens) - 1) . '___', $css, 1); |
|
| 241 | $i = $i + 1; // attn: advancing the loop |
|
| 242 | $this->preserved_tokens[] = ''; |
|
| 243 | $css = preg_replace('/' . self::COMMENT . $i . '___/', self::TOKEN . (count($this->preserved_tokens) - 1) . '___', $css, 1); |
|
| 244 | continue; |
|
| 245 | } |
|
| 246 | ||
| 247 | // keep empty comments after child selectors (IE7 hack) |
|
| 248 | // e.g. html >/**/ body |
|
| 249 | if (strlen($token) === 0) { |
|
| 250 | $start_index = $this->index_of($css, $this->str_slice($placeholder, 1, -1)); |
|
| 251 | if ($start_index > 2) { |
|
| 252 | if (substr($css, $start_index - 3, 1) === '>') { |
|
| 253 | $this->preserved_tokens[] = ''; |
|
| 254 | $css = preg_replace($placeholder, self::TOKEN . (count($this->preserved_tokens) - 1) . '___', $css, 1); |
|
| 255 | } |
|
| 256 | } |
|
| 257 | } |
|
| 258 | ||
| 259 | // in all other cases kill the comment |
|
| 260 | $css = preg_replace('/\/\*' . $this->str_slice($placeholder, 1, -1) . '\*\//', '', $css, 1); |
|
| 261 | } |
|
| 262 | ||
| 263 | ||
| 264 | // Normalize all whitespace strings to single spaces. Easier to work with that way. |
|
| @@ 239-281 (lines=43) @@ | ||
| 236 | private function minify($css, $linebreak_pos) |
|
| 237 | { |
|
| 238 | // strings are safe, now wrestle the comments |
|
| 239 | for ($i = 0, $max = count($this->comments); $i < $max; $i++) { |
|
| 240 | ||
| 241 | $token = $this->comments[$i]; |
|
| 242 | $placeholder = '/' . self::COMMENT . $i . '___/'; |
|
| 243 | ||
| 244 | // ! in the first position of the comment means preserve |
|
| 245 | // so push to the preserved tokens keeping the ! |
|
| 246 | if (substr($token, 0, 1) === '!') { |
|
| 247 | $this->preserved_tokens[] = $token; |
|
| 248 | $token_tring = self::TOKEN . (count($this->preserved_tokens) - 1) . '___'; |
|
| 249 | $css = preg_replace($placeholder, $token_tring, $css, 1); |
|
| 250 | // Preserve new lines for /*! important comments |
|
| 251 | $css = preg_replace('/\s*[\n\r\f]+\s*(\/\*'. $token_tring .')/S', self::NL.'$1', $css); |
|
| 252 | $css = preg_replace('/('. $token_tring .'\*\/)\s*[\n\r\f]+\s*/', '$1'.self::NL, $css); |
|
| 253 | continue; |
|
| 254 | } |
|
| 255 | ||
| 256 | // \ in the last position looks like hack for Mac/IE5 |
|
| 257 | // shorten that to /*\*/ and the next one to /**/ |
|
| 258 | if (substr($token, (strlen($token) - 1), 1) === '\\') { |
|
| 259 | $this->preserved_tokens[] = '\\'; |
|
| 260 | $css = preg_replace($placeholder, self::TOKEN . (count($this->preserved_tokens) - 1) . '___', $css, 1); |
|
| 261 | $i = $i + 1; // attn: advancing the loop |
|
| 262 | $this->preserved_tokens[] = ''; |
|
| 263 | $css = preg_replace('/' . self::COMMENT . $i . '___/', self::TOKEN . (count($this->preserved_tokens) - 1) . '___', $css, 1); |
|
| 264 | continue; |
|
| 265 | } |
|
| 266 | ||
| 267 | // keep empty comments after child selectors (IE7 hack) |
|
| 268 | // e.g. html >/**/ body |
|
| 269 | if (strlen($token) === 0) { |
|
| 270 | $start_index = $this->index_of($css, $this->str_slice($placeholder, 1, -1)); |
|
| 271 | if ($start_index > 2) { |
|
| 272 | if (substr($css, $start_index - 3, 1) === '>') { |
|
| 273 | $this->preserved_tokens[] = ''; |
|
| 274 | $css = preg_replace($placeholder, self::TOKEN . (count($this->preserved_tokens) - 1) . '___', $css, 1); |
|
| 275 | } |
|
| 276 | } |
|
| 277 | } |
|
| 278 | ||
| 279 | // in all other cases kill the comment |
|
| 280 | $css = preg_replace('/\/\*' . $this->str_slice($placeholder, 1, -1) . '\*\//', '', $css, 1); |
|
| 281 | } |
|
| 282 | ||
| 283 | ||
| 284 | // Normalize all whitespace strings to single spaces. Easier to work with that way. |
|