/resources/compiler/cCSSCompiler.php

https://github.com/h6w/amplesdk · PHP · 134 lines · 84 code · 26 blank · 24 comment · 2 complexity · 81c6c8f6d66f59a2a5a6edc64e976206 MD5 · raw file

  1. <?php
  2. class cCSSCompiler
  3. {
  4. var $output = "";
  5. var $debug = false;
  6. function cCSSCompiler() {
  7. }
  8. function readFromString($sString) {
  9. $this->output = $sString;
  10. }
  11. function readFromFile($sFileName) {
  12. $this->output = $this->output . join("", file($sFileName));
  13. }
  14. function getOutput() {
  15. return $this->output;
  16. }
  17. function stripComments() {
  18. $sData = $this->output;
  19. // Strip '/* comment */' comments
  20. $sData = preg_replace('/\/\*.+\*\//Us', "", $sData);
  21. $this->output = $sData;
  22. }
  23. function stripSpaces() {
  24. $sData = $this->output;
  25. // replace tabs with spaces
  26. $sData = str_replace(" ", " ", $sData);
  27. // Strip ' : ' spaces around
  28. $sData = preg_replace('/ *([=\+\:\|\^<>\{\};,]) */', '$1', $sData);
  29. // strip all more than one spaces
  30. $sData = preg_replace("/\s\s+/", "", $sData);
  31. $sData = preg_replace("/;;+/", ";", $sData);
  32. // $sData = str_replace(";}", "}", $sData); // If enabled, causes vendor prefixes regexp issues, todo later
  33. $this->output = $sData;
  34. }
  35. function obfuscate() {
  36. $sCSS = $this->output;
  37. // Rewrite display:inline-block to display:inline (IE8-)
  38. // display:inline-block
  39. // -> display:inline-block;
  40. // -> *display:inline;
  41. $sCSS = preg_replace("/display\s*:\s*inline-block/", "display:inline-block;zoom:1;*display:inline", $sCSS);
  42. // Rewrite opacity
  43. // opacity : .5
  44. // -> opacity: .5;
  45. // -> -ms-filter:Alpha(opacity=' + nOpacity * 100 + ');
  46. // -> filter:Alpha(opacity=' + nOpacity * 100 + ');
  47. $sCSS = preg_replace_callback("/(?:[^-])opacity\s*:\s*(\d?\.?\d+)/", "cCSSCompiler_replaceOpacity", $sCSS);
  48. // add vendor prefixed styles
  49. $sBefore = "$1$2$3$4-";
  50. $sAfter = "-$2$3$4";
  51. // IE
  52. $sCSS = preg_replace("/([\s;{])(transition\-?\w*\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "ms" . $sAfter, $sCSS);
  53. $sCSS = preg_replace("/([\s;{])(transform\-?\w*\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "ms" . $sAfter, $sCSS);
  54. // Opera
  55. $sCSS = preg_replace("/([\s;{])(text-overflow\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "o" . $sAfter, $sCSS);
  56. $sCSS = preg_replace("/([\s;{])(transition\-?\w*\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "o" . $sAfter, $sCSS);
  57. $sCSS = preg_replace("/([\s;{])(transform\-?\w*\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "o" . $sAfter, $sCSS);
  58. // WebKit
  59. $sCSS = preg_replace("/([\s;{])(text-overflow\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "webkit" . $sAfter, $sCSS);
  60. $sCSS = preg_replace("/([\s;{])(box-shadow\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "webkit" . $sAfter, $sCSS);
  61. $sCSS = preg_replace("/([\s;{])(outline-radius\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "webkit" . $sAfter, $sCSS);
  62. $sCSS = preg_replace("/([\s;{])(border-radius\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "webkit" . $sAfter, $sCSS);
  63. $sCSS = preg_replace("/([\s;{])(transition\-?\w*\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "webkit" . $sAfter, $sCSS);
  64. $sCSS = preg_replace("/([\s;{])(transform\-?\w*\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "webkit" . $sAfter, $sCSS);
  65. // Gecko
  66. $sCSS = preg_replace("/([\s;{])(text-overflow\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "moz" . $sAfter, $sCSS);
  67. $sCSS = preg_replace("/([\s;{])(box-shadow\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "moz" . $sAfter, $sCSS);
  68. $sCSS = preg_replace("/([\s;{])(outline-radius\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "moz" . $sAfter, $sCSS);
  69. $sCSS = preg_replace("/([\s;{])(border-radius\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "moz" . $sAfter, $sCSS);
  70. $sCSS = preg_replace("/([\s;{])(box-sizing\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "moz" . $sAfter, $sCSS);
  71. $sCSS = preg_replace("/([\s;{])(transition\-?\w*\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "moz" . $sAfter, $sCSS);
  72. $sCSS = preg_replace("/([\s;{])(transform\-?\w*\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "moz" . $sAfter, $sCSS);
  73. //
  74. $sBefore = $sBefore . 'moz-border-radius-';
  75. $sAfter = ':$3$4';
  76. $sCSS = preg_replace("/([\s;{])(border-top-left-radius\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "topleft" . $sAfter, $sCSS);
  77. $sCSS = preg_replace("/([\s;{])(border-top-right-radius\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "topright" . $sAfter, $sCSS);
  78. $sCSS = preg_replace("/([\s;{])(border-bottom-left-radius\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "bottomleft" . $sAfter, $sCSS);
  79. $sCSS = preg_replace("/([\s;{])(border-bottom-right-radius\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "bottomright" . $sAfter, $sCSS);
  80. // Remove prefix declaration
  81. $sCSS = preg_replace("/@namespace\s+([\w-]+\s+)?(url\()?(['\"])?[^'\";\s]+(['\"])?\)?;?/", "", $sCSS);
  82. //
  83. $aCSS = array();
  84. if (preg_match_all("/([^{]+)({[^}]*})/", $sCSS, $aRules)) {
  85. for ($nIndex = 0, $nLength = count($aRules[0]); $nIndex < $nLength; $nIndex++) {
  86. $sCSSSelector = $aRules[1][$nIndex];
  87. $sCSSSelector = preg_replace("/\|/", '-', $sCSSSelector); // Namespace
  88. $sCSSSelector = preg_replace("/(^|[\s>+~,}]|not\()([\w-])/", '$1.$2', $sCSSSelector); // Element
  89. $sCSSSelector = preg_replace("/\[([\w-]+)=?([\w-]+)?\]/", '-$1-$2', $sCSSSelector); // Attribute
  90. $sCSSSelector = preg_replace("/::/", '--', $sCSSSelector); // Pseudo-element
  91. $sCSSSelector = preg_replace("/:nth-child\((\d+)\)/", '_nth-child-$1', $sCSSSelector);// Pseudo-class nth-child
  92. $sCSSSelector = preg_replace("/:(?!last-child|first-child|not)/", '_', $sCSSSelector);// Pseudo-class
  93. // $sCSSSelector = preg_replace("/>/g, '--' + "gateway" + '>', $sCSSSelector);
  94. // $sCSSSelector = preg_replace("/(--gateway){2,}/g, '--' + "gateway", $sCSSSelector); // > selector
  95. $aCSS[] = $sCSSSelector;
  96. $aCSS[] = $aRules[2][$nIndex];
  97. }
  98. $sCSS = join('', $aCSS);
  99. }
  100. $this->output = $sCSS;
  101. }
  102. }
  103. function cCSSCompiler_replaceOpacity($matches) {
  104. return $matches[0] . ";" .
  105. "-ms-filter:\"progid:DXImageTransform.Microsoft.Alpha(opacity=" . ($matches[1] * 100) . ")\";".
  106. "*filter:progid:DXImageTransform.Microsoft.Alpha(opacity=" . ($matches[1] * 100) . ")";
  107. }
  108. ?>