Skip to content

Commit

Permalink
#921@trivial: Adds support for "width" and "height" rules to media qu…
Browse files Browse the repository at this point in the history
…eries.
  • Loading branch information
capricorn86 committed May 18, 2023
1 parent 0e50f0f commit 1c8e533
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
29 changes: 23 additions & 6 deletions packages/happy-dom/src/match-media/MediaQueryItem.ts
Expand Up @@ -205,6 +205,8 @@ export default class MediaQueryItem {
case 'max-width':
case 'min-height':
case 'max-height':
case 'width':
case 'height':
case 'orientation':
case 'prefers-color-scheme':
case 'hover':
Expand Down Expand Up @@ -233,6 +235,12 @@ export default class MediaQueryItem {
case 'max-height':
const maxHeight = this.toPixels(rule.value);
return maxHeight !== null && this.ownerWindow.innerHeight <= maxHeight;
case 'width':
const width = this.toPixels(rule.value);
return width !== null && this.ownerWindow.innerWidth === width;
case 'height':
const height = this.toPixels(rule.value);
return height !== null && this.ownerWindow.innerHeight === height;
case 'orientation':
return rule.value === 'landscape'
? this.ownerWindow.innerWidth > this.ownerWindow.innerHeight
Expand Down Expand Up @@ -269,20 +277,29 @@ export default class MediaQueryItem {
case 'max-aspect-ratio':
case 'aspect-ratio':
const aspectRatio = rule.value.split('/');
const width = parseInt(aspectRatio[0], 10);
const height = parseInt(aspectRatio[1], 10);
const aspectRatioWidth = parseInt(aspectRatio[0], 10);
const aspectRatioHeight = parseInt(aspectRatio[1], 10);

if (isNaN(width) || isNaN(height)) {
if (isNaN(aspectRatioWidth) || isNaN(aspectRatioHeight)) {
return false;
}

switch (rule.name) {
case 'min-aspect-ratio':
return width / height <= this.ownerWindow.innerWidth / this.ownerWindow.innerHeight;
return (
aspectRatioWidth / aspectRatioHeight <=
this.ownerWindow.innerWidth / this.ownerWindow.innerHeight
);
case 'max-aspect-ratio':
return width / height >= this.ownerWindow.innerWidth / this.ownerWindow.innerHeight;
return (
aspectRatioWidth / aspectRatioHeight >=
this.ownerWindow.innerWidth / this.ownerWindow.innerHeight
);
case 'aspect-ratio':
return width / height === this.ownerWindow.innerWidth / this.ownerWindow.innerHeight;
return (
aspectRatioWidth / aspectRatioHeight ===
this.ownerWindow.innerWidth / this.ownerWindow.innerHeight
);
}
}

Expand Down
20 changes: 20 additions & 0 deletions packages/happy-dom/test/match-media/MediaQueryList.test.ts
Expand Up @@ -215,6 +215,26 @@ describe('MediaQueryList', () => {
).toBe(true);
});

it('Handles "width".', () => {
expect(new MediaQueryList({ ownerWindow: window, media: '(width)' }).matches).toBe(true);
expect(new MediaQueryList({ ownerWindow: window, media: '(width: 1023px)' }).matches).toBe(
false
);
expect(new MediaQueryList({ ownerWindow: window, media: '(width: 1024px)' }).matches).toBe(
true
);
});

it('Handles "height".', () => {
expect(new MediaQueryList({ ownerWindow: window, media: '(height)' }).matches).toBe(true);
expect(new MediaQueryList({ ownerWindow: window, media: '(height: 767px)' }).matches).toBe(
false
);
expect(new MediaQueryList({ ownerWindow: window, media: '(height: 768px)' }).matches).toBe(
true
);
});

it('Handles "orientation".', () => {
expect(new MediaQueryList({ ownerWindow: window, media: '(orientation)' }).matches).toBe(
true
Expand Down

0 comments on commit 1c8e533

Please sign in to comment.