This repository has been archived by the owner on Dec 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
59 lines (44 loc) · 2.16 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
var parser = require('postcss-value-parser');
var postcss = require('postcss');
module.exports = postcss.plugin('postcss-color-rgba-fallback', function (opts) {
var method = opts && /^(clone|warn)$/i.test(opts.method) ? opts.method.toLowerCase() : 'replace';
var props = opts && 'properties' in opts ? opts.properties : ['background', 'background-color', 'color', 'border', 'border-bottom', 'border-bottom-color', 'border-color', 'border-left', 'border-left-color', 'border-right', 'border-right-color', 'border-top', 'border-top-color', 'outline', 'outline-color'];
var filter = opts && opts.filter;
return function (css, result) {
css.walkRules(function (rule) {
var decls = {};
rule.nodes.forEach(function (decl) {
if (decl.prop in decls) decls[decl.prop] = false;
else decls[decl.prop] = (!props || ~props.indexOf(decl.prop)) && /\brgba\b/i.test(decl.value) ? decl : false;
});
Object.keys(decls).forEach(function (name) {
var decl = decls[name];
if (!decl) return;
var isbg = /^background/i.test(decl.prop);
var isbf = isbg && filter;
var value = parser(decl.value).walk(function (node) {
if (node.type === 'function' && node.value === 'rgba') {
if (method === 'warn') return result.warn('rgba() detected', { node: decl });
var rgba = [node.nodes[0], node.nodes[2], node.nodes[4], node.nodes[6]].map(function (word, index) {
return (0 + Math.round(word.value * (index === 3 ? 255 : 1)).toString(16)).slice(-2);
});
var argb = rgba.slice(-1).concat(rgba.slice(0, -1)).join('');
var hex = rgba[3] === '00' ? 'transparent' : '#' + rgba.slice(0, 3).join('');
var word = isbf ? 'progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr=\'#' + argb + '\',endColorstr=\'#' + argb + '\')' : hex;
node.value = word;
node.type = 'word';
if (isbf) {
decl.value = parser.stringify(node);
node.value = hex;
}
}
}).toString();
if (method !== 'warn' && value !== decl.value) {
if (method === 'clone' || isbf) decl.cloneBefore({ value: value });
else decl.value = value;
if (isbf) decl.prop = 'filter';
}
});
});
};
});