prefixer.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. "use strict";
  2. function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } it = o[Symbol.iterator](); return it.next.bind(it); }
  3. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  4. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  5. var vendor = require('postcss').vendor;
  6. var Browsers = require('./browsers');
  7. var utils = require('./utils');
  8. /**
  9. * Recursively clone objects
  10. */
  11. function _clone(obj, parent) {
  12. var cloned = new obj.constructor();
  13. for (var _i = 0, _Object$keys = Object.keys(obj || {}); _i < _Object$keys.length; _i++) {
  14. var i = _Object$keys[_i];
  15. var value = obj[i];
  16. if (i === 'parent' && typeof value === 'object') {
  17. if (parent) {
  18. cloned[i] = parent;
  19. }
  20. } else if (i === 'source' || i === null) {
  21. cloned[i] = value;
  22. } else if (Array.isArray(value)) {
  23. cloned[i] = value.map(function (x) {
  24. return _clone(x, cloned);
  25. });
  26. } else if (i !== '_autoprefixerPrefix' && i !== '_autoprefixerValues') {
  27. if (typeof value === 'object' && value !== null) {
  28. value = _clone(value, cloned);
  29. }
  30. cloned[i] = value;
  31. }
  32. }
  33. return cloned;
  34. }
  35. var Prefixer = /*#__PURE__*/function () {
  36. /**
  37. * Add hack to selected names
  38. */
  39. Prefixer.hack = function hack(klass) {
  40. var _this = this;
  41. if (!this.hacks) {
  42. this.hacks = {};
  43. }
  44. return klass.names.map(function (name) {
  45. _this.hacks[name] = klass;
  46. return _this.hacks[name];
  47. });
  48. }
  49. /**
  50. * Load hacks for some names
  51. */
  52. ;
  53. Prefixer.load = function load(name, prefixes, all) {
  54. var Klass = this.hacks && this.hacks[name];
  55. if (Klass) {
  56. return new Klass(name, prefixes, all);
  57. } else {
  58. return new this(name, prefixes, all);
  59. }
  60. }
  61. /**
  62. * Clone node and clean autprefixer custom caches
  63. */
  64. ;
  65. Prefixer.clone = function clone(node, overrides) {
  66. var cloned = _clone(node);
  67. for (var name in overrides) {
  68. cloned[name] = overrides[name];
  69. }
  70. return cloned;
  71. };
  72. function Prefixer(name, prefixes, all) {
  73. this.prefixes = prefixes;
  74. this.name = name;
  75. this.all = all;
  76. }
  77. /**
  78. * Find prefix in node parents
  79. */
  80. var _proto = Prefixer.prototype;
  81. _proto.parentPrefix = function parentPrefix(node) {
  82. var prefix;
  83. if (typeof node._autoprefixerPrefix !== 'undefined') {
  84. prefix = node._autoprefixerPrefix;
  85. } else if (node.type === 'decl' && node.prop[0] === '-') {
  86. prefix = vendor.prefix(node.prop);
  87. } else if (node.type === 'root') {
  88. prefix = false;
  89. } else if (node.type === 'rule' && node.selector.includes(':-') && /:(-\w+-)/.test(node.selector)) {
  90. prefix = node.selector.match(/:(-\w+-)/)[1];
  91. } else if (node.type === 'atrule' && node.name[0] === '-') {
  92. prefix = vendor.prefix(node.name);
  93. } else {
  94. prefix = this.parentPrefix(node.parent);
  95. }
  96. if (!Browsers.prefixes().includes(prefix)) {
  97. prefix = false;
  98. }
  99. node._autoprefixerPrefix = prefix;
  100. return node._autoprefixerPrefix;
  101. }
  102. /**
  103. * Clone node with prefixes
  104. */
  105. ;
  106. _proto.process = function process(node, result) {
  107. if (!this.check(node)) {
  108. return undefined;
  109. }
  110. var parent = this.parentPrefix(node);
  111. var prefixes = this.prefixes.filter(function (prefix) {
  112. return !parent || parent === utils.removeNote(prefix);
  113. });
  114. var added = [];
  115. for (var _iterator = _createForOfIteratorHelperLoose(prefixes), _step; !(_step = _iterator()).done;) {
  116. var prefix = _step.value;
  117. if (this.add(node, prefix, added.concat([prefix]), result)) {
  118. added.push(prefix);
  119. }
  120. }
  121. return added;
  122. }
  123. /**
  124. * Shortcut for Prefixer.clone
  125. */
  126. ;
  127. _proto.clone = function clone(node, overrides) {
  128. return Prefixer.clone(node, overrides);
  129. };
  130. return Prefixer;
  131. }();
  132. module.exports = Prefixer;