-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
66 lines (56 loc) · 1.3 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
/**
* Module dependencies.
*/
var visit = require('rework-visit');
/**
* Define custom mixins.
*/
module.exports = function(mixins) {
if (!mixins) throw new Error('mixins object required');
return function(style, rework){
visit(style, function(declarations){
mixin(rework, declarations, mixins);
});
}
};
/**
* Visit declarations and apply mixins.
*
* @param {Rework} rework
* @param {Array} declarations
* @param {Object} mixins
* @api private
*/
function mixin(rework, declarations, mixins) {
for (var i = 0; i < declarations.length; ++i) {
var decl = declarations[i];
if ('comment' == decl.type) continue;
var key = decl.property;
var val = decl.value;
var fn = mixins[key];
if (!fn) continue;
// invoke mixin
var ret = fn.call(rework, val);
// apply properties
for (var key in ret) {
var val = ret[key];
if (Array.isArray(val)) {
val.forEach(function(val){
declarations.splice(i++, 0, {
type: 'declaration',
property: key,
value: val
});
});
} else {
declarations.splice(i++, 0, {
type: 'declaration',
property: key,
value: val
});
}
}
// remove original
declarations.splice(i--, 1);
}
}