Skip to content

fix: [#1855] Add Symbol.iterator to CSSStyleDeclaration for iteration support#2009

Open
TrevorBurnham wants to merge 1 commit intocapricorn86:masterfrom
TrevorBurnham:fix/1855-cssstyledeclaration-iterator
Open

fix: [#1855] Add Symbol.iterator to CSSStyleDeclaration for iteration support#2009
TrevorBurnham wants to merge 1 commit intocapricorn86:masterfrom
TrevorBurnham:fix/1855-cssstyledeclaration-iterator

Conversation

@TrevorBurnham
Copy link
Copy Markdown
Contributor

Fixes #1855

The CSSStyleDeclaration class was missing the Symbol.iterator implementation, which prevented users from iterating over style properties using for...of loops, the spread operator, or Array.from().

Changes

  • Added [Symbol.iterator]() generator method to CSSStyleDeclaration class
  • Added tests for iteration functionality

Before

const myElement = document.createElement('div');
myElement.setAttribute('style', 'background:red');

for (const key of myElement.style) {
  console.log(key);
}
// TypeError: myElement.style is not iterable

After

const myElement = document.createElement('div');
myElement.setAttribute('style', 'background:red');

for (const key of myElement.style) {
  console.log(key);
}
// Outputs: background-image, background-position-x, background-position-y, etc.

// Also works with spread operator
console.log([...myElement.style]);

// And Array.from()
console.log(Array.from(myElement.style));

@TrevorBurnham TrevorBurnham force-pushed the fix/1855-cssstyledeclaration-iterator branch from 761c6ad to 9fe2f43 Compare January 20, 2026 01:21
Copy link
Copy Markdown
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.

Thank you for your contribution @TrevorBurnham! ⭐

I believe that the implementation can be done in a more efficient way (see comment).

Comment on lines +4855 to +4858
const length = propertyManager.size();
for (let i = 0; i < length; i++) {
yield propertyManager.item(i);
}
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Suggested change
const length = propertyManager.size();
for (let i = 0; i < length; i++) {
yield propertyManager.item(i);
}
return Object.keys(propertyManager.properties)[Symbol.iterator]]();

I believe that this is more performant.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch! I've updated this PR to use a more performant approach for this iteration.

@TrevorBurnham TrevorBurnham force-pushed the fix/1855-cssstyledeclaration-iterator branch from 9fe2f43 to a6d6076 Compare February 9, 2026 03:19
@TrevorBurnham
Copy link
Copy Markdown
Contributor Author

CI failure appears to be due to #2022, which has a patch pending: #2023.

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.

Unable to iterate over CSSStyleDeclaration objects

2 participants