-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
61 lines (50 loc) · 1.39 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
'use strict';
const parseValue = require('postcss-value-parser');
const { colorFunctions, colorWords, hexColorRegExp } = require('./colors.js');
function isColor(node) {
const value = node.value.toLowerCase();
if (node.type === 'word') {
return hexColorRegExp.test(value) || colorWords.includes(value);
}
if (node.type === 'function') {
return colorFunctions.includes(value);
}
return false;
}
function postcssColorImage({ compat = false, preserve = false } = {}) {
return {
postcssPlugin: 'postcss-color-image',
Declaration(declaration) {
// Skip useless parsing
if (!declaration.value.includes('image(')) {
return;
}
const value = parseValue(declaration.value);
value.walk((node) => {
if (
node.type !== 'function' ||
node.value !== 'image' ||
node.nodes.length !== 1 ||
!isColor(node.nodes[0])
) {
return;
}
node.value = 'linear-gradient';
if (compat) {
node.nodes.push({ type: 'div', value: ', ' }, node.nodes[0]);
} else {
const separator = { type: 'space', value: ' ' };
const position = { type: 'word', value: '0' };
node.nodes.push(separator, position, separator, position);
}
});
if (preserve) {
declaration.cloneBefore({ value: value.toString() });
} else {
declaration.value = value.toString();
}
},
};
}
postcssColorImage.postcss = true;
module.exports = postcssColorImage;