Skip to content

fix(console): %c colors with same red component as previous color - #34784

Merged
bartlomieju merged 2 commits into
mainfrom
fix/console-css-hex-color-equals
Jun 5, 2026
Merged

bartlomieju merged 2 commits into
mainfrom
fix/console-css-hex-color-equals

Conversation

@bartlomieju

Copy link
Copy Markdown
Member

When formatting console.log %c directives, the background-color and
color of a span are compared against the previous span's to decide whether
a new ANSI escape needs to be emitted. That comparison used colorEquals,
which indexes its arguments as [r, g, b] arrays, but background-color
and color are stored as raw strings (a named color, or a hex/rgb/hsl
string). Comparing two hex strings by index instead compares their leading
characters, so any two colors sharing the same red component (e.g.
#FFFFFF and #FFAFC8, both starting #FF) were treated as equal and the
new color was never emitted, leaving the previous background to bleed into
the next span.

This normalizes both operands to [r, g, b] arrays before comparing, so
colors are compared by value regardless of how they were written.

Fixes #21605

The previous comparison normalized any color string to an [r,g,b] array
before comparing. An unparseable string like "inherit" or "foo" parses
to null, which then compared equal to an absent (null) color, suppressing
the reset escape (\x1b[49m / \x1b[39m) that cssToAnsi emits for those.

Restrict the parse-and-compare-by-value path to the case both operands
are strings, which is exactly the #21605 hex-prefix collision. Mixed
cases (string vs null or array) keep the original colorEquals behavior.
@bartlomieju
bartlomieju enabled auto-merge (squash) June 5, 2026 14:30
@bartlomieju
bartlomieju merged commit 7b3ccff into main Jun 5, 2026
136 checks passed
@bartlomieju
bartlomieju deleted the fix/console-css-hex-color-equals branch June 5, 2026 15:47
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.

console.log %c with background-color #FFFFFF overwrites the next background color

1 participant