You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
3.8 KiB

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