prefixer.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. (function() {
  2. var Browsers, Prefixer, clone, utils, vendor,
  3. hasProp = {}.hasOwnProperty;
  4. Browsers = require('./browsers');
  5. utils = require('./utils');
  6. vendor = require('postcss/lib/vendor');
  7. clone = function(obj, parent) {
  8. var cloned, i, value;
  9. cloned = new obj.constructor();
  10. for (i in obj) {
  11. if (!hasProp.call(obj, i)) continue;
  12. value = obj[i];
  13. if (i === 'parent' && typeof value === 'object') {
  14. if (parent) {
  15. cloned[i] = parent;
  16. }
  17. } else if (i === 'source') {
  18. cloned[i] = value;
  19. } else if (i === null) {
  20. cloned[i] = value;
  21. } else if (value instanceof Array) {
  22. cloned[i] = value.map(function(i) {
  23. return clone(i, cloned);
  24. });
  25. } else if (i !== '_autoprefixerPrefix' && i !== '_autoprefixerValues') {
  26. if (typeof value === 'object' && value !== null) {
  27. value = clone(value, cloned);
  28. }
  29. cloned[i] = value;
  30. }
  31. }
  32. return cloned;
  33. };
  34. Prefixer = (function() {
  35. Prefixer.hack = function(klass) {
  36. var j, len, name, ref, results;
  37. this.hacks || (this.hacks = {});
  38. ref = klass.names;
  39. results = [];
  40. for (j = 0, len = ref.length; j < len; j++) {
  41. name = ref[j];
  42. results.push(this.hacks[name] = klass);
  43. }
  44. return results;
  45. };
  46. Prefixer.load = function(name, prefixes, all) {
  47. var klass, ref;
  48. klass = (ref = this.hacks) != null ? ref[name] : void 0;
  49. if (klass) {
  50. return new klass(name, prefixes, all);
  51. } else {
  52. return new this(name, prefixes, all);
  53. }
  54. };
  55. Prefixer.clone = function(node, overrides) {
  56. var cloned, name;
  57. cloned = clone(node);
  58. for (name in overrides) {
  59. cloned[name] = overrides[name];
  60. }
  61. return cloned;
  62. };
  63. function Prefixer(name1, prefixes1, all1) {
  64. this.name = name1;
  65. this.prefixes = prefixes1;
  66. this.all = all1;
  67. }
  68. Prefixer.prototype.parentPrefix = function(node) {
  69. var prefix;
  70. prefix = node._autoprefixerPrefix != null ? node._autoprefixerPrefix : node.type === 'decl' && node.prop[0] === '-' ? vendor.prefix(node.prop) : node.type === 'root' ? false : node.type === 'rule' && node.selector.indexOf(':-') !== -1 && /:(-\w+-)/.test(node.selector) ? node.selector.match(/:(-\w+-)/)[1] : node.type === 'atrule' && node.name[0] === '-' ? vendor.prefix(node.name) : this.parentPrefix(node.parent);
  71. if (Browsers.prefixes().indexOf(prefix) === -1) {
  72. prefix = false;
  73. }
  74. return node._autoprefixerPrefix = prefix;
  75. };
  76. Prefixer.prototype.process = function(node) {
  77. var added, j, k, len, len1, parent, prefix, prefixes, ref;
  78. if (!this.check(node)) {
  79. return;
  80. }
  81. parent = this.parentPrefix(node);
  82. prefixes = [];
  83. ref = this.prefixes;
  84. for (j = 0, len = ref.length; j < len; j++) {
  85. prefix = ref[j];
  86. if (parent && parent !== utils.removeNote(prefix)) {
  87. continue;
  88. }
  89. prefixes.push(prefix);
  90. }
  91. added = [];
  92. for (k = 0, len1 = prefixes.length; k < len1; k++) {
  93. prefix = prefixes[k];
  94. if (this.add(node, prefix, added.concat([prefix]))) {
  95. added.push(prefix);
  96. }
  97. }
  98. return added;
  99. };
  100. Prefixer.prototype.clone = function(node, overrides) {
  101. return Prefixer.clone(node, overrides);
  102. };
  103. return Prefixer;
  104. })();
  105. module.exports = Prefixer;
  106. }).call(this);