-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
85 lines (68 loc) · 2.38 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
"use strict";
module.exports = function (tasks, stylecow) {
tasks.addTask({
filter: 'ConditionalFeatureRange',
position: 'before',
fn: function (range) {
let expression = range.getParent();
range
.getChildren('Comparator')
.forEach(function (comparator, index) {
if (index !== 0) {
expression.before((new stylecow.Keyword()).setName('and'));
}
let exp = new stylecow.ConditionalExpression();
exp.push(resolve(comparator));
expression.before(exp);
});
let p = range.getParent();
expression.detach();
}
});
function resolve (comparator) {
let value = new stylecow.Value(),
feature = new stylecow.ConditionalFeature(),
comp = comparator.name,
unit = comparator.next();
if (comparator.prev().is('Keyword')) {
feature.setNameWithVendor(comparator.prev().name);
}
// (10px > width) => (width < 10px)
else {
unit = comparator.prev();
feature.setNameWithVendor(comparator.next().name);
if (comp.indexOf('<') !== -1) {
comp = comp.replace('<', '>');
} else if (comp.indexOf('>') !== -1) {
comp = comp.replace('>', '<');
}
}
feature.push(value);
value.push(unit);
switch (comp) {
case '<=':
return feature.setName('max-' + feature.name);
case '>=':
return feature.setName('min-' + feature.name);
case '<':
unitStep(unit, -0.001);
return feature.setName('max-' + feature.name);
case '>':
unitStep(unit, 0.001);
return feature.setName('min-' + feature.name);
default:
tasks.log('Error in the media query comparator', comparator);
}
}
function unitStep(unit, amount) {
if (unit.is('Number')) {
return unit.name += amount;
}
if (unit.is('Unit')) {
return unit.get('Number').name += amount;
}
if (unit.is('Ratio')) {
return unit.getAll('Number')[1].name += amount;
}
}
};