a3_CSSmin

The WooCommerce Predictive Search LITE a3 CSSmin class.

Defined (1)

The class is defined in the following location(s).

/admin/less/lib/cssmin.inc.php  
  1. class a3_CSSmin 
  2. const NL = '___YUICSSMIN_PRESERVED_NL___'; 
  3. const TOKEN = '___YUICSSMIN_PRESERVED_TOKEN_'; 
  4. const COMMENT = '___YUICSSMIN_PRESERVE_CANDIDATE_COMMENT_'; 
  5. const CLASSCOLON = '___YUICSSMIN_PSEUDOCLASSCOLON___'; 
  6. const QUERY_FRACTION = '___YUICSSMIN_QUERY_FRACTION___'; 
  7.  
  8. private $comments; 
  9. private $preserved_tokens; 
  10. private $memory_limit; 
  11. private $max_execution_time; 
  12. private $pcre_backtrack_limit; 
  13. private $pcre_recursion_limit; 
  14. private $raise_php_limits; 
  15.  
  16. /** 
  17. * @param bool|int $raise_php_limits 
  18. * If true, PHP settings will be raised if needed 
  19. */ 
  20. public function __construct($raise_php_limits = TRUE) 
  21. // Set suggested PHP limits 
  22. $this->memory_limit = 128 * 1048576; // 128MB in bytes 
  23. $this->max_execution_time = 60; // 1 min 
  24. $this->pcre_backtrack_limit = 1000 * 1000; 
  25. $this->pcre_recursion_limit = 500 * 1000; 
  26.  
  27. $this->raise_php_limits = (bool) $raise_php_limits; 
  28.  
  29. /** 
  30. * Minify a string of CSS 
  31. * @param string $css 
  32. * @param int|bool $linebreak_pos 
  33. * @return string 
  34. */ 
  35. public function run($css = '', $linebreak_pos = FALSE) 
  36. if (empty($css)) { 
  37. return ''; 
  38.  
  39. if ($this->raise_php_limits) { 
  40. $this->do_raise_php_limits(); 
  41.  
  42. $this->comments = array(); 
  43. $this->preserved_tokens = array(); 
  44.  
  45. $start_index = 0; 
  46. $length = strlen($css); 
  47.  
  48. $css = $this->extract_data_urls($css); 
  49.  
  50. // collect all comment blocks... 
  51. while (($start_index = $this->index_of($css, '/*', $start_index)) >= 0) { 
  52. $end_index = $this->index_of($css, '*/', $start_index + 2); 
  53. if ($end_index < 0) { 
  54. $end_index = $length; 
  55. $comment_found = $this->str_slice($css, $start_index + 2, $end_index); 
  56. $this->comments[] = $comment_found; 
  57. $comment_preserve_string = self::COMMENT . (count($this->comments) - 1) . '___'; 
  58. $css = $this->str_slice($css, 0, $start_index + 2) . $comment_preserve_string . $this->str_slice($css, $end_index); 
  59. // Set correct start_index: Fixes issue #2528130 
  60. $start_index = $end_index + 2 + strlen($comment_preserve_string) - strlen($comment_found); 
  61.  
  62. // preserve strings so their content doesn't get accidentally minified 
  63. $css = preg_replace_callback('/(?:"(?:[^\\\\"]|\\\\.|\\\\)*")|'."(?:'(?:[^\\\\']|\\\\.|\\\\)*')/S", array($this, 'replace_string'), $css); 
  64.  
  65. // Let's divide css code in chunks of 5.000 chars aprox. 
  66. // Reason: PHP's PCRE functions like preg_replace have a "backtrack limit" 
  67. // of 100.000 chars by default (php < 5.3.7) so if we're dealing with really 
  68. // long strings and a (sub)pattern matches a number of chars greater than 
  69. // the backtrack limit number (i.e. /(.*)/s) PCRE functions may fail silently 
  70. // returning NULL and $css would be empty. 
  71. $charset = ''; 
  72. $charset_regexp = '/(@charset)( [^;]+;)/i'; 
  73. $css_chunks = array(); 
  74. $css_chunk_length = 5000; // aprox size, not exact 
  75. $start_index = 0; 
  76. $i = $css_chunk_length; // save initial iterations 
  77. $l = strlen($css); 
  78.  
  79.  
  80. // if the number of characters is 5000 or less, do not chunk 
  81. if ($l <= $css_chunk_length) { 
  82. $css_chunks[] = $css; 
  83. } else { 
  84. // chunk css code securely 
  85. while ($i < $l) { 
  86. $i += 50; // save iterations 
  87. if ($l - $start_index <= $css_chunk_length || $i >= $l) { 
  88. $css_chunks[] = $this->str_slice($css, $start_index); 
  89. break; 
  90. if ($css[$i - 1] === '}' && $i - $start_index > $css_chunk_length) { 
  91. // If there are two ending curly braces }} separated or not by spaces,  
  92. // join them in the same chunk (i.e. @media blocks) 
  93. $next_chunk = substr($css, $i); 
  94. if (preg_match('/^\s*\}/', $next_chunk)) { 
  95. $i = $i + $this->index_of($next_chunk, '}') + 1; 
  96.  
  97. $css_chunks[] = $this->str_slice($css, $start_index, $i); 
  98. $start_index = $i; 
  99.  
  100. // Minify each chunk 
  101. for ($i = 0, $n = count($css_chunks); $i < $n; $i++) { 
  102. $css_chunks[$i] = $this->minify($css_chunks[$i], $linebreak_pos); 
  103. // Keep the first @charset at-rule found 
  104. if (empty($charset) && preg_match($charset_regexp, $css_chunks[$i], $matches)) { 
  105. $charset = strtolower($matches[1]) . $matches[2]; 
  106. // Delete all @charset at-rules 
  107. $css_chunks[$i] = preg_replace($charset_regexp, '', $css_chunks[$i]); 
  108.  
  109. // Update the first chunk and push the charset to the top of the file. 
  110. $css_chunks[0] = $charset . $css_chunks[0]; 
  111.  
  112. return implode('', $css_chunks); 
  113.  
  114. /** 
  115. * Sets the memory limit for this script 
  116. * @param int|string $limit 
  117. */ 
  118. public function set_memory_limit($limit) 
  119. $this->memory_limit = $this->normalize_int($limit); 
  120.  
  121. /** 
  122. * Sets the maximum execution time for this script 
  123. * @param int|string $seconds 
  124. */ 
  125. public function set_max_execution_time($seconds) 
  126. $this->max_execution_time = (int) $seconds; 
  127.  
  128. /** 
  129. * Sets the PCRE backtrack limit for this script 
  130. * @param int $limit 
  131. */ 
  132. public function set_pcre_backtrack_limit($limit) 
  133. $this->pcre_backtrack_limit = (int) $limit; 
  134.  
  135. /** 
  136. * Sets the PCRE recursion limit for this script 
  137. * @param int $limit 
  138. */ 
  139. public function set_pcre_recursion_limit($limit) 
  140. $this->pcre_recursion_limit = (int) $limit; 
  141.  
  142. /** 
  143. * Try to configure PHP to use at least the suggested minimum settings 
  144. */ 
  145. private function do_raise_php_limits() 
  146. $php_limits = array( 
  147. 'memory_limit' => $this->memory_limit,  
  148. 'max_execution_time' => $this->max_execution_time,  
  149. 'pcre.backtrack_limit' => $this->pcre_backtrack_limit,  
  150. 'pcre.recursion_limit' => $this->pcre_recursion_limit 
  151. ); 
  152.  
  153. // If current settings are higher respect them. 
  154. foreach ($php_limits as $name => $suggested) { 
  155. $current = $this->normalize_int(ini_get($name)); 
  156. // memory_limit exception: allow -1 for "no memory limit". 
  157. if ($current > -1 && ($suggested == -1 || $current < $suggested)) { 
  158. ini_set($name, $suggested); 
  159.  
  160. /** 
  161. * Does bulk of the minification 
  162. * @param string $css 
  163. * @param int|bool $linebreak_pos 
  164. * @return string 
  165. */ 
  166. private function minify($css, $linebreak_pos) 
  167. // strings are safe, now wrestle the comments 
  168. for ($i = 0, $max = count($this->comments); $i < $max; $i++) { 
  169.  
  170. $token = $this->comments[$i]; 
  171. $placeholder = '/' . self::COMMENT . $i . '___/'; 
  172.  
  173. // ! in the first position of the comment means preserve 
  174. // so push to the preserved tokens keeping the ! 
  175. if (substr($token, 0, 1) === '!') { 
  176. $this->preserved_tokens[] = $token; 
  177. $token_tring = self::TOKEN . (count($this->preserved_tokens) - 1) . '___'; 
  178. $css = preg_replace($placeholder, $token_tring, $css, 1); 
  179. // Preserve new lines for /*! important comments 
  180. $css = preg_replace('/\s*[\n\r\f]+\s*(\/\*'. $token_tring .')/S', self::NL.'$1', $css); 
  181. $css = preg_replace('/('. $token_tring .'\*\/)\s*[\n\r\f]+\s*/', '$1'.self::NL, $css); 
  182. continue; 
  183.  
  184. // \ in the last position looks like hack for Mac/IE5 
  185. // shorten that to /*\*/ and the next one to /**/ 
  186. if (substr($token, (strlen($token) - 1), 1) === '\\') { 
  187. $this->preserved_tokens[] = '\\'; 
  188. $css = preg_replace($placeholder, self::TOKEN . (count($this->preserved_tokens) - 1) . '___', $css, 1); 
  189. $i = $i + 1; // attn: advancing the loop 
  190. $this->preserved_tokens[] = ''; 
  191. $css = preg_replace('/' . self::COMMENT . $i . '___/', self::TOKEN . (count($this->preserved_tokens) - 1) . '___', $css, 1); 
  192. continue; 
  193.  
  194. // keep empty comments after child selectors (IE7 hack) 
  195. // e.g. html >/**/ body 
  196. if (strlen($token) === 0) { 
  197. $start_index = $this->index_of($css, $this->str_slice($placeholder, 1, -1)); 
  198. if ($start_index > 2) { 
  199. if (substr($css, $start_index - 3, 1) === '>') { 
  200. $this->preserved_tokens[] = ''; 
  201. $css = preg_replace($placeholder, self::TOKEN . (count($this->preserved_tokens) - 1) . '___', $css, 1); 
  202.  
  203. // in all other cases kill the comment 
  204. $css = preg_replace('/\/\*' . $this->str_slice($placeholder, 1, -1) . '\*\//', '', $css, 1); 
  205.  
  206.  
  207. // Normalize all whitespace strings to single spaces. Easier to work with that way. 
  208. $css = preg_replace('/\s+/', ' ', $css); 
  209.  
  210. // Fix IE7 issue on matrix filters which browser accept whitespaces between Matrix parameters 
  211. $css = preg_replace_callback('/\s*filter\:\s*progid:DXImageTransform\.Microsoft\.Matrix\(([^\)]+)\)/', array($this, 'preserve_old_IE_specific_matrix_definition'), $css); 
  212.  
  213. // Shorten & preserve calculations calc(...) since spaces are important 
  214. $css = preg_replace_callback('/calc(\(((?:[^\(\)]+|(?1))*)\))/i', array($this, 'replace_calc'), $css); 
  215.  
  216. // Replace positive sign from numbers preceded by : or a white-space before the leading space is removed 
  217. // +1.2em to 1.2em, +.8px to .8px, +2% to 2% 
  218. $css = preg_replace('/((?<!\\\\)\:|\s)\+(\.?\d+)/S', '$1$2', $css); 
  219.  
  220. // Remove leading zeros from integer and float numbers preceded by : or a white-space 
  221. // 000.6 to .6, -0.8 to -.8, 0050 to 50, -01.05 to -1.05 
  222. $css = preg_replace('/((?<!\\\\)\:|\s)(\-?)0+(\.?\d+)/S', '$1$2$3', $css); 
  223.  
  224. // Remove trailing zeros from float numbers preceded by : or a white-space 
  225. // -6.0100em to -6.01em, .0100 to .01, 1.200px to 1.2px 
  226. $css = preg_replace('/((?<!\\\\)\:|\s)(\-?)(\d?\.\d+?)0+([^\d])/S', '$1$2$3$4', $css); 
  227.  
  228. // Remove trailing .0 -> -9.0 to -9 
  229. $css = preg_replace('/((?<!\\\\)\:|\s)(\-?\d+)\.0([^\d])/S', '$1$2$3', $css); 
  230.  
  231. // Replace 0 length numbers with 0 
  232. $css = preg_replace('/((?<!\\\\)\:|\s)\-?\.?0+([^\d])/S', '${1}0$2', $css); 
  233.  
  234. // Remove the spaces before the things that should not have spaces before them. 
  235. // But, be careful not to turn "p :link {...}" into "p:link{...}" 
  236. // Swap out any pseudo-class colons with the token, and then swap back. 
  237. $css = preg_replace_callback('/(?:^|\})[^\{]*\s+\:/', array($this, 'replace_colon'), $css); 
  238.  
  239. // Remove spaces before the things that should not have spaces before them. 
  240. $css = preg_replace('/\s+([\!\{\}\;\:\>\+\(\)\]\~\=, ])/', '$1', $css); 
  241.  
  242. // Restore spaces for !important 
  243. $css = preg_replace('/\!important/i', ' !important', $css); 
  244.  
  245. // bring back the colon 
  246. $css = preg_replace('/' . self::CLASSCOLON . '/', ':', $css); 
  247.  
  248. // retain space for special IE6 cases 
  249. $css = preg_replace_callback('/\:first\-(line|letter)(\{|, )/i', array($this, 'lowercase_pseudo_first'), $css); 
  250.  
  251. // no space after the end of a preserved comment 
  252. $css = preg_replace('/\*\/ /', '*/', $css); 
  253.  
  254. // lowercase some popular @directives 
  255. $css = preg_replace_callback('/@(font-face|import|(?:-(?:atsc|khtml|moz|ms|o|wap|webkit)-)?keyframe|media|page|namespace)/i', array($this, 'lowercase_directives'), $css); 
  256.  
  257. // lowercase some more common pseudo-elements 
  258. $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); 
  259.  
  260. // lowercase some more common functions 
  261. $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); 
  262.  
  263. // lower case some common function that can be values 
  264. // NOTE: rgb() isn't useful as we replace with #hex later, as well as and() is already done for us 
  265. $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); 
  266.  
  267. // Put the space back in some cases, to support stuff like 
  268. // @media screen and (-webkit-min-device-pixel-ratio:0) { 
  269. $css = preg_replace('/\band\(/i', 'and (', $css); 
  270.  
  271. // Remove the spaces after the things that should not have spaces after them. 
  272. $css = preg_replace('/([\!\{\}\:;\>\+\(\[\~\=, ])\s+/S', '$1', $css); 
  273.  
  274. // remove unnecessary semicolons 
  275. $css = preg_replace('/;+\}/', '}', $css); 
  276.  
  277. // Fix for issue: #2528146 
  278. // Restore semicolon if the last property is prefixed with a `*` (lte IE7 hack) 
  279. // to avoid issues on Symbian S60 3.x browsers. 
  280. $css = preg_replace('/(\*[a-z0-9\-]+\s*\:[^;\}]+)(\})/', '$1;$2', $css); 
  281.  
  282. // Replace 0 <length> and 0 <percentage> values with 0. 
  283. // <length> data type: https://developer.mozilla.org/en-US/docs/Web/CSS/length 
  284. // <percentage> data type: https://developer.mozilla.org/en-US/docs/Web/CSS/percentage 
  285. $css = preg_replace('/([^\\\\]\:|\s)0(?:em|ex|ch|rem|vw|vh|vm|vmin|cm|mm|in|px|pt|pc|%)/iS', '${1}0', $css); 
  286.  
  287. // 0% step in a keyframe? restore the % unit 
  288. $css = preg_replace_callback('/(@[a-z\-]*?keyframes[^\{]+\{)(.*?)(\}\})/iS', array($this, 'replace_keyframe_zero'), $css); 
  289.  
  290. // Replace 0 0; or 0 0 0; or 0 0 0 0; with 0. 
  291. $css = preg_replace('/\:0(?: 0) {1, 3}(;|\}| \!)/', ':0$1', $css); 
  292.  
  293. // Fix for issue: #2528142 
  294. // Replace text-shadow:0; with text-shadow:0 0 0; 
  295. $css = preg_replace('/(text-shadow\:0)(;|\}| \!)/i', '$1 0 0$2', $css); 
  296.  
  297. // Replace background-position:0; with background-position:0 0; 
  298. // same for transform-origin 
  299. // Changing -webkit-mask-position: 0 0 to just a single 0 will result in the second parameter defaulting to 50% (center) 
  300. $css = preg_replace('/(background\-position|webkit-mask-position|(?:webkit|moz|o|ms|)\-?transform\-origin)\:0(;|\}| \!)/iS', '$1:0 0$2', $css); 
  301.  
  302. // Shorten colors from rgb(51, 102, 153) to #336699, rgb(100%, 0%, 0%) to #ff0000 (sRGB color space) 
  303. // Shorten colors from hsl(0, 100%, 50%) to #ff0000 (sRGB color space) 
  304. // This makes it more likely that it'll get further compressed in the next step. 
  305. $css = preg_replace_callback('/rgb\s*\(\s*([0-9, \s\-\.\%]+)\s*\)(.{1})/i', array($this, 'rgb_to_hex'), $css); 
  306. $css = preg_replace_callback('/hsl\s*\(\s*([0-9, \s\-\.\%]+)\s*\)(.{1})/i', array($this, 'hsl_to_hex'), $css); 
  307.  
  308. // Shorten colors from #AABBCC to #ABC or short color name. 
  309. $css = $this->compress_hex_colors($css); 
  310.  
  311. // border: none to border:0, outline: none to outline:0 
  312. $css = preg_replace('/(border\-?(?:top|right|bottom|left|)|outline)\:none(;|\}| \!)/iS', '$1:0$2', $css); 
  313.  
  314. // shorter opacity IE filter 
  315. $css = preg_replace('/progid\:DXImageTransform\.Microsoft\.Alpha\(Opacity\=/i', 'alpha(opacity=', $css); 
  316.  
  317. // Find a fraction that is used for Opera's -o-device-pixel-ratio query 
  318. // Add token to add the "\" back in later 
  319. $css = preg_replace('/\(([a-z\-]+):([0-9]+)\/([0-9]+)\)/i', '($1:$2'. self::QUERY_FRACTION .'$3)', $css); 
  320.  
  321. // Remove empty rules. 
  322. $css = preg_replace('/[^\};\{\/]+\{\}/S', '', $css); 
  323.  
  324. // Add "/" back to fix Opera -o-device-pixel-ratio query 
  325. $css = preg_replace('/'. self::QUERY_FRACTION .'/', '/', $css); 
  326.  
  327. // Replace multiple semi-colons in a row by a single one 
  328. // See SF bug #1980989 
  329. $css = preg_replace('/;;+/', ';', $css); 
  330.  
  331. // Restore new lines for /*! important comments 
  332. $css = preg_replace('/'. self::NL .'/', "\n", $css); 
  333.  
  334. // Lowercase all uppercase properties 
  335. $css = preg_replace_callback('/(\{|\;)([A-Z\-]+)(\:)/', array($this, 'lowercase_properties'), $css); 
  336.  
  337. // Some source control tools don't like it when files containing lines longer 
  338. // than, say 8000 characters, are checked in. The linebreak option is used in 
  339. // that case to split long lines after a specific column. 
  340. if ($linebreak_pos !== FALSE && (int) $linebreak_pos >= 0) { 
  341. $linebreak_pos = (int) $linebreak_pos; 
  342. $start_index = $i = 0; 
  343. while ($i < strlen($css)) { 
  344. $i++; 
  345. if ($css[$i - 1] === '}' && $i - $start_index > $linebreak_pos) { 
  346. $css = $this->str_slice($css, 0, $i) . "\n" . $this->str_slice($css, $i); 
  347. $start_index = $i; 
  348.  
  349. // restore preserved comments and strings in reverse order 
  350. for ($i = count($this->preserved_tokens) - 1; $i >= 0; $i--) { 
  351. $css = preg_replace('/' . self::TOKEN . $i . '___/', $this->preserved_tokens[$i], $css, 1); 
  352.  
  353. // Trim the final string (for any leading or trailing white spaces) 
  354. return trim($css); 
  355.  
  356. /** 
  357. * Utility method to replace all data urls with tokens before we start 
  358. * compressing, to avoid performance issues running some of the subsequent 
  359. * regexes against large strings chunks. 
  360. * @param string $css 
  361. * @return string 
  362. */ 
  363. private function extract_data_urls($css) 
  364. // Leave data urls alone to increase parse performance. 
  365. $max_index = strlen($css) - 1; 
  366. $append_index = $index = $last_index = $offset = 0; 
  367. $sb = array(); 
  368. $pattern = '/url\(\s*(["\']?)data\:/i'; 
  369.  
  370. // Since we need to account for non-base64 data urls, we need to handle 
  371. // ' and ) being part of the data string. Hence switching to indexOf,  
  372. // to determine whether or not we have matching string terminators and 
  373. // handling sb appends directly, instead of using matcher.append* methods. 
  374.  
  375. while (preg_match($pattern, $css, $m, 0, $offset)) { 
  376. $index = $this->index_of($css, $m[0], $offset); 
  377. $last_index = $index + strlen($m[0]); 
  378. $start_index = $index + 4; // "url(".length() 
  379. $end_index = $last_index - 1; 
  380. $terminator = $m[1]; // ', " or empty (not quoted) 
  381. $found_terminator = FALSE; 
  382.  
  383. if (strlen($terminator) === 0) { 
  384. $terminator = ')'; 
  385.  
  386. while ($found_terminator === FALSE && $end_index+1 <= $max_index) { 
  387. $end_index = $this->index_of($css, $terminator, $end_index + 1); 
  388.  
  389. // endIndex == 0 doesn't really apply here 
  390. if ($end_index > 0 && substr($css, $end_index - 1, 1) !== '\\') { 
  391. $found_terminator = TRUE; 
  392. if (')' != $terminator) { 
  393. $end_index = $this->index_of($css, ')', $end_index); 
  394.  
  395. // Enough searching, start moving stuff over to the buffer 
  396. $sb[] = $this->str_slice($css, $append_index, $index); 
  397.  
  398. if ($found_terminator) { 
  399. $token = $this->str_slice($css, $start_index, $end_index); 
  400. $token = preg_replace('/\s+/', '', $token); 
  401. $this->preserved_tokens[] = $token; 
  402.  
  403. $preserver = 'url(' . self::TOKEN . (count($this->preserved_tokens) - 1) . '___)'; 
  404. $sb[] = $preserver; 
  405.  
  406. $append_index = $end_index + 1; 
  407. } else { 
  408. // No end terminator found, re-add the whole match. Should we throw/warn here? 
  409. $sb[] = $this->str_slice($css, $index, $last_index); 
  410. $append_index = $last_index; 
  411.  
  412. $offset = $last_index; 
  413.  
  414. $sb[] = $this->str_slice($css, $append_index); 
  415.  
  416. return implode('', $sb); 
  417.  
  418. /** 
  419. * Utility method to compress hex color values of the form #AABBCC to #ABC or short color name. 
  420. * DOES NOT compress CSS ID selectors which match the above pattern (which would break things). 
  421. * e.g. #AddressForm { ... } 
  422. * DOES NOT compress IE filters, which have hex color values (which would break things). 
  423. * e.g. filter: chroma(color="#FFFFFF"); 
  424. * DOES NOT compress invalid hex values. 
  425. * e.g. background-color: #aabbccdd 
  426. * @param string $css 
  427. * @return string 
  428. */ 
  429. private function compress_hex_colors($css) 
  430. // Look for hex colors inside { ... } (to avoid IDs) and which don't have a =, or a " in front of them (to avoid filters) 
  431. $pattern = '/(\=\s*?["\']?)?#([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])(\}|[^0-9a-f{][^{]*?\})/iS'; 
  432. $_index = $index = $last_index = $offset = 0; 
  433. $sb = array(); 
  434. // See: http://ajaxmin.codeplex.com/wikipage?title=CSS%20Colors 
  435. $short_safe = array( 
  436. '#808080' => 'gray',  
  437. '#008000' => 'green',  
  438. '#800000' => 'maroon',  
  439. '#000080' => 'navy',  
  440. '#808000' => 'olive',  
  441. '#ffa500' => 'orange',  
  442. '#800080' => 'purple',  
  443. '#c0c0c0' => 'silver',  
  444. '#008080' => 'teal',  
  445. '#f00' => 'red' 
  446. ); 
  447.  
  448. while (preg_match($pattern, $css, $m, 0, $offset)) { 
  449. $index = $this->index_of($css, $m[0], $offset); 
  450. $last_index = $index + strlen($m[0]); 
  451. $is_filter = $m[1] !== null && $m[1] !== ''; 
  452.  
  453. $sb[] = $this->str_slice($css, $_index, $index); 
  454.  
  455. if ($is_filter) { 
  456. // Restore, maintain case, otherwise filter will break 
  457. $sb[] = $m[1] . '#' . $m[2] . $m[3] . $m[4] . $m[5] . $m[6] . $m[7]; 
  458. } else { 
  459. if (strtolower($m[2]) == strtolower($m[3]) && 
  460. strtolower($m[4]) == strtolower($m[5]) && 
  461. strtolower($m[6]) == strtolower($m[7])) { 
  462. // Compress. 
  463. $hex = '#' . strtolower($m[3] . $m[5] . $m[7]); 
  464. } else { 
  465. // Non compressible color, restore but lower case. 
  466. $hex = '#' . strtolower($m[2] . $m[3] . $m[4] . $m[5] . $m[6] . $m[7]); 
  467. // replace Hex colors to short safe color names 
  468. $sb[] = array_key_exists($hex, $short_safe) ? $short_safe[$hex] : $hex; 
  469.  
  470. $_index = $offset = $last_index - strlen($m[8]); 
  471.  
  472. $sb[] = $this->str_slice($css, $_index); 
  473.  
  474. return implode('', $sb); 
  475.  
  476. /** CALLBACKS 
  477. * --------------------------------------------------------------------------------------------- 
  478. */ 
  479.  
  480. private function replace_string($matches) 
  481. $match = $matches[0]; 
  482. $quote = substr($match, 0, 1); 
  483. // Must use addcslashes in PHP to avoid parsing of backslashes 
  484. $match = addcslashes($this->str_slice($match, 1, -1), '\\'); 
  485.  
  486. // maybe the string contains a comment-like substring? 
  487. // one, maybe more? put'em back then 
  488. if (($pos = $this->index_of($match, self::COMMENT)) >= 0) { 
  489. for ($i = 0, $max = count($this->comments); $i < $max; $i++) { 
  490. $match = preg_replace('/' . self::COMMENT . $i . '___/', $this->comments[$i], $match, 1); 
  491.  
  492. // minify alpha opacity in filter strings 
  493. $match = preg_replace('/progid\:DXImageTransform\.Microsoft\.Alpha\(Opacity\=/i', 'alpha(opacity=', $match); 
  494.  
  495. $this->preserved_tokens[] = $match; 
  496. return $quote . self::TOKEN . (count($this->preserved_tokens) - 1) . '___' . $quote; 
  497.  
  498. private function replace_colon($matches) 
  499. return preg_replace('/\:/', self::CLASSCOLON, $matches[0]); 
  500.  
  501. private function replace_calc($matches) 
  502. $this->preserved_tokens[] = trim(preg_replace('/\s*([\*\/\(\), ])\s*/', '$1', $matches[2])); 
  503. return 'calc('. self::TOKEN . (count($this->preserved_tokens) - 1) . '___' . ')'; 
  504.  
  505. private function preserve_old_IE_specific_matrix_definition($matches) 
  506. $this->preserved_tokens[] = $matches[1]; 
  507. return 'filter:progid:DXImageTransform.Microsoft.Matrix(' . self::TOKEN . (count($this->preserved_tokens) - 1) . '___' . ')'; 
  508.  
  509. private function replace_keyframe_zero($matches) 
  510. return $matches[1] . preg_replace('/0(\{|, [^\)\{]+\{)/', '0%$1', $matches[2]) . $matches[3]; 
  511.  
  512. private function rgb_to_hex($matches) 
  513. // Support for percentage values rgb(100%, 0%, 45%); 
  514. if ($this->index_of($matches[1], '%') >= 0) { 
  515. $rgbcolors = explode(', ', str_replace('%', '', $matches[1])); 
  516. for ($i = 0; $i < count($rgbcolors); $i++) { 
  517. $rgbcolors[$i] = $this->round_number(floatval($rgbcolors[$i]) * 2.55); 
  518. } else { 
  519. $rgbcolors = explode(', ', $matches[1]); 
  520.  
  521. // Values outside the sRGB color space should be clipped (0-255) 
  522. for ($i = 0; $i < count($rgbcolors); $i++) { 
  523. $rgbcolors[$i] = $this->clamp_number(intval($rgbcolors[$i], 10), 0, 255); 
  524. $rgbcolors[$i] = sprintf("%02x", $rgbcolors[$i]); 
  525.  
  526. // Fix for issue #2528093 
  527. if (!preg_match('/[\s\, \);\}]/', $matches[2])) { 
  528. $matches[2] = ' ' . $matches[2]; 
  529.  
  530. return '#' . implode('', $rgbcolors) . $matches[2]; 
  531.  
  532. private function hsl_to_hex($matches) 
  533. $values = explode(', ', str_replace('%', '', $matches[1])); 
  534. $h = floatval($values[0]); 
  535. $s = floatval($values[1]); 
  536. $l = floatval($values[2]); 
  537.  
  538. // Wrap and clamp, then fraction! 
  539. $h = ((($h % 360) + 360) % 360) / 360; 
  540. $s = $this->clamp_number($s, 0, 100) / 100; 
  541. $l = $this->clamp_number($l, 0, 100) / 100; 
  542.  
  543. if ($s == 0) { 
  544. $r = $g = $b = $this->round_number(255 * $l); 
  545. } else { 
  546. $v2 = $l < 0.5 ? $l * (1 + $s) : ($l + $s) - ($s * $l); 
  547. $v1 = (2 * $l) - $v2; 
  548. $r = $this->round_number(255 * $this->hue_to_rgb($v1, $v2, $h + (1/3))); 
  549. $g = $this->round_number(255 * $this->hue_to_rgb($v1, $v2, $h)); 
  550. $b = $this->round_number(255 * $this->hue_to_rgb($v1, $v2, $h - (1/3))); 
  551.  
  552. return $this->rgb_to_hex(array('', $r.', '.$g.', '.$b, $matches[2])); 
  553.  
  554. private function lowercase_pseudo_first($matches) 
  555. return ':first-'. strtolower($matches[1]) .' '. $matches[2]; 
  556.  
  557. private function lowercase_directives($matches) 
  558. return '@'. strtolower($matches[1]); 
  559.  
  560. private function lowercase_pseudo_elements($matches) 
  561. return ':'. strtolower($matches[1]); 
  562.  
  563. private function lowercase_common_functions($matches) 
  564. return ':'. strtolower($matches[1]) .'('; 
  565.  
  566. private function lowercase_common_functions_values($matches) 
  567. return $matches[1] . strtolower($matches[2]); 
  568.  
  569. private function lowercase_properties($matches) 
  570. return $matches[1].strtolower($matches[2]).$matches[3]; 
  571.  
  572. /** HELPERS 
  573. * --------------------------------------------------------------------------------------------- 
  574. */ 
  575.  
  576. private function hue_to_rgb($v1, $v2, $vh) 
  577. $vh = $vh < 0 ? $vh + 1 : ($vh > 1 ? $vh - 1 : $vh); 
  578. if ($vh * 6 < 1) return $v1 + ($v2 - $v1) * 6 * $vh; 
  579. if ($vh * 2 < 1) return $v2; 
  580. if ($vh * 3 < 2) return $v1 + ($v2 - $v1) * ((2/3) - $vh) * 6; 
  581. return $v1; 
  582.  
  583. private function round_number($n) 
  584. return intval(floor(floatval($n) + 0.5), 10); 
  585.  
  586. private function clamp_number($n, $min, $max) 
  587. return min(max($n, $min), $max); 
  588.  
  589. /** 
  590. * PHP port of Javascript's "indexOf" function for strings only 
  591. * Author: Tubal Martin http://blog.margenn.com 
  592. * @param string $haystack 
  593. * @param string $needle 
  594. * @param int $offset index (optional) 
  595. * @return int 
  596. */ 
  597. private function index_of($haystack, $needle, $offset = 0) 
  598. $index = strpos($haystack, $needle, $offset); 
  599.  
  600. return ($index !== FALSE) ? $index : -1; 
  601.  
  602. /** 
  603. * PHP port of Javascript's "slice" function for strings only 
  604. * Author: Tubal Martin http://blog.margenn.com 
  605. * Tests: http://margenn.com/tubal/str_slice/ 
  606. * @param string $str 
  607. * @param int $start index 
  608. * @param int|bool $end index (optional) 
  609. * @return string 
  610. */ 
  611. private function str_slice($str, $start = 0, $end = FALSE) 
  612. if ($end !== FALSE && ($start < 0 || $end <= 0)) { 
  613. $max = strlen($str); 
  614.  
  615. if ($start < 0) { 
  616. if (($start = $max + $start) < 0) { 
  617. return ''; 
  618.  
  619. if ($end < 0) { 
  620. if (($end = $max + $end) < 0) { 
  621. return ''; 
  622.  
  623. if ($end <= $start) { 
  624. return ''; 
  625.  
  626. $slice = ($end === FALSE) ? substr($str, $start) : substr($str, $start, $end - $start); 
  627. return ($slice === FALSE) ? '' : $slice; 
  628.  
  629. /** 
  630. * Convert strings like "64M" or "30" to int values 
  631. * @param mixed $size 
  632. * @return int 
  633. */ 
  634. private function normalize_int($size) 
  635. if (is_string($size)) { 
  636. switch (substr($size, -1)) { 
  637. case 'M': case 'm': return $size * 1048576; 
  638. case 'K': case 'k': return $size * 1024; 
  639. case 'G': case 'g': return $size * 1073741824; 
  640.  
  641. return (int) $size;