-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (58 loc) · 1.97 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
var postcss = require('postcss');
function pseudoClassRegex(str) {
return new RegExp(":" + str + "\\([ '\"]*([^ '\"]*)[ '\"]*\\)", "gi"); // eslint-disable-line quotes,max-len
}
function replaceRegex(rule, pseudoClassDefs) {
pseudoClassDefs.forEach(function (pseudoClassDef) {
rule.selectors.forEach(function (sel, index) {
if (sel.match(pseudoClassDef.regex)) {
var newSels = [];
pseudoClassDef.replacedBy.forEach(function (replItem) {
newSels.push(sel.replace(pseudoClassDef.regex, replItem));
});
var x = rule.selectors.splice(0);
Array.prototype.splice.apply(x, [index, 1].concat(newSels));
rule.selector = x.join(',');
replaceRegex(rule, pseudoClassDefs);
}
});
});
}
module.exports = postcss.plugin('postcss-class-patterns', function (opts) { // eslint-disable-line no-unused-vars
opts = opts || {};
var pseudoClassDefs = [
{
regex: pseudoClassRegex('class-starts-with'),
replacedBy: [
'[class^="$1"]',
'[class*=" $1"]',
'[class*="\a$1"]',
'[class*="\c$1"]',
'[class*="\d$1"]',
'[class*="\9$1"]'
]
},
{
regex: pseudoClassRegex('class-ends-with'),
replacedBy: [
'[class$="$1"]',
'[class*="$1 "]',
'[class*="$1\a"]',
'[class*="$1\c"]',
'[class*="$1\d"]',
'[class*="$1\9"]'
]
},
{
regex: pseudoClassRegex('class-contains'),
replacedBy: [
'[class*="$1"]'
]
}
];
return function (css, result) { // eslint-disable-line no-unused-vars
css.walkRules(function (rule) {
replaceRegex(rule, pseudoClassDefs);
});
};
});