-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.js
180 lines (143 loc) · 5.53 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
Copyright (c) 2014, Yahoo! Inc. All rights reserved.
Copyrights licensed under the New BSD License.
See the accompanying LICENSE file for terms.
*/
'use strict';
exports.match = matchQuery;
exports.parse = parseQuery;
// -----------------------------------------------------------------------------
var RE_MEDIA_QUERY = /^(?:(only|not)?\s*([_a-z][_a-z0-9-]*)|(\([^\)]+\)))(?:\s*and\s*(.*))?$/i,
RE_MQ_EXPRESSION = /^\(\s*([_a-z-][_a-z0-9-]*)\s*(?:\:\s*([^\)]+))?\s*\)$/,
RE_MQ_FEATURE = /^(?:(min|max)-)?(.+)/,
RE_LENGTH_UNIT = /(em|rem|px|cm|mm|in|pt|pc)?\s*$/,
RE_RESOLUTION_UNIT = /(dpi|dpcm|dppx)?\s*$/;
function matchQuery(mediaQuery, values) {
return parseQuery(mediaQuery).some(function (query) {
var inverse = query.inverse;
// Either the parsed or specified `type` is "all", or the types must be
// equal for a match.
var typeMatch = query.type === 'all' || values.type === query.type;
// Quit early when `type` doesn't match, but take "not" into account.
if ((typeMatch && inverse) || !(typeMatch || inverse)) {
return false;
}
var expressionsMatch = query.expressions.every(function (expression) {
var feature = expression.feature,
modifier = expression.modifier,
expValue = expression.value,
value = values[feature];
// Missing or falsy values don't match.
if (!value) { return false; }
switch (feature) {
case 'orientation':
case 'scan':
return value.toLowerCase() === expValue.toLowerCase();
case 'width':
case 'height':
case 'device-width':
case 'device-height':
expValue = toPx(expValue);
value = toPx(value);
break;
case 'resolution':
expValue = toDpi(expValue);
value = toDpi(value);
break;
case 'aspect-ratio':
case 'device-aspect-ratio':
case /* Deprecated */ 'device-pixel-ratio':
expValue = toDecimal(expValue);
value = toDecimal(value);
break;
case 'grid':
case 'color':
case 'color-index':
case 'monochrome':
expValue = parseInt(expValue, 10) || 1;
value = parseInt(value, 10) || 0;
break;
}
switch (modifier) {
case 'min': return value >= expValue;
case 'max': return value <= expValue;
default : return value === expValue;
}
});
return (expressionsMatch && !inverse) || (!expressionsMatch && inverse);
});
}
function parseQuery(mediaQuery) {
return mediaQuery.split(',').map(function (query) {
query = query.trim();
var captures = query.match(RE_MEDIA_QUERY);
// Media Query must be valid.
if (!captures) {
throw new SyntaxError('Invalid CSS media query: "' + query + '"');
}
var modifier = captures[1],
type = captures[2],
expressions = ((captures[3] || '') + (captures[4] || '')).trim(),
parsed = {};
parsed.inverse = !!modifier && modifier.toLowerCase() === 'not';
parsed.type = type ? type.toLowerCase() : 'all';
// Check for media query expressions.
if (!expressions) {
parsed.expressions = [];
return parsed;
}
// Split expressions into a list.
expressions = expressions.match(/\([^\)]+\)/g);
// Media Query must be valid.
if (!expressions) {
throw new SyntaxError('Invalid CSS media query: "' + query + '"');
}
parsed.expressions = expressions.map(function (expression) {
var captures = expression.match(RE_MQ_EXPRESSION);
// Media Query must be valid.
if (!captures) {
throw new SyntaxError('Invalid CSS media query: "' + query + '"');
}
var feature = captures[1].toLowerCase().match(RE_MQ_FEATURE);
return {
modifier: feature[1],
feature : feature[2],
value : captures[2]
};
});
return parsed;
});
}
// -- Utilities ----------------------------------------------------------------
function toDecimal(ratio) {
var decimal = Number(ratio),
numbers;
if (!decimal) {
numbers = ratio.match(/^(\d+)\s*\/\s*(\d+)$/);
decimal = numbers[1] / numbers[2];
}
return decimal;
}
function toDpi(resolution) {
var value = parseFloat(resolution),
units = String(resolution).match(RE_RESOLUTION_UNIT)[1];
switch (units) {
case 'dpcm': return value / 2.54;
case 'dppx': return value * 96;
default : return value;
}
}
function toPx(length) {
var value = parseFloat(length),
units = String(length).match(RE_LENGTH_UNIT)[1];
switch (units) {
case 'em' : return value * 16;
case 'rem': return value * 16;
case 'cm' : return value * 96 / 2.54;
case 'mm' : return value * 96 / 2.54 / 10;
case 'in' : return value * 96;
case 'pt' : return value * 72;
case 'pc' : return value * 72 / 12;
default : return value;
}
}