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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -77,21 +77,17 @@ export default abstract class AbstractCSSStyleDeclaration {

if (this._ownerElement) {
const style = new CSSStyleDeclarationPropertyManager({ cssText });
if (!style.size()) {
delete this._ownerElement['_attributes']['style'];
} else {
if (!this._ownerElement['_attributes']['style']) {
Attr._ownerDocument = this._ownerElement.ownerDocument;
this._ownerElement['_attributes']['style'] = new Attr();
this._ownerElement['_attributes']['style'].name = 'style';
}

if (this._ownerElement.isConnected) {
this._ownerElement.ownerDocument['_cacheID']++;
}
if (!this._ownerElement['_attributes']['style']) {
Attr._ownerDocument = this._ownerElement.ownerDocument;
this._ownerElement['_attributes']['style'] = new Attr();
this._ownerElement['_attributes']['style'].name = 'style';
}

this._ownerElement['_attributes']['style'].value = style.toString();
if (this._ownerElement.isConnected) {
this._ownerElement.ownerDocument['_cacheID']++;
}

this._ownerElement['_attributes']['style'].value = style.toString();
} else {
this._style = new CSSStyleDeclarationPropertyManager({ cssText });
}
Expand Down
10 changes: 10 additions & 0 deletions packages/happy-dom/src/nodes/html-element/HTMLElement.ts
Expand Up @@ -200,6 +200,16 @@ export default class HTMLElement extends Element implements IHTMLElement {
return this._style;
}

/**
* Sets style.
*
* @param cssText Style as text.
* @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style#setting_styles
*/
public set style(cssText: string | CSSStyleDeclaration) {
this.style.cssText = typeof cssText === 'string' ? <string>cssText : '';
}

/**
* Returns data set.
*
Expand Down
4 changes: 3 additions & 1 deletion packages/happy-dom/src/nodes/html-element/IHTMLElement.ts
Expand Up @@ -9,7 +9,6 @@ import IElement from '../element/IElement';
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement.
*/
export default interface IHTMLElement extends IElement {
style: CSSStyleDeclaration;
dataset: { [key: string]: string };
tabIndex: number;
offsetHeight: number;
Expand Down Expand Up @@ -48,6 +47,9 @@ export default interface IHTMLElement extends IElement {
ontransitionrun: (event: Event) => void | null;
ontransitionstart: (event: Event) => void | null;

get style(): CSSStyleDeclaration;
set style(cssText: CSSStyleDeclaration | string);

/**
* Triggers a click event.
*/
Expand Down
Expand Up @@ -1978,7 +1978,7 @@ describe('CSSStyleDeclaration', () => {

declaration.cssText = '';

expect(element.getAttribute('style')).toBe(null);
expect(element.getAttribute('style')).toBe('');
});
});

Expand Down
28 changes: 28 additions & 0 deletions packages/happy-dom/test/nodes/html-element/HTMLElement.test.ts
Expand Up @@ -239,6 +239,34 @@ describe('HTMLElement', () => {
});
});

describe('set style()', () => {
it('Sets the value of the style.cssText property.', () => {
element.style = 'border-radius: 2px; padding: 2px;';

expect(element.style.cssText).toEqual('border-radius: 2px; padding: 2px;');
expect(element.style.borderRadius).toEqual('2px');
expect(element.style.padding).toEqual('2px');
expect(element.getAttribute('style')).toEqual('border-radius: 2px; padding: 2px;');
expect(element.outerHTML).toEqual('<div style="border-radius: 2px; padding: 2px;"></div>');

element.style = '';

expect(element.style.cssText).toEqual('');
expect(element.style.borderRadius).toEqual('');
expect(element.style.padding).toEqual('');
expect(element.getAttribute('style')).toEqual('');
expect(element.outerHTML).toEqual('<div style=""></div>');

element.style = null;

expect(element.style.cssText).toEqual('');
expect(element.style.borderRadius).toEqual('');
expect(element.style.padding).toEqual('');
expect(element.getAttribute('style')).toEqual('');
expect(element.outerHTML).toEqual('<div style=""></div>');
});
});

describe('get dataset()', () => {
it('Returns a Proxy behaving like an object that can add, remove, set and get element attributes prefixed with "data-".', () => {
element.setAttribute('test-alpha', 'value1');
Expand Down