Skip to content

Commit

Permalink
feat: [#1147] Adds support for the aspect-ratio property
Browse files Browse the repository at this point in the history
  • Loading branch information
yinm committed Sep 12, 2024
1 parent afd256b commit 5967ab4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/happy-dom/src/css/declaration/CSSStyleDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4740,4 +4740,12 @@ export default class CSSStyleDeclaration extends AbstractCSSStyleDeclaration {
public set containerName(value: string) {
this.setProperty('container-name', value);
}

public get aspectRatio(): string {
return this.getPropertyValue('aspect-ratio');
}

public set aspectRatio(value: string) {
this.setProperty('aspect-ratio', value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,9 @@ export default class CSSStyleDeclarationPropertyManager {
case 'visibility':
properties = CSSStyleDeclarationPropertySetParser.getVisibility(value, important);
break;
case 'aspect-ratio':
properties = CSSStyleDeclarationPropertySetParser.getAspectRatio(value, important);
break;

default:
const trimmedValue = value.trim();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3256,4 +3256,38 @@ export default class CSSStyleDeclarationPropertySetParser {
}
return null;
}

/**
* Returns aspect ratio.
*
* @param value Value.
* @param important Important.
* @returns Property
*/
public static getAspectRatio(
value: string,
important: boolean
): {
[key: string]: ICSSStyleDeclarationPropertyValue;
} {
const variable = CSSStyleDeclarationValueParser.getVariable(value);
if (variable) {
return { 'aspect-ratio': { value: variable, important } };
}

const lowerValue = value.toLowerCase();
if (CSSStyleDeclarationValueParser.getGlobal(lowerValue)) {
return { 'aspect-ratio': { value: lowerValue, important } };
}

const parts = value.split('/');
if (parts.length === 1 && !Number.isNaN(Number(parts[0]))) {
return { 'aspect-ratio': { value, important } };
}
if (parts.length === 2 && !Number.isNaN(Number(parts[0])) && !Number.isNaN(Number(parts[1]))) {
return { 'aspect-ratio': { value, important } };
}

return null;
}
}

0 comments on commit 5967ab4

Please sign in to comment.