Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix a match case for not operator
  • Loading branch information
kristerkari committed May 6, 2018
commit b31a87106411e730d1d75f506feb840fabf9184e
33 changes: 33 additions & 0 deletions src/__tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,39 @@ describe("media queries", () => {
}
})
).toEqual({ a: 1 });
expect(
process({
__mediaQueries: {
"@media android": [
{
inverse: false,
type: "android",
expressions: []
}
],
"@media not ios and (orientation: portrait)": [
{
inverse: true,
type: "ios",
expressions: [
{
feature: "orientation",
modifier: undefined,
value: "portrait"
}
]
}
]
},
a: 1,
"@media android": {
a: 2
},
"@media not ios and (orientation: portrait)": {
a: 4
}
})
).toEqual({ a: 4 });
expect(
process({
__mediaQueries: {
Expand Down
15 changes: 8 additions & 7 deletions src/mediaquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ function matchQuery(query, values) {
// 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;
}

if (query.expressions.length === 0) {
return typeMatch || inverse;
// 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) {
Expand Down Expand Up @@ -94,7 +92,10 @@ function matchQuery(query, values) {
}
});

return expressionsMatch || inverse;
if (inverse) {
return !(typeMatch && expressionsMatch);
}
return typeMatch && expressionsMatch;
}

// -- Utilities ----------------------------------------------------------------
Expand Down