Advertisement
Guest User

Untitled

a guest
Dec 27th, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. (function (global) {
  2. function clean(css) {
  3. return css
  4. .replace(/\/\*[\W\w]*?\*\//g, "") // remove comments
  5. .replace(/^\s+|\s+$/g, "") // remove trailing spaces
  6. .replace(/\s*([:;{}])\s*/g, "$1") // remove trailing separator spaces
  7. .replace(/\};+/g, "}") // remove unnecessary separators
  8. .replace(/([^:;{}])}/g, "$1;}") // add trailing separators
  9. }
  10.  
  11. function refine(css, isBlock) {
  12. return /^@/.test(css) ? (css = css.split(" ")) && {
  13. "identifier": css.shift().substr(1).toLowerCase(),
  14. "parameters": css.join(" ")
  15. } : (isBlock ? /:$/ : /:/).test(css) ? (css = css.split(":")) && {
  16. "property": css.shift(),
  17. "value": css.join(":")
  18. } : css;
  19. }
  20.  
  21. function parse(css, regExp, object) {
  22. for (var m; (m = regExp.exec(css)) != null;) {
  23. if (m[2] == "{") object.block.push(object = {
  24. "selector": refine(m[1], true),
  25. "block": [],
  26. "parent": object
  27. });
  28. else if (m[2] == "}") object = object.parent;
  29. else if (m[2] == ";") object.block.push(refine(m[1]));
  30. }
  31. }
  32.  
  33. global.parseCSS = function (css) {
  34. return parse(clean(css), /([^{};]*)([;{}])/g, css = { block: [] }), css;
  35. };
  36.  
  37.  
  38. global.vendorCSS = function (css) {
  39. var newCSS = {}, block, i, e;
  40.  
  41. if (css.selector) newCSS.selector = css.selector;
  42.  
  43. if (css.property) newCSS.property = global.vendor.property[css.property] || css.property;
  44.  
  45. if (css.value) {
  46. newCSS.value = css.value;
  47.  
  48. for (i in global.vendor.value) {
  49. e = global.vendor.value[i];
  50. i = new RegExp(i, "g");
  51.  
  52. if (newCSS.value.match(i)) newCSS.value = newCSS.value.replace(i, e);
  53. }
  54. }
  55.  
  56. if (css.block) for (newCSS.block = [], i = 0; css.block[i]; ++i) newCSS.block[i] = global.vendorCSS(css.block[i]);
  57.  
  58. return newCSS;
  59. };
  60.  
  61. global.renderCSS = function (css) {
  62. var str = "", i;
  63.  
  64. if (css.selector) str += css.selector + "{";
  65.  
  66. if (css.property) str += css.property + ":" + css.value + ";";
  67.  
  68. for (i = 0; css.block && css.block[i]; ++i) str += global.renderCSS(css.block[i]);
  69.  
  70. if (css.selector) str += "}";
  71.  
  72. return str;
  73. };
  74.  
  75. global.vendor = function (vendorObject, vendorPrefixRe, camelToDashedRe, camelToDashedFn, style, linearGradient, e, m) {
  76. for (e in style) if (m = e.match(vendorPrefixRe)) {
  77. vendorObject.prefix = m[1].toLowerCase();
  78.  
  79. break;
  80. }
  81.  
  82. for (e in style) if (m = e.match(vendorPrefixRe)) vendorObject.property[e[m[1].length].toLowerCase() + e.slice(m[1].length + 1).replace(camelToDashedRe, camelToDashedFn)] = e.replace(camelToDashedRe, camelToDashedFn);
  83.  
  84. style.backgroundImage = "-" + vendorObject.prefix + "-" + linearGradient + "(red, red)", vendorObject.value[linearGradient] = style.backgroundImage ? "-" + vendorObject.prefix + "-" + linearGradient : linearGradient;
  85.  
  86. return vendorObject;
  87. }({
  88. property: {},
  89. value: {}
  90. }, /^(O|Moz|ms|webkit)[A-Z]/, /[A-Z]/g, function (e) {
  91. return "-" + e.toLowerCase()
  92. }, document.createElement("x-element").style, "linear-gradient");
  93. })(this);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement