1 | 'use strict';
|
---|
2 |
|
---|
3 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
---|
4 |
|
---|
5 | var postcss = require('postcss');
|
---|
6 | var postcss__default = _interopDefault(postcss);
|
---|
7 |
|
---|
8 | function 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 |
|
---|
21 | function 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
|
---|
28 | var validSelector = /&(?:[^\w-|]|$)/;
|
---|
29 | const replaceable = /&/g;
|
---|
30 |
|
---|
31 | function mergeSelectors(fromSelectors, toSelectors) {
|
---|
32 | return fromSelectors.reduce((selectors, fromSelector) => selectors.concat(toSelectors.map(toSelector => toSelector.replace(replaceable, fromSelector))), []);
|
---|
33 | }
|
---|
34 |
|
---|
35 | function 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 | }
|
---|
51 | const isRuleWithinRule = node => node.type === 'rule' && Object(node.parent).type === 'rule' && node.selectors.every(selector => selector.trim().lastIndexOf('&') === 0 && validSelector.test(selector));
|
---|
52 |
|
---|
53 | const comma = postcss.list.comma;
|
---|
54 | function 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 | }
|
---|
68 | const 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 |
|
---|
70 | var 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 |
|
---|
77 | function 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 | }
|
---|
89 | const isAtruleWithinRule = node => node.type === 'atrule' && validAtrules.indexOf(node.name) !== -1 && Object(node.parent).type === 'rule';
|
---|
90 |
|
---|
91 | const comma$1 = postcss.list.comma;
|
---|
92 | function 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 |
|
---|
101 | function 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 | }
|
---|
109 | const isAtruleWithinAtrule = node => node.type === 'atrule' && validAtrules.indexOf(node.name) !== -1 && Object(node.parent).type === 'atrule' && node.name === node.parent.name;
|
---|
110 |
|
---|
111 | function 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 |
|
---|
131 | var index = postcss__default.plugin('postcss-nesting', () => walk);
|
---|
132 |
|
---|
133 | module.exports = index;
|
---|
134 | //# sourceMappingURL=index.cjs.js.map
|
---|