Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 15 additions & 1 deletion packages/core/css-mediaquery/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,25 @@ describe('css-mediaquery', () => {
expect(
matchQuery('not screen and (orientation: portrait)', {
type: MediaQueryType.screen,
orientation: 'landscape',
orientation: 'portrait',
}),
).toBe(false);
});

it('should return true when the features of a `not` query do not match', () => {
expect(
matchQuery('not screen and (orientation: portrait)', {
type: MediaQueryType.screen,
orientation: 'landscape',
}),
).toBe(true);
expect(matchQuery('not all and (min-width: 600)', { type: MediaQueryType.screen, width: 400 })).toBe(true);
});

it('should return true when the type of a `not` query does not match', () => {
expect(matchQuery('not print and (min-width: 100)', { type: MediaQueryType.screen, width: 800 })).toBe(true);
});

it('should not disrupt an OR query', () => {
expect(
matchQuery('not screen and (color), screen and (min-height: 48em)', {
Expand Down
8 changes: 4 additions & 4 deletions packages/core/css-mediaquery/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ export function matchQuery(mediaQuery: string, values: MediaQueryEnvironmentPara
// equal for a match.
const 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;
// `not` negates the whole query, the type included.
if (!typeMatch) {
return inverse;
}

const expressionsMatch = features.every((feature) => {
Expand Down Expand Up @@ -114,7 +114,7 @@ export function matchQuery(mediaQuery: string, values: MediaQueryEnvironmentPara
}
});

return (expressionsMatch && !inverse) || (!expressionsMatch && inverse);
return inverse ? !expressionsMatch : expressionsMatch;
});
}

Expand Down
Loading