Repository navigation
fix: resolve var() in style attributes of elements no rule matches - #706
Merged
Merged
Conversation
Regression test for #701: a variable used in an element's style attribute isn't resolved when no stylesheet rule matches the element. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Only elements matched by a stylesheet rule got their style attribute parsed, so var() there stayed unresolved while the <style> block that defined the variable was removed. Run one more pass over elements whose style attribute uses or defines a custom property. Fixes #701 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
postcss's strict parser, used for style attributes, keeps trailing whitespace in custom property values, so a var() resolving to one ended up as `color: red ;`. Custom property values are trimmed per spec. Co-Authored-By: Claude Opus 5.5 <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.
Fixes #701
var()in an element'sstyleattribute was only resolved if a stylesheet rule also matched that element. Otherwise juice removed the<style>holding the variable definition and leftvar(--accent)in place.Same with variables defined inline on an ancestor: an ancestor no rule matched never got its
styleattribute parsed, so the lookup skipped past it.Fix
After all rules are handled, and only when
resolveCSSVariablesis on, run one more pass over elements whose style attribute contains--. It's an empty rule fed throughhandleRule, so it reuses the existing setup (parsing the style attribute,data-juice-*overrides) and skips elements that already havestyleProps.Note that elements whose inline style only defines custom properties (e.g.
<div style="--accent: red">) are left as they are.Perf
Compared against
master(median of interleaved rounds, Node 24):Basically noise for regular emails - the only extra cost is one attribute selector query. The big jumps are only in the bug cases, where
masterwas faster because it skipped those elements entirely.Trailing whitespace in custom property values
Also fixed here, since this PR makes it show up a lot more: a custom property defined last in a
styleattribute without a trailing;resolved with the whitespace still attached:Style attributes are parsed with postcss's strict parser, which keeps whitespace in custom property values (the safe parser used for
<style>tags already trims it). Custom property values are trimmed per spec, sodeclToLegacynow trims the value. Regular properties are already trimmed by postcss, so no change there.🤖 Generated with Claude Code