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.

217 lines
5.2 KiB

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