Skip to content

fix: resolve var() in style attributes of elements no rule matches - #706

Merged
cossssmin merged 4 commits into
masterfrom
fix/resolve-unmatched-inline-vars
Nov 9, 2026
Merged

cossssmin merged 4 commits into
masterfrom
fix/resolve-unmatched-inline-vars

Conversation

@cossssmin

@cossssmin cossssmin commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #701

var() in an element's style attribute was only resolved if a stylesheet rule also matched that element. Otherwise juice removed the <style> holding the variable definition and left var(--accent) in place.

Same with variables defined inline on an ancestor: an ancestor no rule matched never got its style attribute parsed, so the lookup skipped past it.

Fix

After all rules are handled, and only when resolveCSSVariables is on, run one more pass over elements whose style attribute contains --. It's an empty rule fed through handleRule, so it reuses the existing setup (parsing the style attribute, data-juice-* overrides) and skips elements that already have styleProps.

handleRule([`[${styleAttributeName}*="--"]`, []]);

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):

Scenario master this PR Δ
Issue repro (tiny) 0.036 ms 0.055 ms +50% (~20 µs)
Email, no vars (~1.2k els) 29.3 ms 29.4 ms +0.3%
Email, no inline styles (~1.2k els) 14.1 ms 14.4 ms +1.8%
Email, vars, all els matched by rules 29.3 ms 30.5 ms +4.2%
Email, vars, no els matched (bug case) 5.3 ms 14.9 ms +181%
Large, no vars (~10k els) 313 ms 314 ms +0.2%
Large, vars, no els matched (bug case) 159 ms 240 ms +51%

Basically noise for regular emails - the only extra cost is one attribute selector query. The big jumps are only in the bug cases, where master was 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 style attribute without a trailing ; resolved with the whitespace still attached:

<div style="--accent: red"><a style="color: var(--accent)">Link</a></div>

<!-- before -->
<a style="color: red ;">Link</a>

<!-- after -->
<a style="color: red;">Link</a>

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, so declToLegacy now trims the value. Regular properties are already trimmed by postcss, so no change there.

🤖 Generated with Claude Code

cossssmin and others added 4 commits November 9, 2026 18:33
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>
@cossssmin
cossssmin merged commit 0947347 into master Nov 9, 2026
3 checks passed
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.

resolveCSSVariables drops the variable definition but leaves var() unresolved in style attributes

1 participant