PageRenderTime 60ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/resources/compiler/cCSSCompiler.php

https://github.com/mibamur/amplesdk
PHP | 128 lines | 79 code | 26 blank | 23 comment | 2 complexity | 68cd0072484bd6420791182a4f2ff2f7 MD5 | raw file
Possible License(s): GPL-2.0
  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. // Opera
  52. $sCSS = preg_replace("/([\s;{])(text-overflow\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "o" . $sAfter, $sCSS);
  53. $sCSS = preg_replace("/([\s;{])(transition\-?\w*\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "o" . $sAfter, $sCSS);
  54. // WebKit
  55. $sCSS = preg_replace("/([\s;{])(text-overflow\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "webkit" . $sAfter, $sCSS);
  56. $sCSS = preg_replace("/([\s;{])(box-shadow\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "webkit" . $sAfter, $sCSS);
  57. $sCSS = preg_replace("/([\s;{])(outline-radius\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "webkit" . $sAfter, $sCSS);
  58. $sCSS = preg_replace("/([\s;{])(border-radius\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "webkit" . $sAfter, $sCSS);
  59. $sCSS = preg_replace("/([\s;{])(transition\-?\w*\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "webkit" . $sAfter, $sCSS);
  60. // Gecko
  61. $sCSS = preg_replace("/([\s;{])(text-overflow\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "moz" . $sAfter, $sCSS);
  62. $sCSS = preg_replace("/([\s;{])(box-shadow\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "moz" . $sAfter, $sCSS);
  63. $sCSS = preg_replace("/([\s;{])(outline-radius\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "moz" . $sAfter, $sCSS);
  64. $sCSS = preg_replace("/([\s;{])(border-radius\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "moz" . $sAfter, $sCSS);
  65. $sCSS = preg_replace("/([\s;{])(box-sizing\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "moz" . $sAfter, $sCSS);
  66. $sCSS = preg_replace("/([\s;{])(transition\-?\w*\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "moz" . $sAfter, $sCSS);
  67. //
  68. $sBefore = $sBefore . 'moz-border-radius-';
  69. $sAfter = ':$3$4';
  70. $sCSS = preg_replace("/([\s;{])(border-top-left-radius\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "topleft" . $sAfter, $sCSS);
  71. $sCSS = preg_replace("/([\s;{])(border-top-right-radius\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "topright" . $sAfter, $sCSS);
  72. $sCSS = preg_replace("/([\s;{])(border-bottom-left-radius\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "bottomleft" . $sAfter, $sCSS);
  73. $sCSS = preg_replace("/([\s;{])(border-bottom-right-radius\s*:\s*)([^\n;}]+)([\n;}])/", $sBefore . "bottomright" . $sAfter, $sCSS);
  74. // Remove prefix declaration
  75. $sCSS = preg_replace("/@namespace\s+([\w-]+\s+)?(url\()?(['\"])?[^'\";\s]+(['\"])?\)?;?/", "", $sCSS);
  76. //
  77. $aCSS = array();
  78. if (preg_match_all("/([^{]+)({[^}]*})/", $sCSS, $aRules)) {
  79. for ($nIndex = 0, $nLength = count($aRules[0]); $nIndex < $nLength; $nIndex++) {
  80. $sCSSSelector = $aRules[1][$nIndex];
  81. $sCSSSelector = preg_replace("/\|/", '-', $sCSSSelector); // Namespace
  82. $sCSSSelector = preg_replace("/(^|[\s>+~,}]|not\()([\w-])/", '$1.$2', $sCSSSelector); // Element
  83. $sCSSSelector = preg_replace("/\[([\w-]+)=?([\w-]+)?\]/", '-$1-$2', $sCSSSelector); // Attribute
  84. $sCSSSelector = preg_replace("/::/", '--', $sCSSSelector); // Pseudo-element
  85. $sCSSSelector = preg_replace("/:nth-child\((\d+)\)/", '_nth-child-$1', $sCSSSelector);// Pseudo-class nth-child
  86. $sCSSSelector = preg_replace("/:(?!last-child|first-child|not)/", '_', $sCSSSelector);// Pseudo-class
  87. // $sCSSSelector = preg_replace("/>/g, '--' + "gateway" + '>', $sCSSSelector);
  88. // $sCSSSelector = preg_replace("/(--gateway){2,}/g, '--' + "gateway", $sCSSSelector); // > selector
  89. $aCSS[] = $sCSSSelector;
  90. $aCSS[] = $aRules[2][$nIndex];
  91. }
  92. $sCSS = join('', $aCSS);
  93. }
  94. $this->output = $sCSS;
  95. }
  96. }
  97. function cCSSCompiler_replaceOpacity($matches) {
  98. return $matches[0] . ";" .
  99. "-ms-filter:\"progid:DXImageTransform.Microsoft.Alpha(opacity=" . ($matches[1] * 100) . ")\";".
  100. "*filter:progid:DXImageTransform.Microsoft.Alpha(opacity=" . ($matches[1] * 100) . ")";
  101. }
  102. ?>