Repository navigation
feat: support string styles through cssText and the style attribute - #110
Conversation
commit: |
| function attributeChanged(attr: Attr, value: string | null): void { | ||
| const element = attr.ownerElement; | ||
| if (element === null || attr.namespaceURI !== null) { | ||
| return; | ||
| } | ||
| switch (attr.localName) { | ||
| case 'style': | ||
| element.style.cssText = value ?? ''; | ||
| break; | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
We don't have them yet, but would this be a good case to use MutationObserver when we support them?
There was a problem hiding this comment.
i don't think so, MutationObserver delivers records async, so right after setAttribute('style', ...) reading el.style.color would still give you the old value until the observer runs. the spec keeps them in sync inside the attribute change steps, and MO records get queued from that same algorithm. so once we add MO, this is where we'd queue the record, alongside the switch rather than replacing it
| 'uppercase value', | ||
| 'overwriting with invalid value', | ||
| 'use rgb', | ||
| // Other cases use unsupported properties, shorthands, or computed styles. |
There was a problem hiding this comment.
What would it take to unlock these other test cases such as shorthands? Would it help to use a more formal CSS parser?
There was a problem hiding this comment.
looks like we'd need a proper inline style parser ;)
There was a problem hiding this comment.
i don't think the parser is the blocker here, those cases skip because we don't support the properties yet (font-size, font-weight, margin) or getComputedStyle. a real parser only matters once a value can contain ; or :, like strings or url(), and none of ours can today. for shorthands, padding is probably the one worth doing first
There was a problem hiding this comment.
As long as you see the path forward, sounds good!
| "Inline style declaration without whitespace between '!' and 'important'.", | ||
| "Style set dynamically via cssText without whitespace between '!' and 'important'.", | ||
| "Style set dynamically via cssText with whitespace between '!' and 'important'.", | ||
| // The remaining case uses the unsupported background-color property. |
There was a problem hiding this comment.
We should add a note to support the background color property.
There was a problem hiding this comment.
good call, tty ops already take a bg so it should mostly be wiring.
natemoo-re
left a comment
There was a problem hiding this comment.
agree with your responses here! onward!
What does this PR do?
preact, vue and solid set string styles with
style.cssText, svelte's custom renderer and plain dom code usesetAttribute('style', ...), and vue and solid clear styles by removing the attribute. none of that did anything before, so string styles were silently dropped.this adds
cssText(read and write) to the style declaration, plus a small attribute changed hook, like the spec's attribute change steps, that sendsstyleattribute changes to it. writingelement.stylechanges back into the attribute is left for a follow up.it's the first step toward svelte support, but useful on its own. also selects 8 upstream cssom subtests that pass now.
Type of change
Checklist
pnpm test)pnpm format)AI-generated code disclosure