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