source: trip-planner-front/node_modules/postcss-selector-matches/dist/replaceRuleSelector.js@ ceaed42

Last change on this file since ceaed42 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.0 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = replaceRuleSelector;
7
8var _list = require("postcss/lib/list");
9
10var _list2 = _interopRequireDefault(_list);
11
12var _balancedMatch = require("balanced-match");
13
14var _balancedMatch2 = _interopRequireDefault(_balancedMatch);
15
16function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
18function _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
20var pseudoClass = ":matches";
21var selectorElementRE = /^[a-zA-Z]/;
22
23function isElementSelector(selector) {
24 var matches = selectorElementRE.exec(selector);
25 // console.log({selector, matches})
26 return matches;
27}
28
29function normalizeSelector(selector, preWhitespace, pre) {
30 if (isElementSelector(selector) && !isElementSelector(pre)) {
31 return `${preWhitespace}${selector}${pre}`;
32 }
33
34 return `${preWhitespace}${pre}${selector}`;
35}
36
37function 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
84function 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}
88module.exports = exports.default;
Note: See TracBrowser for help on using the repository browser.