Repository: capricorn86/happy-dom
Pull request #934 (2023-05-23 19:33:09)

#932@patch: Fixes issue where CSS variables where not possible to be …
…retrieved from an instance of CSSStyleDeclaration retrieved from Window.getComputedStyle() when calling CSSStyleDeclaration.getPropertyValue().


Patch:
diff --git a/packages/happy-dom/src/css/declaration/element-style/CSSStyleDeclarationElementStyle.ts b/packages/happy-dom/src/css/declaration/element-style/CSSStyleDeclarationElementStyle.ts
index e4ddb20f..d64276d1 100644
--- a/packages/happy-dom/src/css/declaration/element-style/CSSStyleDeclarationElementStyle.ts
+++ b/packages/happy-dom/src/css/declaration/element-style/CSSStyleDeclarationElementStyle.ts
@@ -187,22 +187,19 @@ export default class CSSStyleDeclarationElementStyle {
 			}
 
 			CSSStyleDeclarationCSSParser.parse(elementCSSText, (name, value, important) => {
-				if (name.startsWith('--')) {
-					const cssValue = this.parseCSSVariablesInValue(value, cssVariables);
-					if (cssValue) {
-						cssVariables[name] = cssValue;
-					}
-					return;
-				}
-
+				const isCSSVariable = name.startsWith('--');
 				if (
+					isCSSVariable ||
 					CSSStyleDeclarationElementInheritedProperties[name] ||
 					parentElement === targetElement
 				) {
 					const cssValue = this.parseCSSVariablesInValue(value, cssVariables);
 					if (cssValue && (!propertyManager.get(name)?.important || important)) {
 						propertyManager.set(name, cssValue, important);
-						if (name === 'font' || name === 'font-size') {
+
+						if (isCSSVariable) {
+							cssVariables[name] = cssValue;
+						} else if (name === 'font' || name === 'font-size') {
 							const fontSize = propertyManager.properties['font-size'];
 							if (fontSize !== null) {
 								const parsedValue = this.parseMeasurementsInValue({


Test patch:
diff --git a/packages/happy-dom/test/css/declaration/CSSStyleDeclaration.test.ts b/packages/happy-dom/test/css/declaration/CSSStyleDeclaration.test.ts
index 56b2454d..7659922e 100644
--- a/packages/happy-dom/test/css/declaration/CSSStyleDeclaration.test.ts
+++ b/packages/happy-dom/test/css/declaration/CSSStyleDeclaration.test.ts
@@ -2,17 +2,17 @@ import CSSStyleDeclaration from '../../../src/css/declaration/CSSStyleDeclaratio
 import Window from '../../../src/window/Window';
 import IWindow from '../../../src/window/IWindow';
 import IDocument from '../../../src/nodes/document/IDocument';
-import IElement from '../../../src/nodes/element/IElement';
+import IHTMLElement from '../../../src/nodes/html-element/IHTMLElement';
 
 describe('CSSStyleDeclaration', () => {
 	let window: IWindow;
 	let document: IDocument;
-	let element: IElement;
+	let element: IHTMLElement;
 
 	beforeEach(() => {
 		window = new Window();
 		document = window.document;
-		element = document.createElement('div');
+		element = <IHTMLElement>document.createElement('div');
 	});
 
 	describe(`get {number}()`, () => {
@@ -2721,6 +2721,23 @@ describe('CSSStyleDeclaration', () => {
 			expect(element.getAttribute('style')).toBe('border: 2px solid green; --test-key: value;');
 			expect(declaration.getPropertyValue('--test-key')).toBe('value');
 		});
+
+		it('Can set a CSS variable.', () => {
+			const declaration = new CSSStyleDeclaration(element);
+
+			element.setAttribute('style', `border: 2px solid green;`);
+
+			declaration.setProperty('--test-key', 'value');
+
+			expect(element.getAttribute('style')).toBe('border: 2px solid green; --test-key: value;');
+			expect(declaration.getPropertyValue('--test-key')).toBe('value');
+		});
+
+		it('Can set a CSS variable as element style property.', () => {
+			element.style.setProperty('--test-key', 'value');
+			document.body.appendChild(element);
+			expect(new CSSStyleDeclaration(element, true).getPropertyValue('--test-key')).toBe('value');
+		});
 	});
 
 	describe('removeProperty()', () => {