diff --git a/packages/happy-dom/src/css/declaration/CSSStyleDeclaration.ts b/packages/happy-dom/src/css/declaration/CSSStyleDeclaration.ts index a94e6ed84..4e6f8050e 100644 --- a/packages/happy-dom/src/css/declaration/CSSStyleDeclaration.ts +++ b/packages/happy-dom/src/css/declaration/CSSStyleDeclaration.ts @@ -4845,6 +4845,18 @@ export default class CSSStyleDeclaration { } } + /** + * Returns an iterator, allowing you to go through all property names contained in this object. + * + * @returns Iterator. + */ + public *[Symbol.iterator](): IterableIterator { + const propertyManager = this.#getPropertyManager(); + for (const key in propertyManager.properties) { + yield key; + } + } + /** * Returns item. * diff --git a/packages/happy-dom/test/css/declaration/CSSStyleDeclaration.test.ts b/packages/happy-dom/test/css/declaration/CSSStyleDeclaration.test.ts index ead481e94..68db2164d 100644 --- a/packages/happy-dom/test/css/declaration/CSSStyleDeclaration.test.ts +++ b/packages/happy-dom/test/css/declaration/CSSStyleDeclaration.test.ts @@ -84,6 +84,41 @@ describe('CSSStyleDeclaration', () => { }); }); + describe('[Symbol.iterator]()', () => { + it('Returns an iterator for property names.', () => { + const declaration = new CSSStyleDeclaration(PropertySymbol.illegalConstructor, window, { + element + }); + + element.setAttribute('style', 'color: red; font-size: 12px'); + + expect([...declaration]).toEqual(['color', 'font-size']); + }); + + it('Returns an empty iterator when no styles are set.', () => { + const declaration = new CSSStyleDeclaration(PropertySymbol.illegalConstructor, window, { + element + }); + + expect([...declaration]).toEqual([]); + }); + + it('Iterates expanded shorthand properties.', () => { + const declaration = new CSSStyleDeclaration(PropertySymbol.illegalConstructor, window, { + element + }); + + element.setAttribute('style', 'margin: 10px'); + + const keys = [...declaration]; + + expect(keys).toContain('margin-top'); + expect(keys).toContain('margin-right'); + expect(keys).toContain('margin-bottom'); + expect(keys).toContain('margin-left'); + }); + }); + describe('get border()', () => { it('Returns style property.', () => { const declaration = new CSSStyleDeclaration(PropertySymbol.illegalConstructor, window, {