Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#628@patch: Handle style setter in HTMLElement. #629

Merged
merged 2 commits into from Oct 25, 2022

Conversation

jledentu
Copy link
Contributor

This PR adds a setter on HTMLElement.style the reproduce the behaviour of DOM implementations, as explained in #628.

This also fixes another difference between happy-dom and browser implementations: setting null or '' as style (or in cssText) shouldn't remove the style attribute.

element.style.cssText = null;
console.log(element.getAttribute('style')); // displays '', not null

Since TypeScript forces a value returned by a getter to be assignable to a setter (see microsoft/TypeScript#42425), the style setter accepts a CSSStyleDeclaration but then resets the cssText, as implemented in some browsers.

Closes #628

Copy link
Owner

@capricorn86 capricorn86 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice contribution! 🌟

I just found a minor thing.

* @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style#setting_styles
*/
public set style(cssText: string | CSSStyleDeclaration) {
this.setAttribute('style', typeof cssText === 'string' ? <string>cssText : '');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.setAttribute('style', typeof cssText === 'string' ? <string>cssText : '');
this.style.cssText = cssText;

We have to parse the CSS string before setting it (e.g. element.style = 'invalid: invalid' won't work).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I updated

@capricorn86 capricorn86 merged commit 59a8ed7 into capricorn86:master Oct 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HTMLElement.style cannot be set directly
2 participants