Skip to content

CSS: preserve held prefixed background declarations before unparsed values #46872

Closed

Description

@coderabbitai

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=o

Also reproduce with --target=bun.

Actual behavior

Main emits the declarations in this order:

  1. -moz-linear-gradient
  2. -webkit-radial-gradient
  3. -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=bun because 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/files changes 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

  1. added
    bugSomething isn't working
    bundlerSomething to do with the bundler
    confirmed bugWe can reproduce this issue
    cssCSS parser, bundler-related
    on Oct 25, 2026
  2. robobun commented on Oct 26, 2026

    @robobun
    Collaborator

    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

    .btn{background:#2a6;background:-moz-linear-gradient(top,#3b7,#195)}
    • bun build prints the -moz- line alone, at exit 0 (main 449877f, also with --minify and with --target=bun).
    • Chrome and Safari do not read -moz-. The source gives them #2a6. The output gives them no background.
    • background:#2a6; background-image:-moz-.. becomes background: #2a6 -moz-.., with the same effect.
    • No user reported it. lightningcss-cli 1.30.2 prints what main prints. esbuild 0.25.0 keeps both lines.

    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

    main A: the handler keeps a list of states B: write at once, take back C: prefixed gradients stay tokens
    State built built emulated: main's binary, the functions renamed
    src/ +767 -192, 4 files +201 -37, 1 file a parser change, not written
    Every sequence of up to 3 from 20 background declarations (8,420): wrong at the default targets / with no targets 1,889 / 3,477 0 / 0 0 / 0 0 / 0
    A second set with lab(), image-set() and conic-gradient() (18,278 sequences, default targets): wrong 4,090 940 586
    Of those, wrong where main is right 65 0
    836 real rule blocks: Chrome 153 computes another background (default targets / none) 2 / 38 0 / 0 0 / 0 0 / 0
    836 real rule blocks at the default targets: bytes, declarations 310,954, 5,648 282,461, 5,366 269,511, 5,211 448,943, 7,052
    Release .text +12,032 B +3,328 B

    "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

    Questions

    1. Does bun keep a fallback of the author that lightningcss drops?
    2. If yes: B or C? C needs no change in the handler, and --minify then 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-image as a longhand.

    More numbers for B
    • Whole files. 102 of 4,288 real stylesheets change at the default targets: 69 shrink, 1 grows by 66 B, net -39,745 B. With no targets 156 change, net -18,352 B. No whole rule appears or disappears.
    • A rule with no prefixed image. Instructions per property: color:red 974 to 970, background:red 644 to 631, background-color:red 68 to 70. Per rule at the end: 317 to 311. No new allocation.
    • A rule with a prefixed stack. #2a6; -webkit-..; standard: 6,658 to 8,522 instructions for the rule. Allocations per rule: between 5 fewer and 1 more.
    • Other output changes. A lone background: -webkit-linear-gradient(..) text stays 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.
    • Targets that need a generated prefix (not reachable from the CLI). After a prefixed value of the author, B generates no prefix. N equal rules that merge keep N copies, main keeps one.
    • Tests on the branch. 100 new rows in 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.
    • Not done. No Firefox run: which browser reads which prefix comes from MDN data. B has not had a full review yet.
  3. added a commit that references this issue on Oct 27, 2026
    7942008
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingbundlerSomething to do with the bundlerconfirmed bugWe can reproduce this issuecssCSS parser, bundler-related

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions