@@ -6,22 +6,84 @@ See the accompanying LICENSE file for terms.
6
6
7
7
'use strict' ;
8
8
9
- module . exports = match ;
9
+ exports . match = matchQuery ;
10
+ exports . parse = parseQuery ;
10
11
11
12
// -----------------------------------------------------------------------------
12
13
13
- function match ( mediaQuery , values ) {
14
- return parseQuery ( mediaQuery ) ;
15
- }
16
-
17
- // -- Utilities ----------------------------------------------------------------
18
-
19
14
var RE_MEDIA_QUERY = / (?: ( o n l y | n o t ) ? \s * ( [ ^ \s \( \) ] + ) \s * a n d \s * ) ? ( .+ ) ? / i,
20
15
RE_MQ_EXPRESSION = / \( \s * ( [ ^ \s \: \) ] + ) \s * (?: \: \s * ( [ ^ \s \) ] + ) ) ? \s * \) / ,
21
16
RE_MQ_FEATURE = / ^ (?: ( m i n | m a x ) - ) ? ( .+ ) / ,
22
17
RE_LENGTH_UNIT = / ( e m | r e m | p x | c m | m m | i n | p t | p c ) ? $ / ,
23
18
RE_RESOLUTION_UNIT = / ( d p i | d p c m | d p p x ) ? $ / ;
24
19
20
+ function matchQuery ( mediaQuery , values ) {
21
+ return parseQuery ( mediaQuery ) . some ( function ( query ) {
22
+ var inverse = query . inverse ;
23
+
24
+ // Either the parsed or specified `type` is "all", or the types must be
25
+ // equal for a match.
26
+ var typeMatch = query . type === 'all' || values . type === 'all' ||
27
+ values . type === query . type ;
28
+
29
+ // Quit early when `type` doesn't match, but take "not" into account.
30
+ if ( ( typeMatch && inverse ) || ! ( typeMatch || inverse ) ) {
31
+ return false ;
32
+ }
33
+
34
+ var expressionsMatch = query . expressions . every ( function ( expression ) {
35
+ var feature = expression . feature ,
36
+ modifier = expression . modifier ,
37
+ expValue = expression . value ,
38
+ value = values [ feature ] ;
39
+
40
+ // Missing or falsy values don't match.
41
+ if ( ! value ) { return false ; }
42
+
43
+ switch ( feature ) {
44
+ case 'orientation' :
45
+ case 'scan' :
46
+ return value . toLowerCase ( ) === expValue . toLowerCase ( ) ;
47
+
48
+ case 'width' :
49
+ case 'height' :
50
+ case 'device-width' :
51
+ case 'device-height' :
52
+ expValue = toPx ( expValue ) ;
53
+ value = toPx ( value ) ;
54
+ break ;
55
+
56
+ case 'resolution' :
57
+ expValue = toDpi ( expValue ) ;
58
+ value = toDpi ( value ) ;
59
+ break ;
60
+
61
+ case 'aspect-ratio' :
62
+ case 'device-aspect-ratio' :
63
+ expValue = toDecimal ( expValue ) ;
64
+ value = toDecimal ( value ) ;
65
+ break ;
66
+
67
+ case 'grid' :
68
+ case 'color' :
69
+ case 'color-index' :
70
+ case 'monochrome' :
71
+ expValue = parseInt ( expValue , 10 ) || 1 ;
72
+ value = parseInt ( value , 10 ) || 0 ;
73
+ break ;
74
+ }
75
+
76
+ switch ( modifier ) {
77
+ case 'min' : return value >= expValue ;
78
+ case 'max' : return value <= expValue ;
79
+ default : return value === expValue ;
80
+ }
81
+ } ) ;
82
+
83
+ return ( expressionsMatch && ! inverse ) || ( ! expressionsMatch && inverse ) ;
84
+ } ) ;
85
+ }
86
+
25
87
function parseQuery ( mediaQuery ) {
26
88
return mediaQuery . split ( ',' ) . map ( function ( query ) {
27
89
var captures = query . match ( RE_MEDIA_QUERY ) ,
@@ -30,9 +92,8 @@ function parseQuery(mediaQuery) {
30
92
expressions = captures [ 3 ] ,
31
93
parsed = { } ;
32
94
33
- parsed . only = ! ! modifier && modifier . toLowerCase ( ) === 'only' ;
34
- parsed . not = ! ! modifier && modifier . toLowerCase ( ) === 'not' ;
35
- parsed . type = type ? type . toLowerCase ( ) : 'all' ;
95
+ parsed . inverse = ! ! modifier && modifier . toLowerCase ( ) === 'not' ;
96
+ parsed . type = type ? type . toLowerCase ( ) : 'all' ;
36
97
37
98
// Split expressions into a list.
38
99
expressions = expressions . match ( / \( [ ^ \) ] + \) / g) ;
@@ -42,9 +103,8 @@ function parseQuery(mediaQuery) {
42
103
feature = captures [ 1 ] . toLowerCase ( ) . match ( RE_MQ_FEATURE ) ;
43
104
44
105
return {
45
- feature : feature [ 0 ] ,
46
106
modifier : feature [ 1 ] ,
47
- property : feature [ 2 ] ,
107
+ feature : feature [ 2 ] ,
48
108
value : captures [ 2 ]
49
109
} ;
50
110
} ) ;
@@ -53,6 +113,8 @@ function parseQuery(mediaQuery) {
53
113
} ) ;
54
114
}
55
115
116
+ // -- Utilities ----------------------------------------------------------------
117
+
56
118
function toDecimal ( ratio ) {
57
119
var decimal = Number ( ratio ) ,
58
120
numbers ;
0 commit comments