Skip to content

Commit

Permalink
Fix issue with parsing media query that does not have expressions
Browse files Browse the repository at this point in the history
Fixes #5
  • Loading branch information
ericf committed Jan 21, 2014
1 parent 79b7c85 commit d1413c5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ exports.parse = parseQuery;

// -----------------------------------------------------------------------------

var RE_MEDIA_QUERY = /(?:(only|not)?\s*([^\s\(\)]+)\s*and\s*)?(.+)?/i,
var RE_MEDIA_QUERY = /(?:(only|not)?\s*([^\s\(\)]+)(?:\s*and)?\s*)?(.+)?/i,
RE_MQ_EXPRESSION = /\(\s*([^\s\:\)]+)\s*(?:\:\s*([^\s\)]+))?\s*\)/,
RE_MQ_FEATURE = /^(?:(min|max)-)?(.+)/,
RE_LENGTH_UNIT = /(em|rem|px|cm|mm|in|pt|pc)?$/,
Expand Down Expand Up @@ -86,10 +86,12 @@ function matchQuery(mediaQuery, values) {

function parseQuery(mediaQuery) {
return mediaQuery.split(',').map(function (query) {
query = query.trim();

var captures = query.match(RE_MEDIA_QUERY),
modifier = captures[1],
type = captures[2],
expressions = captures[3],
expressions = captures[3] || '',
parsed = {};

parsed.inverse = !!modifier && modifier.toLowerCase() === 'not';
Expand Down
22 changes: 21 additions & 1 deletion test/unit-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@
var expect = require('chai').expect,
mediaQuery = require('../');

describe('mediaQuery.parse()', function () {
it('should parse media queries without expressions', function () {
expect(mediaQuery.parse('screen')).to.eql([
{
inverse : false,
type : 'screen',
expressions: []
}
]);

expect(mediaQuery.parse('not screen')).to.eql([
{
inverse : true,
type : 'screen',
expressions: []
}
]);
});
});

describe('mediaQuery.match()', function () {
describe('Equality Check', function () {
it('Orientation: should return true for a correct match (===)', function () {
Expand Down Expand Up @@ -279,7 +299,7 @@ describe('mediaQuery.match()', function () {
});
});

describe('#mediaQuery.match() Integration Tests', function () {
describe('mediaQuery.match() Integration Tests', function () {
describe('Real World Use Cases (mostly AND)', function () {
it('should return true because of width and type match', function () {
expect(mediaQuery.match(
Expand Down

0 comments on commit d1413c5

Please sign in to comment.