Repository navigation
Description
Summary
BackgroundHandler can reorder prefixed background declarations across an unparsed value. This changes the cascade and can change the rendered gradient.
This defect exists on main independently of #46870. Track the handler change separately from that PR's gradient parser changes.
Requested by @robobun in #46870 (comment).
Related PR: #46870.
Reproduction
Reported on main at commit 179664ea89.
Save this as a.css:
.r{background:-webkit-linear-gradient(red,blue);background:-moz-linear-gradient(red,blue);background:-webkit-radial-gradient(center,circle cover,yellow,green)}Build with the default browser target:
bun build a.css --outdir=oAlso reproduce with --target=bun.
Actual behavior
Main emits the declarations in this order:
-moz-linear-gradient-webkit-radial-gradient-webkit-linear-gradient
The requester reports that Chrome 153 paints the radial gradient for the source and the linear gradient for the output.
Expected behavior
Preserve source order so that the last accepted background declaration remains the winner.
Reported cause and affected areas
In src/css/properties/background.rs, the Unparsed arm of BackgroundHandler::handle_property (reported near Line 785) calls flush, which writes only the current fields. The prefixed declarations held in self.decls are written later in finalize, after the unparsed value.
Update the handler so held prefixed declarations are emitted before the unparsed background value. Review the effect on fallback removal, rather than treating this as an isolated serialization change.
Regression coverage belongs in test/js/bun/css/css.test.ts.
Reported impact and direct-fix measurements
These measurements were supplied by @robobun and have not been independently reproduced for this issue:
- Among 2,140 real rule blocks from 407 npm packages, 13 blocks paint differently from their source at
--target=bunbecause of this defect. Affected packages include smallipop, stylus-skeuomorphic, mc-wallet, and intro.js. - A direct fix that writes held declarations before the unparsed value changes 16 of 400 whole real stylesheets.
- Across those whole stylesheets, default-target output grows by 0.043%. Prefixed fallbacks before an
-ms-gradient line are then retained. - None of the 15 stylesheets in
test/js/bun/css/fileschanges output.
These whole-stylesheet measurements use a different scope from the earlier rule-block size measurements in the linked thread. Evaluate the fallback-retention trade-off with maintainer input.
Acceptance criteria
- The reproduction above emits the background declarations in source order with both the default browser target and
--target=bun. - The source and built reproduction retain the same computed background in Chrome.
- The todo row from css: keep a prefixed gradient that uses the standard syntax as written #46870 for two typed prefixed values before
-webkit-radial-gradient(20px 30px, ..)passes as a normal test, with an exact source-order assertion. - Review and document changes to fallback retention for legacy gradient stacks, including stacks with an
-ms-gradient followed by a standard gradient.
Activity
robobun commentedon Oct 26, 2026
A fuzz run found a second fault in
BackgroundHandler. It has the same cause as this issue, and one change repairs both. It needs a decision from a maintainer before a PR.The fault
bun buildprints the-moz-line alone, at exit 0 (main 449877f, also with--minifyand with--target=bun).-moz-. The source gives them#2a6. The output gives them no background.background:#2a6; background-image:-moz-..becomesbackground: #2a6 -moz-.., with the same effect.The handler has one slot per longhand and a held list for prefixed values. A typed prefixed value overwrites the slot, which is this fault. The held list is written at the end of the rule, which is the order fault above.
Three shapes, measured
src/lab(),image-set()andconic-gradient()(18,278 sequences, default targets): wrong.text"Wrong" means: for some set of prefixes that a browser reads, Chrome computes other background longhands from the output than from the source. The prefixed function names are rewritten so that Chrome reads exactly that set. The second set also accepts an output that equals the source with some declarations lowered. The 836 blocks are the distinct rule blocks with a prefixed background value in 69 npm packages and in WebKit's own CSS.
B in three sentences. The handler writes the pending values and then the prefixed declaration, as it does today for a value that is kept as tokens. It records the range of the declaration list that it wrote. At the end of the rule it removes the recorded declarations that a later declaration replaces for every target, which is the rule that the handler has for a pending value.
What B does not do
background:#2a6; background:-webkit-linear-gradient(..)keeps both lines. A has the table and drops#2a6at the default targets.all(css: make theallshorthand reset the declarations before it #42042), the order of a legacy alias such as-webkit-background-size(css: resolve legacy aliases in the name table and keep them after the property they alias #45825). Example:.a{background:#eee} .a{all:unset;background:-moz-..}prints#eeeafterall.background: image-set(..); background-image: url(b.png)becomesbackground: url("b.png").width:10px; width:-moz-fit-contenthas the same fault inSizeHandler. That half comes after css: keep an unparsed width/height/color after the declarations written before it #42101.border-imageis css: keep border-image andborderin source cascade order within a rule #42018.Questions
--minifythen cannot reverse a kept prefixed gradient (css: read a prefixed linear-gradient direction in the 2011 syntax #46697). C prints 44% more than main on the 836 blocks.I recommend B. One PR for the answer replaces #46882. B prints the output of #46882 in 13 of its 16 rows. In 2 rows B also drops the prefixed values that a later standard value replaces. In 1 row B keeps
background-imageas a longhand.More numbers for B
color:red974 to 970,background:red644 to 631,background-color:red68 to 70. Per rule at the end: 317 to 311. No new allocation.#2a6; -webkit-..; standard: 6,658 to 8,522 instructions for the rule. Allocations per rule: between 5 fewer and 1 more.background: -webkit-linear-gradient(..) textstays one declaration (main writes the clip as two more declarations).background-image: url(a.png), -moz-..prints once (main prints it twice). The colours of a prefixed value are lowered to RGB in place, and no-webkit-gradient()copy is generated for it.test/js/bun/css/css.test.ts, 62 fail on main, no existing row is edited. One of them checks every rule of up to 3 declarations with no browser.css: consolidate the open CSS parser fixes (oven-sh#47025)