selector.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. "use strict";
  2. function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; }
  3. 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); }
  4. 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); }
  5. 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; }
  6. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _defaults(subClass, superClass); }
  7. var _require = require('postcss'),
  8. list = _require.list;
  9. var OldSelector = require('./old-selector');
  10. var Prefixer = require('./prefixer');
  11. var Browsers = require('./browsers');
  12. var utils = require('./utils');
  13. var Selector = /*#__PURE__*/function (_Prefixer) {
  14. _inheritsLoose(Selector, _Prefixer);
  15. function Selector(name, prefixes, all) {
  16. var _this;
  17. _this = _Prefixer.call(this, name, prefixes, all) || this;
  18. _this.regexpCache = {};
  19. return _this;
  20. }
  21. /**
  22. * Is rule selectors need to be prefixed
  23. */
  24. var _proto = Selector.prototype;
  25. _proto.check = function check(rule) {
  26. if (rule.selector.includes(this.name)) {
  27. return !!rule.selector.match(this.regexp());
  28. }
  29. return false;
  30. }
  31. /**
  32. * Return prefixed version of selector
  33. */
  34. ;
  35. _proto.prefixed = function prefixed(prefix) {
  36. return this.name.replace(/^(\W*)/, "$1" + prefix);
  37. }
  38. /**
  39. * Lazy loadRegExp for name
  40. */
  41. ;
  42. _proto.regexp = function regexp(prefix) {
  43. if (this.regexpCache[prefix]) {
  44. return this.regexpCache[prefix];
  45. }
  46. var name = prefix ? this.prefixed(prefix) : this.name;
  47. this.regexpCache[prefix] = new RegExp("(^|[^:\"'=])" + utils.escapeRegexp(name), 'gi');
  48. return this.regexpCache[prefix];
  49. }
  50. /**
  51. * All possible prefixes
  52. */
  53. ;
  54. _proto.possible = function possible() {
  55. return Browsers.prefixes();
  56. }
  57. /**
  58. * Return all possible selector prefixes
  59. */
  60. ;
  61. _proto.prefixeds = function prefixeds(rule) {
  62. var _this2 = this;
  63. if (rule._autoprefixerPrefixeds) {
  64. if (rule._autoprefixerPrefixeds[this.name]) {
  65. return rule._autoprefixerPrefixeds;
  66. }
  67. } else {
  68. rule._autoprefixerPrefixeds = {};
  69. }
  70. var prefixeds = {};
  71. if (rule.selector.includes(',')) {
  72. var ruleParts = list.comma(rule.selector);
  73. var toProcess = ruleParts.filter(function (el) {
  74. return el.includes(_this2.name);
  75. });
  76. var _loop = function _loop() {
  77. var prefix = _step.value;
  78. prefixeds[prefix] = toProcess.map(function (el) {
  79. return _this2.replace(el, prefix);
  80. }).join(', ');
  81. };
  82. for (var _iterator = _createForOfIteratorHelperLoose(this.possible()), _step; !(_step = _iterator()).done;) {
  83. _loop();
  84. }
  85. } else {
  86. for (var _iterator2 = _createForOfIteratorHelperLoose(this.possible()), _step2; !(_step2 = _iterator2()).done;) {
  87. var prefix = _step2.value;
  88. prefixeds[prefix] = this.replace(rule.selector, prefix);
  89. }
  90. }
  91. rule._autoprefixerPrefixeds[this.name] = prefixeds;
  92. return rule._autoprefixerPrefixeds;
  93. }
  94. /**
  95. * Is rule already prefixed before
  96. */
  97. ;
  98. _proto.already = function already(rule, prefixeds, prefix) {
  99. var index = rule.parent.index(rule) - 1;
  100. while (index >= 0) {
  101. var before = rule.parent.nodes[index];
  102. if (before.type !== 'rule') {
  103. return false;
  104. }
  105. var some = false;
  106. for (var key in prefixeds[this.name]) {
  107. var prefixed = prefixeds[this.name][key];
  108. if (before.selector === prefixed) {
  109. if (prefix === key) {
  110. return true;
  111. } else {
  112. some = true;
  113. break;
  114. }
  115. }
  116. }
  117. if (!some) {
  118. return false;
  119. }
  120. index -= 1;
  121. }
  122. return false;
  123. }
  124. /**
  125. * Replace selectors by prefixed one
  126. */
  127. ;
  128. _proto.replace = function replace(selector, prefix) {
  129. return selector.replace(this.regexp(), "$1" + this.prefixed(prefix));
  130. }
  131. /**
  132. * Clone and add prefixes for at-rule
  133. */
  134. ;
  135. _proto.add = function add(rule, prefix) {
  136. var prefixeds = this.prefixeds(rule);
  137. if (this.already(rule, prefixeds, prefix)) {
  138. return;
  139. }
  140. var cloned = this.clone(rule, {
  141. selector: prefixeds[this.name][prefix]
  142. });
  143. rule.parent.insertBefore(rule, cloned);
  144. }
  145. /**
  146. * Return function to fast find prefixed selector
  147. */
  148. ;
  149. _proto.old = function old(prefix) {
  150. return new OldSelector(this, prefix);
  151. };
  152. return Selector;
  153. }(Prefixer);
  154. module.exports = Selector;