source: trip-planner-front/node_modules/postcss-nesting/index.cjs.js@ b738035

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

initial commit

  • Property mode set to 100644
File size: 5.0 KB
RevLine 
[6a3a178]1'use strict';
2
3function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
4
5var postcss = require('postcss');
6var postcss__default = _interopDefault(postcss);
7
8function shiftNodesBeforeParent(node) {
9 const parent = node.parent;
10 const index = parent.index(node); // conditionally move previous siblings into a clone of the parent
11
12 if (index) {
13 parent.cloneBefore().removeAll().append(parent.nodes.slice(0, index));
14 } // move the current node before the parent (and after the conditional clone)
15
16
17 parent.before(node);
18 return parent;
19}
20
21function cleanupParent(parent) {
22 if (!parent.nodes.length) {
23 parent.remove();
24 }
25}
26
27// a valid selector is an ampersand followed by a non-word character or nothing
28var validSelector = /&(?:[^\w-|]|$)/;
29const replaceable = /&/g;
30
31function mergeSelectors(fromSelectors, toSelectors) {
32 return fromSelectors.reduce((selectors, fromSelector) => selectors.concat(toSelectors.map(toSelector => toSelector.replace(replaceable, fromSelector))), []);
33}
34
35function transformRuleWithinRule(node) {
36 // move previous siblings and the node to before the parent
37 const parent = shiftNodesBeforeParent(node); // update the selectors of the node to be merged with the parent
38
39 node.selectors = mergeSelectors(parent.selectors, node.selectors); // merge similar rules back together
40 // eslint-disable-next-line no-extra-parens
41
42 const areSameRule = node.type === 'rule' && parent.type === 'rule' && node.selector === parent.selector || node.type === 'atrule' && parent.type === 'atrule' && node.params === parent.params;
43
44 if (areSameRule) {
45 node.append(...parent.nodes);
46 } // conditionally cleanup an empty parent rule
47
48
49 cleanupParent(parent);
50}
51const isRuleWithinRule = node => node.type === 'rule' && Object(node.parent).type === 'rule' && node.selectors.every(selector => selector.trim().lastIndexOf('&') === 0 && validSelector.test(selector));
52
53const comma = postcss.list.comma;
54function transformNestRuleWithinRule(node) {
55 // move previous siblings and the node to before the parent
56 const parent = shiftNodesBeforeParent(node); // clone the parent as a new rule with children appended to it
57
58 const rule = parent.clone().removeAll().append(node.nodes); // replace the node with the new rule
59
60 node.replaceWith(rule); // update the selectors of the node to be merged with the parent
61
62 rule.selectors = mergeSelectors(parent.selectors, comma(node.params)); // conditionally cleanup an empty parent rule
63
64 cleanupParent(parent); // walk the children of the new rule
65
66 walk(rule);
67}
68const isNestRuleWithinRule = node => node.type === 'atrule' && node.name === 'nest' && Object(node.parent).type === 'rule' && comma(node.params).every(selector => selector.split('&').length === 2 && validSelector.test(selector));
69
70var validAtrules = ['document', 'media', 'supports'];
71
72/*
73 * DEPRECATED: In v7.0.0 these features will be removed as they are not part of
74 * the nesting proposal.
75 */
76
77function atruleWithinRule(node) {
78 // move previous siblings and the node to before the parent
79 const parent = shiftNodesBeforeParent(node); // clone the parent as a new rule with children appended to it
80
81 const rule = parent.clone().removeAll().append(node.nodes); // append the new rule to the node
82
83 node.append(rule); // conditionally cleanup an empty parent rule
84
85 cleanupParent(parent); // walk the children of the new rule
86
87 walk(rule);
88}
89const isAtruleWithinRule = node => node.type === 'atrule' && validAtrules.indexOf(node.name) !== -1 && Object(node.parent).type === 'rule';
90
91const comma$1 = postcss.list.comma;
92function mergeParams(fromParams, toParams) {
93 return comma$1(fromParams).map(params1 => comma$1(toParams).map(params2 => `${params1} and ${params2}`).join(', ')).join(', ');
94}
95
96/*
97 * DEPRECATED: In v7.0.0 these features will be removed as they are not part of
98 * the nesting proposal.
99 */
100
101function transformAtruleWithinAtrule(node) {
102 // move previous siblings and the node to before the parent
103 const parent = shiftNodesBeforeParent(node); // update the params of the node to be merged with the parent
104
105 node.params = mergeParams(parent.params, node.params); // conditionally cleanup an empty parent rule
106
107 cleanupParent(parent);
108}
109const isAtruleWithinAtrule = node => node.type === 'atrule' && validAtrules.indexOf(node.name) !== -1 && Object(node.parent).type === 'atrule' && node.name === node.parent.name;
110
111function walk(node) {
112 node.nodes.slice(0).forEach(child => {
113 if (child.parent === node) {
114 if (isRuleWithinRule(child)) {
115 transformRuleWithinRule(child);
116 } else if (isNestRuleWithinRule(child)) {
117 transformNestRuleWithinRule(child);
118 } else if (isAtruleWithinRule(child)) {
119 atruleWithinRule(child);
120 } else if (isAtruleWithinAtrule(child)) {
121 transformAtruleWithinAtrule(child);
122 }
123
124 if (Object(child.nodes).length) {
125 walk(child);
126 }
127 }
128 });
129}
130
131var index = postcss__default.plugin('postcss-nesting', () => walk);
132
133module.exports = index;
134//# sourceMappingURL=index.cjs.js.map
Note: See TracBrowser for help on using the repository browser.