[6a3a178] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.default = replaceRuleSelector;
|
---|
| 7 |
|
---|
| 8 | var _list = require("postcss/lib/list");
|
---|
| 9 |
|
---|
| 10 | var _list2 = _interopRequireDefault(_list);
|
---|
| 11 |
|
---|
| 12 | var _balancedMatch = require("balanced-match");
|
---|
| 13 |
|
---|
| 14 | var _balancedMatch2 = _interopRequireDefault(_balancedMatch);
|
---|
| 15 |
|
---|
| 16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
| 17 |
|
---|
| 18 | 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); } }
|
---|
| 19 |
|
---|
| 20 | var pseudoClass = ":matches";
|
---|
| 21 | var selectorElementRE = /^[a-zA-Z]/;
|
---|
| 22 |
|
---|
| 23 | function isElementSelector(selector) {
|
---|
| 24 | var matches = selectorElementRE.exec(selector);
|
---|
| 25 | // console.log({selector, matches})
|
---|
| 26 | return matches;
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | function normalizeSelector(selector, preWhitespace, pre) {
|
---|
| 30 | if (isElementSelector(selector) && !isElementSelector(pre)) {
|
---|
| 31 | return `${preWhitespace}${selector}${pre}`;
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | return `${preWhitespace}${pre}${selector}`;
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | function explodeSelector(selector, options) {
|
---|
| 38 | if (selector && selector.indexOf(pseudoClass) > -1) {
|
---|
| 39 | var newSelectors = [];
|
---|
| 40 | var preWhitespaceMatches = selector.match(/^\s+/);
|
---|
| 41 | var preWhitespace = preWhitespaceMatches ? preWhitespaceMatches[0] : "";
|
---|
| 42 | var selectorPart = _list2.default.comma(selector);
|
---|
| 43 | selectorPart.forEach(function (part) {
|
---|
| 44 | var position = part.indexOf(pseudoClass);
|
---|
| 45 | var pre = part.slice(0, position);
|
---|
| 46 | var body = part.slice(position);
|
---|
| 47 | var matches = (0, _balancedMatch2.default)("(", ")", body);
|
---|
| 48 |
|
---|
| 49 | var bodySelectors = matches && matches.body ? _list2.default.comma(matches.body).reduce(function (acc, s) {
|
---|
| 50 | return [].concat(_toConsumableArray(acc), _toConsumableArray(explodeSelector(s, options)));
|
---|
| 51 | }, []) : [body];
|
---|
| 52 |
|
---|
| 53 | var postSelectors = matches && matches.post ? explodeSelector(matches.post, options) : [];
|
---|
| 54 |
|
---|
| 55 | var newParts = void 0;
|
---|
| 56 | if (postSelectors.length === 0) {
|
---|
| 57 | // the test below is a poor way to try we are facing a piece of a
|
---|
| 58 | // selector...
|
---|
| 59 | if (position === -1 || pre.indexOf(" ") > -1) {
|
---|
| 60 | newParts = bodySelectors.map(function (s) {
|
---|
| 61 | return preWhitespace + pre + s;
|
---|
| 62 | });
|
---|
| 63 | } else {
|
---|
| 64 | newParts = bodySelectors.map(function (s) {
|
---|
| 65 | return normalizeSelector(s, preWhitespace, pre);
|
---|
| 66 | });
|
---|
| 67 | }
|
---|
| 68 | } else {
|
---|
| 69 | newParts = [];
|
---|
| 70 | postSelectors.forEach(function (postS) {
|
---|
| 71 | bodySelectors.forEach(function (s) {
|
---|
| 72 | newParts.push(preWhitespace + pre + s + postS);
|
---|
| 73 | });
|
---|
| 74 | });
|
---|
| 75 | }
|
---|
| 76 | newSelectors = [].concat(_toConsumableArray(newSelectors), _toConsumableArray(newParts));
|
---|
| 77 | });
|
---|
| 78 |
|
---|
| 79 | return newSelectors;
|
---|
| 80 | }
|
---|
| 81 | return [selector];
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | function replaceRuleSelector(rule, options) {
|
---|
| 85 | var indentation = rule.raws && rule.raws.before ? rule.raws.before.split("\n").pop() : "";
|
---|
| 86 | return explodeSelector(rule.selector, options).join("," + (options.lineBreak ? "\n" + indentation : " "));
|
---|
| 87 | }
|
---|
| 88 | module.exports = exports.default; |
---|