fix: reading an unset style property returns "" per CSSOM, not undefined#328
Open
magicismight wants to merge 1 commit into
Open
fix: reading an unset style property returns "" per CSSOM, not undefined#328magicismight wants to merge 1 commit into
magicismight wants to merge 1 commit into
Conversation
Reading a property that is not set — via `el.style.someProp` or
`el.style.getPropertyValue("some-prop")` — returned undefined, but CSSOM
defines both as the empty string (the property getter is routed through the
same handler as getPropertyValue). Callers that assume the spec, e.g.
`el.style.color.replaceAll(...)`, crash on the undefined. Normalize an absent
property to "" in the style getter.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Reading a style property that isn't set returns
undefined, but per CSSOM both the camel-cased property getter andgetPropertyValue()must return the empty string for an unset property.Because the property getter is routed through the same handler as
getPropertyValue, both are affected. Code that assumes the spec — e.g.el.style.color.replaceAll(...)or any string method — throwsCannot read properties of undefinedon an element that simply hasn't set that property. This bites libraries that parse arbitrary HTML and read inline styles (ProseMirror's DOMParser, etc.).Fix
Normalize an absent property to
""in the style getter (esm/interface/css-style-declaration.js), which also coversgetPropertyValue. Setting/removing/serialization are untouched; a set property still returns its value, andcssTextis unchanged.CJS is regenerated via
npm run cjs. A test is added intest/interface/css-style-declaration.js; the full suite (node test/index.js) passes.Not a duplicate
Distinct from the open CSS issues:
<style>sheets (element.sheet), not the inlineelement.styledeclaration.el.style.property = ''ande.style.setProperty('property', '')should delete the property #327 concerns setting an empty string (delete semantics), not reading an unset property.