-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
50 lines (43 loc) · 1.05 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
'use strict';
var postcss = require('postcss');
var contrast = require('contrast');
/**
* PostCSS plugin to transform contrast()
*/
module.exports = postcss.plugin('postcss-contrast', function(opts) {
opts = opts || {};
var dark = opts.dark;
var light = opts.light;
return function(css) {
css.walkDecls(function(decl) {
if (!decl.value || decl.value.indexOf('contrast(') === -1) {
return;
}
var index = decl.value.indexOf('(');
var last = decl.value.indexOf(')');
var value = decl.value.slice(++index, last);
var black = '#000';
var white = '#fff';
return new Promise(function(resolve) {
if (contrast(value) === 'light') {
if (!dark) {
decl.value = black;
}
else {
decl.value = opts.dark;
}
resolve();
}
else {
if (!light) {
decl.value = white;
}
else {
decl.value = opts.light;
}
resolve();
}
});
});
};
});