index.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _postcss = require("postcss");
  6. var _postcss2 = _interopRequireDefault(_postcss);
  7. var _list = require("postcss/lib/list");
  8. var _list2 = _interopRequireDefault(_list);
  9. var _balancedMatch = require("balanced-match");
  10. var _balancedMatch2 = _interopRequireDefault(_balancedMatch);
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. function explodeSelector(pseudoClass, selector) {
  13. var position = locatePseudoClass(selector, pseudoClass);
  14. if (selector && position > -1) {
  15. var pre = selector.slice(0, position);
  16. var matches = (0, _balancedMatch2.default)("(", ")", selector.slice(position));
  17. if (!matches) {
  18. return selector;
  19. }
  20. var bodySelectors = matches.body ? _list2.default.comma(matches.body).map(function (s) {
  21. return explodeSelector(pseudoClass, s);
  22. }).join(`)${pseudoClass}(`) : "";
  23. var postSelectors = matches.post ? explodeSelector(pseudoClass, matches.post) : "";
  24. return `${pre}${pseudoClass}(${bodySelectors})${postSelectors}`;
  25. }
  26. return selector;
  27. }
  28. var patternCache = {};
  29. function locatePseudoClass(selector, pseudoClass) {
  30. patternCache[pseudoClass] = patternCache[pseudoClass] || new RegExp(`([^\\\\]|^)${pseudoClass}`);
  31. // The regex is used to ensure that selectors with
  32. // escaped colons in them are treated properly
  33. // Ex: .foo\:not-bar is a valid CSS selector
  34. // But it is not a reference to a pseudo selector
  35. var pattern = patternCache[pseudoClass];
  36. var position = selector.search(pattern);
  37. if (position === -1) {
  38. return -1;
  39. }
  40. // The offset returned by the regex may be off by one because
  41. // of it including the negated character match in the position
  42. return position + selector.slice(position).indexOf(pseudoClass);
  43. }
  44. function explodeSelectors(pseudoClass) {
  45. return function () {
  46. return function (css) {
  47. css.walkRules(function (rule) {
  48. if (rule.selector && rule.selector.indexOf(pseudoClass) > -1) {
  49. rule.selector = explodeSelector(pseudoClass, rule.selector);
  50. }
  51. });
  52. };
  53. };
  54. }
  55. exports.default = _postcss2.default.plugin("postcss-selector-not", explodeSelectors(":not"));
  56. module.exports = exports.default;