replaceRuleSelector.js
3.03 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = replaceRuleSelector;
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 _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
var pseudoClass = ":matches";
var selectorElementRE = /^[a-zA-Z]/;
function isElementSelector(selector) {
var matches = selectorElementRE.exec(selector);
// console.log({selector, matches})
return matches;
}
function normalizeSelector(selector, preWhitespace, pre) {
if (isElementSelector(selector) && !isElementSelector(pre)) {
return `${preWhitespace}${selector}${pre}`;
}
return `${preWhitespace}${pre}${selector}`;
}
function explodeSelector(selector, options) {
if (selector && selector.indexOf(pseudoClass) > -1) {
var newSelectors = [];
var preWhitespaceMatches = selector.match(/^\s+/);
var preWhitespace = preWhitespaceMatches ? preWhitespaceMatches[0] : "";
var selectorPart = _list2.default.comma(selector);
selectorPart.forEach(function (part) {
var position = part.indexOf(pseudoClass);
var pre = part.slice(0, position);
var body = part.slice(position);
var matches = (0, _balancedMatch2.default)("(", ")", body);
var bodySelectors = matches && matches.body ? _list2.default.comma(matches.body).reduce(function (acc, s) {
return [].concat(_toConsumableArray(acc), _toConsumableArray(explodeSelector(s, options)));
}, []) : [body];
var postSelectors = matches && matches.post ? explodeSelector(matches.post, options) : [];
var newParts = void 0;
if (postSelectors.length === 0) {
// the test below is a poor way to try we are facing a piece of a
// selector...
if (position === -1 || pre.indexOf(" ") > -1) {
newParts = bodySelectors.map(function (s) {
return preWhitespace + pre + s;
});
} else {
newParts = bodySelectors.map(function (s) {
return normalizeSelector(s, preWhitespace, pre);
});
}
} else {
newParts = [];
postSelectors.forEach(function (postS) {
bodySelectors.forEach(function (s) {
newParts.push(preWhitespace + pre + s + postS);
});
});
}
newSelectors = [].concat(_toConsumableArray(newSelectors), _toConsumableArray(newParts));
});
return newSelectors;
}
return [selector];
}
function replaceRuleSelector(rule, options) {
var indentation = rule.raws && rule.raws.before ? rule.raws.before.split("\n").pop() : "";
return explodeSelector(rule.selector, options).join("," + (options.lineBreak ? "\n" + indentation : " "));
}
module.exports = exports.default;