-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
191 lines (163 loc) · 5.41 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
var plugin = function (options) {
options = options || {};
options.preserveBeforeAfter = options.preserveBeforeAfter || true;
// Backwards compatibility--we always by default ignored `:root`.
var blacklist = {
':root': true,
':host': true,
':host-context': true
};
var prefix = options.prefix || '\\:';
(options.blacklist || []).forEach(function (blacklistItem) {
blacklist[blacklistItem] = true;
});
var restrictTo;
if (Array.isArray(options.restrictTo) && options.restrictTo.length) {
restrictTo = options.restrictTo.reduce(function (target, pseudoClass) {
var finalClass =
(pseudoClass.charAt(0) === ':' ? '' : ':') +
pseudoClass.replace(/\(.*/g, '');
if (!Object.prototype.hasOwnProperty.call(target, finalClass)) {
target[finalClass] = true;
}
return target;
}, {});
}
return {
postcssPlugin: 'postcss-pseudo-classes',
prepare: function () {
var fixed = [];
return {
Rule: function (rule) {
if (fixed.indexOf(rule) !== -1) {
return;
}
fixed.push(rule);
var combinations;
rule.selectors.forEach(function (selector) {
// Ignore some popular things that are never useful
if (blacklist[selector]) {
return;
}
if (!selector.includes(':')) {
return;
}
var selectorParts = selector.split(' ');
var pseudoedSelectorParts = [];
selectorParts.forEach(function (selectorPart, index) {
var pseudos = selectorPart.match(/::?([^:]+)/g);
if (!pseudos) {
if (options.allCombinations) {
pseudoedSelectorParts[index] = [selectorPart];
} else {
pseudoedSelectorParts.push(selectorPart);
}
return;
}
var baseSelector = selectorPart.substr(
0,
selectorPart.length - pseudos.join('').length
);
var classPseudos = pseudos.map(function (pseudo) {
var pseudoToCheck = pseudo.replace(/\(.*/g, '');
// restrictTo a subset of pseudo classes
if (
blacklist[pseudoToCheck] ||
(restrictTo && !restrictTo[pseudoToCheck])
) {
return pseudo;
}
// Ignore pseudo-elements!
if (pseudo.match(/^::/)) {
return pseudo;
}
// Ignore ':before' and ':after'
if (
options.preserveBeforeAfter &&
[':before', ':after'].indexOf(pseudo) !== -1
) {
return pseudo;
}
// Kill the colon
pseudo = pseudo.substr(1);
// Replace left and right parens
pseudo = pseudo.replace(/\(/g, '\\(');
pseudo = pseudo.replace(/\)/g, '\\)');
return '.' + prefix + pseudo;
});
// Add all combinations of pseudo selectors/pseudo styles given a
// selector with multiple pseudo styles.
if (options.allCombinations) {
combinations = createCombinations(pseudos, classPseudos);
pseudoedSelectorParts[index] = [];
combinations.forEach(function (combination) {
pseudoedSelectorParts[index].push(baseSelector + combination);
});
} else {
pseudoedSelectorParts.push(
baseSelector + classPseudos.join('')
);
}
});
if (options.allCombinations) {
var serialCombinations = createSerialCombinations(
pseudoedSelectorParts,
appendWithSpace
);
serialCombinations.forEach(function (combination) {
addSelector(combination);
});
} else {
addSelector(pseudoedSelectorParts.join(' '));
}
function addSelector(newSelector) {
if (newSelector && newSelector !== selector) {
rule.selector += ',\n' + newSelector;
}
}
});
}
};
}
};
};
plugin.postcss = true;
// a.length === b.length
function createCombinations(a, b) {
var combinations = [''];
var newCombinations;
for (var i = 0, len = a.length; i < len; i += 1) {
newCombinations = [];
combinations.forEach(function (combination) {
newCombinations.push(combination + a[i]);
// Don't repeat work.
if (a[i] !== b[i]) {
newCombinations.push(combination + b[i]);
}
});
combinations = newCombinations;
}
return combinations;
}
// arr = [[list of 1st el], [list of 2nd el] ... etc]
function createSerialCombinations(arr, fn) {
var combinations = [''];
var newCombinations;
arr.forEach(function (elements) {
newCombinations = [];
elements.forEach(function (element) {
combinations.forEach(function (combination) {
newCombinations.push(fn(combination, element));
});
});
combinations = newCombinations;
});
return combinations;
}
function appendWithSpace(a, b) {
if (a) {
a += ' ';
}
return a + b;
}
module.exports = plugin;