| @@ 510-560 (lines=51) @@ | ||
| 507 | * @param string $css |
|
| 508 | * @return string |
|
| 509 | */ |
|
| 510 | private function compress_hex_colors($css) |
|
| 511 | { |
|
| 512 | // Look for hex colors inside { ... } (to avoid IDs) and which don't have a =, or a " in front of them (to avoid filters) |
|
| 513 | $pattern = '/(\=\s*?["\']?)?#([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])(\}|[^0-9a-f{][^{]*?\})/iS'; |
|
| 514 | $_index = $index = $last_index = $offset = 0; |
|
| 515 | $sb = array(); |
|
| 516 | // See: http://ajaxmin.codeplex.com/wikipage?title=CSS%20Colors |
|
| 517 | $short_safe = array( |
|
| 518 | '#808080' => 'gray', |
|
| 519 | '#008000' => 'green', |
|
| 520 | '#800000' => 'maroon', |
|
| 521 | '#000080' => 'navy', |
|
| 522 | '#808000' => 'olive', |
|
| 523 | '#ffa500' => 'orange', |
|
| 524 | '#800080' => 'purple', |
|
| 525 | '#c0c0c0' => 'silver', |
|
| 526 | '#008080' => 'teal', |
|
| 527 | '#f00' => 'red' |
|
| 528 | ); |
|
| 529 | ||
| 530 | while (preg_match($pattern, $css, $m, 0, $offset)) { |
|
| 531 | $index = $this->index_of($css, $m[0], $offset); |
|
| 532 | $last_index = $index + strlen($m[0]); |
|
| 533 | $is_filter = $m[1] !== null && $m[1] !== ''; |
|
| 534 | ||
| 535 | $sb[] = $this->str_slice($css, $_index, $index); |
|
| 536 | ||
| 537 | if ($is_filter) { |
|
| 538 | // Restore, maintain case, otherwise filter will break |
|
| 539 | $sb[] = $m[1] . '#' . $m[2] . $m[3] . $m[4] . $m[5] . $m[6] . $m[7]; |
|
| 540 | } else { |
|
| 541 | if (strtolower($m[2]) == strtolower($m[3]) && |
|
| 542 | strtolower($m[4]) == strtolower($m[5]) && |
|
| 543 | strtolower($m[6]) == strtolower($m[7])) { |
|
| 544 | // Compress. |
|
| 545 | $hex = '#' . strtolower($m[3] . $m[5] . $m[7]); |
|
| 546 | } else { |
|
| 547 | // Non compressible color, restore but lower case. |
|
| 548 | $hex = '#' . strtolower($m[2] . $m[3] . $m[4] . $m[5] . $m[6] . $m[7]); |
|
| 549 | } |
|
| 550 | // replace Hex colors to short safe color names |
|
| 551 | $sb[] = array_key_exists($hex, $short_safe) ? $short_safe[$hex] : $hex; |
|
| 552 | } |
|
| 553 | ||
| 554 | $_index = $offset = $last_index - strlen($m[8]); |
|
| 555 | } |
|
| 556 | ||
| 557 | $sb[] = $this->str_slice($css, $_index); |
|
| 558 | ||
| 559 | return implode('', $sb); |
|
| 560 | } |
|
| 561 | ||
| 562 | /* CALLBACKS |
|
| 563 | * --------------------------------------------------------------------------------------------- |
|
| @@ 512-562 (lines=51) @@ | ||
| 509 | * @param string $css |
|
| 510 | * @return string |
|
| 511 | */ |
|
| 512 | private function compress_hex_colors($css) |
|
| 513 | { |
|
| 514 | // Look for hex colors inside { ... } (to avoid IDs) and which don't have a =, or a " in front of them (to avoid filters) |
|
| 515 | $pattern = '/(\=\s*?["\']?)?#([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])(\}|[^0-9a-f{][^{]*?\})/iS'; |
|
| 516 | $_index = $index = $last_index = $offset = 0; |
|
| 517 | $sb = array(); |
|
| 518 | // See: http://ajaxmin.codeplex.com/wikipage?title=CSS%20Colors |
|
| 519 | $short_safe = array( |
|
| 520 | '#808080' => 'gray', |
|
| 521 | '#008000' => 'green', |
|
| 522 | '#800000' => 'maroon', |
|
| 523 | '#000080' => 'navy', |
|
| 524 | '#808000' => 'olive', |
|
| 525 | '#ffa500' => 'orange', |
|
| 526 | '#800080' => 'purple', |
|
| 527 | '#c0c0c0' => 'silver', |
|
| 528 | '#008080' => 'teal', |
|
| 529 | '#f00' => 'red' |
|
| 530 | ); |
|
| 531 | ||
| 532 | while (preg_match($pattern, $css, $m, 0, $offset)) { |
|
| 533 | $index = $this->index_of($css, $m[0], $offset); |
|
| 534 | $last_index = $index + strlen($m[0]); |
|
| 535 | $is_filter = $m[1] !== null && $m[1] !== ''; |
|
| 536 | ||
| 537 | $sb[] = $this->str_slice($css, $_index, $index); |
|
| 538 | ||
| 539 | if ($is_filter) { |
|
| 540 | // Restore, maintain case, otherwise filter will break |
|
| 541 | $sb[] = $m[1] . '#' . $m[2] . $m[3] . $m[4] . $m[5] . $m[6] . $m[7]; |
|
| 542 | } else { |
|
| 543 | if (strtolower($m[2]) == strtolower($m[3]) && |
|
| 544 | strtolower($m[4]) == strtolower($m[5]) && |
|
| 545 | strtolower($m[6]) == strtolower($m[7])) { |
|
| 546 | // Compress. |
|
| 547 | $hex = '#' . strtolower($m[3] . $m[5] . $m[7]); |
|
| 548 | } else { |
|
| 549 | // Non compressible color, restore but lower case. |
|
| 550 | $hex = '#' . strtolower($m[2] . $m[3] . $m[4] . $m[5] . $m[6] . $m[7]); |
|
| 551 | } |
|
| 552 | // replace Hex colors to short safe color names |
|
| 553 | $sb[] = array_key_exists($hex, $short_safe) ? $short_safe[$hex] : $hex; |
|
| 554 | } |
|
| 555 | ||
| 556 | $_index = $offset = $last_index - strlen($m[8]); |
|
| 557 | } |
|
| 558 | ||
| 559 | $sb[] = $this->str_slice($css, $_index); |
|
| 560 | ||
| 561 | return implode('', $sb); |
|
| 562 | } |
|
| 563 | ||
| 564 | /* CALLBACKS |
|
| 565 | * --------------------------------------------------------------------------------------------- |
|