/application/libraries/Minify/drivers/Minify_css.php

https://github.com/sevir/Creamy · PHP · 158 lines · 61 code · 15 blank · 82 comment · 6 complexity · b96b786d5d8821ca16d197f88cb5b7f0 MD5 · raw file

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter Minify
  4. *
  5. * @package ci-minify
  6. * @author Eric Barnes, F.S. Encinas
  7. * @copyright Copyright (c) Eric Barnes
  8. * @since Version 1.0
  9. * @link http://ericlbarnes.com
  10. * @link https://github.com/fsencinas/ci-minify/
  11. */
  12. // ------------------------------------------------------------------------
  13. /**
  14. * Minify CSS Driver
  15. *
  16. * @subpackage Drivers
  17. */
  18. class Minify_css extends CI_Driver {
  19. /**
  20. * Constructor
  21. *
  22. * @return \Minify_css
  23. */
  24. public function __construct()
  25. {
  26. log_message('debug', 'Minify CSS Initialized');
  27. }
  28. // ------------------------------------------------------------------------
  29. /**
  30. * Min
  31. *
  32. * Minify a CSS file
  33. *
  34. * @param string $file
  35. * @param bool $compact
  36. * @param null $is_aggregated
  37. * @return string
  38. */
  39. public function min($file = '', $compact = TRUE, $is_aggregated = NULL)
  40. {
  41. if ($file == '' OR ! file_exists($file))
  42. {
  43. log_message('error', 'Minify_css->min missing file '.$file);
  44. return FALSE;
  45. }
  46. if ( ! isset($is_aggregated))
  47. {
  48. $contents = file_get_contents($file);
  49. }
  50. else
  51. {
  52. $contents = $this->remove_charsets(file_get_contents($file));
  53. }
  54. if ($compact != FALSE)
  55. {
  56. return trim($this->_optimize($contents))."\n";
  57. }
  58. else
  59. {
  60. return "\n".trim($contents)."\n\n";
  61. }
  62. }
  63. // ------------------------------------------------------------------------
  64. /**
  65. * Remove charsets
  66. *
  67. * Charset declarations removal to support do combine function
  68. * in order to set a new one user defined charset at the beggining of the document
  69. * to keep standars compliance (and fix Webkit buggy behaviours)
  70. *
  71. * @author F.S.Encinas
  72. * @param string $contents
  73. * @return string
  74. */
  75. private function remove_charsets($contents)
  76. {
  77. return preg_replace('/^@charset\s+[\'"](\S*)\b[\'"];/i', '', $contents);
  78. }
  79. // ------------------------------------------------------------------------
  80. /**
  81. * Optimize
  82. * Optimize the contents of a css file
  83. * based on Drupal 7 CSS Core aggregator
  84. *
  85. * @author F.S.Encinas
  86. * @param string $contents
  87. * @return string
  88. */
  89. private function _optimize($contents)
  90. {
  91. // Perform some safe CSS optimizations.
  92. // Regexp to match comment blocks.
  93. $comment = '/\*[^*]*\*+(?:[^/*][^*]*\*+)*/';
  94. // Regexp to match double quoted strings.
  95. $double_quot = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"';
  96. // Regexp to match single quoted strings.
  97. $single_quot = "'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'";
  98. // Strip all comment blocks, but keep double/single quoted strings.
  99. $contents = preg_replace(
  100. "<($double_quot|$single_quot)|$comment>Ss",
  101. "$1",
  102. $contents
  103. );
  104. // Remove certain whitespace.
  105. // There are different conditions for removing leading and trailing
  106. // whitespace.
  107. // @see http://php.net/manual/en/regexp.reference.subpatterns.php
  108. $contents = preg_replace_callback(
  109. '<' .
  110. # Strip leading and trailing whitespace.
  111. '\s*([@{};,])\s*' .
  112. # Strip only leading whitespace from:
  113. # - Closing parenthesis: Retain "@media (bar) and foo".
  114. '| \s+([\)])' .
  115. # Strip only trailing whitespace from:
  116. # - Opening parenthesis: Retain "@media (bar) and foo".
  117. # - Colon: Retain :pseudo-selectors.
  118. '| ([\(:])\s+' .
  119. '>xS',
  120. array(get_class($this), '_optimize_call_back'),
  121. $contents
  122. );
  123. return $contents;
  124. }
  125. /**
  126. * Optimize CB
  127. * Optimize Callback Helper companion for optimize fn
  128. * based on Drupal 7 CSS Core aggregator
  129. *
  130. * @author F.S.Encinas
  131. * @param string $matches
  132. * @return array
  133. */
  134. private function _optimize_call_back($matches)
  135. {
  136. // Discard the full match.
  137. unset($matches[0]);
  138. // Use the non-empty match.
  139. return current(array_filter($matches));
  140. }
  141. }
  142. /* End of file Minify_css.php */
  143. /* Location: ./application/libraries/Minify/drivers/Minify_css.php */