prefixer.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. 'use strict';
  2. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. var Browsers = require('./browsers');
  5. var utils = require('./utils');
  6. var vendor = require('postcss').vendor;
  7. /**
  8. * Recursivly clone objects
  9. */
  10. function _clone(obj, parent) {
  11. var cloned = new obj.constructor();
  12. for (var _iterator = Object.keys(obj || {}), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  13. var _ref;
  14. if (_isArray) {
  15. if (_i >= _iterator.length) break;
  16. _ref = _iterator[_i++];
  17. } else {
  18. _i = _iterator.next();
  19. if (_i.done) break;
  20. _ref = _i.value;
  21. }
  22. var i = _ref;
  23. var value = obj[i];
  24. if (i === 'parent' && (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object') {
  25. if (parent) {
  26. cloned[i] = parent;
  27. }
  28. } else if (i === 'source' || i === null) {
  29. cloned[i] = value;
  30. } else if (value instanceof Array) {
  31. cloned[i] = value.map(function (x) {
  32. return _clone(x, cloned);
  33. });
  34. } else if (i !== '_autoprefixerPrefix' && i !== '_autoprefixerValues') {
  35. if ((typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object' && value !== null) {
  36. value = _clone(value, cloned);
  37. }
  38. cloned[i] = value;
  39. }
  40. }
  41. return cloned;
  42. }
  43. var Prefixer = function () {
  44. /**
  45. * Add hack to selected names
  46. */
  47. Prefixer.hack = function hack(klass) {
  48. var _this = this;
  49. if (!this.hacks) {
  50. this.hacks = {};
  51. }
  52. return klass.names.map(function (name) {
  53. _this.hacks[name] = klass;
  54. return _this.hacks[name];
  55. });
  56. };
  57. /**
  58. * Load hacks for some names
  59. */
  60. Prefixer.load = function load(name, prefixes, all) {
  61. var Klass = this.hacks && this.hacks[name];
  62. if (Klass) {
  63. return new Klass(name, prefixes, all);
  64. } else {
  65. return new this(name, prefixes, all);
  66. }
  67. };
  68. /**
  69. * Clone node and clean autprefixer custom caches
  70. */
  71. Prefixer.clone = function clone(node, overrides) {
  72. var cloned = _clone(node);
  73. for (var name in overrides) {
  74. cloned[name] = overrides[name];
  75. }
  76. return cloned;
  77. };
  78. function Prefixer(name, prefixes, all) {
  79. _classCallCheck(this, Prefixer);
  80. this.name = name;
  81. this.prefixes = prefixes;
  82. this.all = all;
  83. }
  84. /**
  85. * Find prefix in node parents
  86. */
  87. Prefixer.prototype.parentPrefix = function parentPrefix(node) {
  88. var prefix = void 0;
  89. if (typeof node._autoprefixerPrefix !== 'undefined') {
  90. prefix = node._autoprefixerPrefix;
  91. } else if (node.type === 'decl' && node.prop[0] === '-') {
  92. prefix = vendor.prefix(node.prop);
  93. } else if (node.type === 'root') {
  94. prefix = false;
  95. } else if (node.type === 'rule' && node.selector.indexOf(':-') !== -1 && /:(-\w+-)/.test(node.selector)) {
  96. prefix = node.selector.match(/:(-\w+-)/)[1];
  97. } else if (node.type === 'atrule' && node.name[0] === '-') {
  98. prefix = vendor.prefix(node.name);
  99. } else {
  100. prefix = this.parentPrefix(node.parent);
  101. }
  102. if (Browsers.prefixes().indexOf(prefix) === -1) {
  103. prefix = false;
  104. }
  105. node._autoprefixerPrefix = prefix;
  106. return node._autoprefixerPrefix;
  107. };
  108. /**
  109. * Clone node with prefixes
  110. */
  111. Prefixer.prototype.process = function process(node, result) {
  112. if (!this.check(node)) {
  113. return undefined;
  114. }
  115. var parent = this.parentPrefix(node);
  116. var prefixes = this.prefixes.filter(function (prefix) {
  117. return !parent || parent === utils.removeNote(prefix);
  118. });
  119. var added = [];
  120. for (var _iterator2 = prefixes, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
  121. var _ref2;
  122. if (_isArray2) {
  123. if (_i2 >= _iterator2.length) break;
  124. _ref2 = _iterator2[_i2++];
  125. } else {
  126. _i2 = _iterator2.next();
  127. if (_i2.done) break;
  128. _ref2 = _i2.value;
  129. }
  130. var prefix = _ref2;
  131. if (this.add(node, prefix, added.concat([prefix]), result)) {
  132. added.push(prefix);
  133. }
  134. }
  135. return added;
  136. };
  137. /**
  138. * Shortcut for Prefixer.clone
  139. */
  140. Prefixer.prototype.clone = function clone(node, overrides) {
  141. return Prefixer.clone(node, overrides);
  142. };
  143. return Prefixer;
  144. }();
  145. module.exports = Prefixer;