forked from kristoferjoseph/postcss-modular-scale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
86 lines (68 loc) · 2.61 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
var postcss = require('postcss');
var ModularScale = require('modular-scale');
var valueParser = require('postcss-value-parser');
var pluginName = 'postcss-modular-scale';
module.exports = postcss.plugin(pluginName, function(opts) {
opts = opts || {};
var ratios = opts.ratios;
var bases = opts.bases;
return function(css, result) {
var declarations = [];
var ms = null;
css.walkDecls(function(decl) {
if (!decl.value) {
return;
}
if (decl.parent.selector === ':root') {
if (decl.prop === '--ms-ratios') {
ratios = decl.value.split(',');
result.messages.push({
type: 'modular-scale-ratios',
plugin: pluginName,
text: 'Modular scale ratios: ' + ratios
});
}
if (decl.prop === '--ms-bases') {
bases = decl.value.split(',');
result.messages.push({
type: 'modular-scale-bases',
plugin: pluginName,
text: 'Modular scale bases: ' + bases
});
}
}
if (decl.value.indexOf('ms(') !== -1) {
declarations.push(decl);
}
});
ms = new ModularScale({
ratios: ratios,
bases: bases
});
declarations.forEach(function(decl) {
var parsedValue = valueParser(decl.value);
parsedValue.walk(function (node) {
if (node.type === 'function' && node.value === 'ms') {
if (node.nodes.length === 1 && node.nodes[0].type === 'word') {
var value = parseFloat(node.nodes[0].value);
if (!isNaN(value)) {
var newValue = ms(value);
node.type = 'word';
node.value = newValue;
result.messages.push({
type: 'modular-scale-result',
plugin: pluginName,
text: 'Modular scale for ' + decl.value + ' is ' + newValue
});
} else {
throw decl.error('Modular scale value should be a number', { plugin: pluginName });
}
} else {
throw decl.error('Modular scale value should be a number', { plugin: pluginName });
}
}
});
decl.value = parsedValue.toString();
});
};
});