source: trip-planner-front/node_modules/postcss-discard-duplicates/dist/index.js@ 6a3a178

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

initial commit

  • Property mode set to 100644
File size: 2.7 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8function noop() {}
9
10function trimValue(value) {
11 return value ? value.trim() : value;
12}
13
14function empty(node) {
15 return !node.nodes.filter(child => child.type !== 'comment').length;
16}
17
18function equals(a, b) {
19 if (a.type !== b.type) {
20 return false;
21 }
22
23 if (a.important !== b.important) {
24 return false;
25 }
26
27 if (a.raws && !b.raws || !a.raws && b.raws) {
28 return false;
29 }
30
31 switch (a.type) {
32 case 'rule':
33 if (a.selector !== b.selector) {
34 return false;
35 }
36
37 break;
38
39 case 'atrule':
40 if (a.name !== b.name || a.params !== b.params) {
41 return false;
42 }
43
44 if (a.raws && trimValue(a.raws.before) !== trimValue(b.raws.before)) {
45 return false;
46 }
47
48 if (a.raws && trimValue(a.raws.afterName) !== trimValue(b.raws.afterName)) {
49 return false;
50 }
51
52 break;
53
54 case 'decl':
55 if (a.prop !== b.prop || a.value !== b.value) {
56 return false;
57 }
58
59 if (a.raws && trimValue(a.raws.before) !== trimValue(b.raws.before)) {
60 return false;
61 }
62
63 break;
64 }
65
66 if (a.nodes) {
67 if (a.nodes.length !== b.nodes.length) {
68 return false;
69 }
70
71 for (let i = 0; i < a.nodes.length; i++) {
72 if (!equals(a.nodes[i], b.nodes[i])) {
73 return false;
74 }
75 }
76 }
77
78 return true;
79}
80
81function dedupeRule(last, nodes) {
82 let index = nodes.indexOf(last) - 1;
83
84 while (index >= 0) {
85 const node = nodes[index--];
86
87 if (node && node.type === 'rule' && node.selector === last.selector) {
88 last.each(child => {
89 if (child.type === 'decl') {
90 dedupeNode(child, node.nodes);
91 }
92 });
93
94 if (empty(node)) {
95 node.remove();
96 }
97 }
98 }
99}
100
101function dedupeNode(last, nodes) {
102 let index = ~nodes.indexOf(last) ? nodes.indexOf(last) - 1 : nodes.length - 1;
103
104 while (index >= 0) {
105 const node = nodes[index--];
106
107 if (node && equals(node, last)) {
108 node.remove();
109 }
110 }
111}
112
113const handlers = {
114 rule: dedupeRule,
115 atrule: dedupeNode,
116 decl: dedupeNode,
117 comment: noop
118};
119
120function dedupe(root) {
121 const {
122 nodes
123 } = root;
124
125 if (!nodes) {
126 return;
127 }
128
129 let index = nodes.length - 1;
130
131 while (index >= 0) {
132 let last = nodes[index--];
133
134 if (!last || !last.parent) {
135 continue;
136 }
137
138 dedupe(last);
139 handlers[last.type](last, nodes);
140 }
141}
142
143function pluginCreator() {
144 return {
145 postcssPlugin: 'postcss-discard-duplicates',
146
147 OnceExit(css) {
148 dedupe(css);
149 }
150
151 };
152}
153
154pluginCreator.postcss = true;
155var _default = pluginCreator;
156exports.default = _default;
157module.exports = exports.default;
Note: See TracBrowser for help on using the repository browser.