-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
90 lines (77 loc) · 2.55 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
86
87
88
89
90
'use strict';
const mediaQuery = require('css-mediaquery');
exports.dimensionsIn = dimensionsIn;
function dimensionsIn(dimensions, css) {
let reg = new RegExp("(@media)(.*?)(\{)", 'g');
let matches = css.match(reg);
if (!matches) {
return [];
}
let screens = matches.filter((rule, index) => {
rule = rule.replace(': ', ':');
if (rule.indexOf('print')>0 || matches.indexOf(rule)!==index)
return false;
return true;
});
matches = screens.map(rule => {
rule = rule.substring(7, rule.length-1).trim();
return rule;
});
let keep = [];
let ast = null;
let mq = null;
for (let i = matches.length - 1; i >= 0; i--) {
mq = matches[i];
if (mq.indexOf('min-w')<0 && mq.indexOf('max-w')<0) {
continue;
}
if (mq.indexOf('device-pixel-ratio')>0) {
continue;
}
if (mq.indexOf('2 / 1')>0) {
mq = mq.replace('2 / 1', '2/1');
}
// avoid errors like screen and (max-width:) in claraluzroldan.com.co
if (mq.indexOf(':)')>0) {
continue;
}
// WTF?
if(mq.indexOf('min-')===0 || mq.indexOf('max-')===0) {
mq = '(' + mq;
}
// (min-width:1440px + 24px * 4)
if (mq.search('px')>0 && mq.search(/px\)/)<0 ) {
mq = mq.replace(/px.*?\)/g, 'px)');
}
// console.log('\nChecking rule:', mq);
ast = mediaQuery.parse(mq);
if(ast[0].expressions.length===0) {
console.log('No expressions found on', ast[0]);
continue;
}
let exp = ast[0].expressions[0];
let k = 0;
let _mq = mq;
let _mqValue = parseInt(exp.value.replace('px', ''));
// console.log('==> Evaluating: ', _mq);
if(exp.modifier==='min') {
while( k < dimensions.length && mediaQuery.match(_mq, {type:'screen', width: dimensions[k].width}) ) {
k++;
}
k--;
} else if(exp.modifier==='max') {
// while( k < dimensions.length && !mediaQuery.match(_mq, {type:'screen', width: dimensions[k].width}) ) {
while( k < dimensions.length && _mqValue<dimensions[k].width ) {
k++;
}
k--;
}
if(k > 0 && k < dimensions.length) {
// console.log(' - Match:', dimensions[k]);
if (keep.indexOf(dimensions[k]) < 0) {
keep.push( dimensions[k] );
}
}
}
return keep.sort();
}