Skip to content

Commit

Permalink
Support media query or
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyalIcing committed Nov 21, 2023
1 parent 6f29a20 commit 08cac11
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions src/media-query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,15 @@ type ParsedMediaInParens = ParsedMediaFeature;
class ParsedMediaCondition {
constructor(
public readonly first: ParsedMediaFeature,
public readonly conditions?: ParsedMediaAnds
public readonly conditions?: ParsedMediaAnds | ParsedMediaOrs
) {}

matches(context: MatchMediaContext) {
const base = this.first.matches(context);
if (this.conditions) {
if (this.conditions instanceof ParsedMediaAnds) {
return base && this.conditions.matches(context);
} else if (this.conditions instanceof ParsedMediaOrs) {
return base || this.conditions.matches(context);
} else {
return base;
}
Expand All @@ -227,7 +229,11 @@ class ParsedMediaCondition {
static *Parser() {
yield optionalWhitespace;
const first: ParsedMediaInParens = yield parsedMediaInParens;
const conditions: ParsedMediaAnds | '' = yield [ParsedMediaAnds.Parser, ''];
const conditions: ParsedMediaAnds | ParsedMediaOrs | '' = yield [
ParsedMediaAnds.Parser,
ParsedMediaOrs.Parser,
'',
];
if (conditions === '') {
return first;
} else {
Expand Down Expand Up @@ -257,6 +263,27 @@ class ParsedMediaAnds {
}
}

class ParsedMediaOrs {
constructor(public readonly list: ReadonlyArray<ParsedMediaInParens>) {}

matches(context: MatchMediaContext) {
return this.list.some((m) => m.matches(context));
}

static *Parser() {
const list: Array<ParsedMediaInParens> = [];

do {
yield requiredWhitespace;
yield 'or';
yield requiredWhitespace;
list.push(yield parsedMediaInParens);
} while (yield hasMore);

return new ParsedMediaOrs(list);
}
}

class ParsedMediaTypeThenConditionWithoutOr {
constructor(
public readonly mediaType: ParsedMediaType | ParsedNotMediaType,
Expand Down Expand Up @@ -624,4 +651,11 @@ test('matchMedia()', () => {
'not print and (min-width: 480px) and (orientation: landscape) and (any-hover: hover)'
).matches
).toBe(true);

expect(
matchMedia(
screenSized(480, 100),
'(orientation: landscape) or (orientation: portrait)'
).matches
).toBe(true);
});

0 comments on commit 08cac11

Please sign in to comment.