Skip to content

Commit 7a008b4

Browse files
committed
Initial drop
0 parents  commit 7a008b4

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

LICENSE

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright 2014 Yahoo! Inc.
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
10+
* Redistributions in binary form must reproduce the above copyright
11+
notice, this list of conditions and the following disclaimer in the
12+
documentation and/or other materials provided with the distribution.
13+
14+
* Neither the name of the Yahoo! Inc. nor the
15+
names of its contributors may be used to endorse or promote products
16+
derived from this software without specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
DISCLAIMED. IN NO EVENT SHALL YAHOO! INC. BE LIABLE FOR ANY
22+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
CSS Media Match
2+
===============
3+
4+
Determins if a givien CSS Media Query matches a set of values.
5+
6+
7+
License
8+
-------
9+
10+
This software is free to use under the Yahoo! Inc. BSD license.
11+
See the [LICENSE file][] for license text and copyright information.
12+
13+
14+
[LICENSE file]: https://github.com/ericf/css-media-match/blob/master/LICENSE

index.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
Copyright (c) 2014, Yahoo! Inc. All rights reserved.
3+
Copyrights licensed under the New BSD License.
4+
See the accompanying LICENSE file for terms.
5+
*/
6+
7+
'use strict';
8+
9+
module.exports = match;
10+
11+
// -----------------------------------------------------------------------------
12+
13+
function match(mediaQuery, values) {
14+
return parseQuery(mediaQuery);
15+
}
16+
17+
// -- Utilities ----------------------------------------------------------------
18+
19+
var RE_MEDIA_QUERY = /(?:(only|not)?\s*([^\s\(\)]+)\s*and\s*)?(.+)?/i,
20+
RE_MQ_EXPRESSION = /\(\s*([^\s\:\)]+)\s*(?:\:\s*([^\s\)]+))?\s*\)/,
21+
RE_LENGTH_VALUE = /(\d+)(em|rem|px|cm|mm|in|pt|pc)?/;
22+
23+
function parseQuery(mediaQuery) {
24+
return mediaQuery.split(',').map(function (query) {
25+
var captures = query.match(RE_MEDIA_QUERY),
26+
modifier = captures[1],
27+
type = captures[2],
28+
expressions = captures[3],
29+
parsed = {};
30+
31+
parsed.only = !!modifier && modifier.toLowerCase() === 'only';
32+
parsed.not = !!modifier && modifier.toLowerCase() === 'not';
33+
parsed.type = type ? type.toLowerCase() : 'all';
34+
35+
// Split expressions into a list.
36+
expressions = expressions.match(/\([^\)]+\)/ig);
37+
38+
parsed.expressions = expressions.map(function (expression) {
39+
var captures = expression.match(RE_MQ_EXPRESSION);
40+
41+
return {
42+
feature: captures[1].toLowerCase(),
43+
value : captures[2]
44+
};
45+
});
46+
47+
return parsed;
48+
});
49+
}
50+
51+
function toPx(length) {
52+
var captures = String(length).match(RE_LENGTH_VALUE),
53+
value = Number(captures[1]),
54+
units = captures[2];
55+
56+
switch (units) {
57+
case 'em':
58+
case 'rem':
59+
return value * 16;
60+
case 'cm':
61+
return value * 96 / 2.54;
62+
case 'mm':
63+
return value * 96 / 2.54 / 10;
64+
case 'in':
65+
return value * 96;
66+
case 'pt':
67+
return value * 72;
68+
case 'pc':
69+
return value * 72 / 12;
70+
default:
71+
return value;
72+
}
73+
}

0 commit comments

Comments
 (0)