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