Skip to content

fix(native): rank an unset specificity slot the same however it is spelled - #432

Merged
danstepanov merged 5 commits into
nativewind:mainfrom
YevheniiKotyrlo:fix/specificity-sparse-slots
Nov 23, 2026
Merged

danstepanov merged 5 commits into
nativewind:mainfrom
YevheniiKotyrlo:fix/specificity-sparse-slots

Conversation

@YevheniiKotyrlo

@YevheniiKotyrlo YevheniiKotyrlo commented Aug 16, 2026 •

Copy link
Copy Markdown
Contributor

Problem

specificityCompareFn answers 0 for two rules that are not equally specific once the sheet has reached a device.

A specificity array is sparse, and getNativeInjectionCode (src/metro/injection-code.ts) injects the sheet with JSON.stringify, which writes each hole as null:

.inp              ->  [1, 1]
.inp::placeholder ->  [2, 1, null, null, 1]

The comparator branched on the raw slot and returned a normalised difference:

if (aSpec[Important] !== bSpec[Important]) {
  return (aSpec[Important] || 0) - (bSpec[Important] || 0);

undefined !== null, so it answered 0 - 0 at a slot neither rule sets. The runtime sort in src/native/styles/index.ts then leaves the two rules in className order, so className="inp inp-ph" and "inp-ph inp" cascade differently. Any rule carrying a pseudo-element or an !important declaration can trigger it.

Solution

  • Every slot is ranked with spec[slot] || 0 before it is compared, so a hole, a null and a 0 are one unset component — Selectors 4 §15 counts an absent component as zero.
  • SpecificityValue admits null, because that is what a native runtime reads. The two compile-time merges in selector-builder.ts read a slot by its type instead of !== undefined.

Why it isn't caught today

registerCSS injects the compiler's own object, where both sides are holes, so every existing test compares undefined with undefined. The compile-time sort in src/compiler/stylesheet.ts runs the same comparator over that in-memory form, which is why only the runtime sort is exposed.

Tests

  • src/__tests__/native/specificity.test.tsx compiles .inp / .inp::placeholder, asserts the compile-time order, puts the rules through the same JSON.stringify round trip, asserts a null arrived, and sorts them with the runtime comparator from both input orders. It also pins each slot alone, against every slot beneath it, and an unset slot spelled as a hole, null or 0 on either side.
  • src/__tests__/vendor/tailwind/states.test.tsx does the same with Tailwind's own output for text-red-500 placeholder:text-blue-500 and text-red-500 selection:text-blue-500, and checks that the serialised rules of active:bg-gray-100 placeholder:text-gray-400 bg-white, and of its selection: twin, sort active:bg-gray-100 after bg-white.

On main, the 9 new cases that carry a null or a 0 into a deciding comparison fail and pass with the fix; the other 15 pass on both.

Verification

CI's jobs, run on the head merged into main 2e152c9: lint, test (1866 passed of 1866), build-library, build-web and commitlint pass on Ubuntu with Node 26.12.1, where .nvmrc pins 22.18.0, and build-ios-dev passes on macOS with Xcode 27.2, where ci.yml pins 26.6. Coverage of src/utilities/specificity.ts is 100% of lines and branches.

No existing issue tracks this — searched the tracker for specificity, specificityCompareFn, placeholder order, className order and null specificity.

@YevheniiKotyrlo

YevheniiKotyrlo commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor Author

No device frame

The description's assertion is the evidence: after the JSON round trip a device's sheet goes through, main's comparator answers 0 for .inp and .inp::placeholder, so their order follows the className order.

@sammoore sammoore left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved! Just want to add test here before merging though, as it will become relevant with some future PRs. Attached is the patch.

0001-test-tailwind-pin-the-cascade-on-both-sides-of-a-pse.patch

@YevheniiKotyrlo

Copy link
Copy Markdown
Contributor Author

Applied your patch as 06d19d0.

YevheniiKotyrlo and others added 5 commits November 23, 2026 11:45
…elled

A specificity array is sparse. A rule that sets `PseudoElements` never writes
`Important` or `Inline`, so those sit as holes inside the array's length —
`selector-builder.ts` merges with `if (value !== undefined)` and
`stylesheet.ts` skips an absent spec entirely, so nothing fills them in.

A hole reads as `undefined` in memory. The sheet reaches a native runtime
through `JSON.stringify` (`metro/injection-code.ts`), and JSON has no holes, so
every one arrives as `null`.

`specificityCompareFn` branched on the RAW slot while returning a NORMALISED
difference:

    if (aSpec[Important] !== bSpec[Important]) {
      return (aSpec[Important] || 0) - (bSpec[Important] || 0);

`undefined !== null` is true, so the comparison entered that branch and answered
`0 - 0`, settling at a slot neither rule uses and never reaching the one that
decides. Two rules that differ only in whether they carry a pseudo-element
compare equal.

The caller is the runtime sort in `native/styles/index.ts`, over rules gathered
across every class name on the element. A zero verdict leaves it nothing to
order by, so the `className` attribute's token order decides the cascade:

    className="inp inp-ph"   ->  one result
    className="inp-ph inp"   ->  the other

`placeholder:` and `selection:` are the everyday Tailwind triggers, and they are
the only two pseudo-elements this compiler emits.

Comparing the ranked value rather than the raw slot fixes it. The loop is part
of that: returning inside a raw-slot branch is what made a `0` difference
terminal instead of falling through to the next slot.

Nothing else reads these slots at runtime — every other `Specificity.` read is
compile time, where the array still has its holes and is already correct. That
is also why the existing suite is blind to this: the compile-time sort runs on
the in-memory form, and it masks the runtime bug whenever two rules share a
class name.

The test asserts at the comparator, over a sheet put through the JSON round trip
a device receives, rather than through a render. A rendered assertion would need
a non-`color` declaration to leak out of the pseudo-element rule, so it would go
inert the moment that leak is fixed; this one does not.
The sheet reaches a native runtime as JSON, which writes each hole in a
specificity array as `null`, so `SpecificityValue` admits it. The two
compile-time merges read a slot by its type rather than `!== undefined`,
and the test asserts the transported rule carries a `null` before it ranks.
The runtime sort must reproduce the order the compile-time sort gave the
same rules, from either input order, after the JSON transport. Each slot
is pinned on its own, against every slot beneath it, and with a hole, a
`null` and a zero on either side.
…utility

Tailwind v4's own output for `text-red-500 placeholder:text-blue-500` and
`text-red-500 selection:text-blue-500`, put through the JSON transport,
sorts the pseudo-element rule last from either input order.
…lity

The existing Tailwind case checks where a placeholder: or selection: rule
lands in the sort. Once nativewind#411 scopes those rules to placeholderTextColor and
selectionColor, no element utility writes the same prop, so that position
no longer shows in what renders.

What still does: on main a pseudo-element rule ranks equal to both of its
neighbours after the JSON transport, so in
`active:bg-gray-100 placeholder:text-gray-400 bg-white` the sort cannot
move bg-white below active:bg-gray-100, and the pressed background loses.
This fails on main and passes here, with nativewind#411 applied or not.
@danstepanov
danstepanov force-pushed the fix/specificity-sparse-slots branch from 06d19d0 to e55303f Compare November 23, 2026 19:45
@danstepanov
danstepanov merged commit 01b4a6f into nativewind:main Nov 23, 2026
6 checks passed
@sammoore

Copy link
Copy Markdown
Contributor

This is a breaking change from every pre-release of Nativewind v5, from react-native-css@3.0.x, and from react-native-css@3.1.0-rc.0 and rc.1.

Nativewind v4 is unaffected.

This notice is written for Tailwind users, but the same applies to hand-written CSS and other libraries (see the end).

In browsers, the order of classes in className never decides which class wins. Specificity and stylesheet order do, so md:bg-gray-100 beats bg-white however you write them. Earlier versions mostly followed this too, but not when a placeholder: or selection: class sat between the two. In that case the class written last won, even when it should have lost. They now follow the same rules as browsers, in any order.

Only this pattern was affected:

Written first Then Then
a variant class: active:, hover:, focus:, disabled:, md:, dark:, … placeholder:* or selection:* (without !) the base class it overrides

Any other order already worked, and so did a placeholder: or selection: class marked !important (placeholder:text-gray-400!).

Impact

The variant class now applies when its condition matches. Before, the base class written after it always won, so the variant never applied:

className When Before Now
active:bg-gray-100 placeholder:text-gray-400 bg-white pressed white gray-100
focus:bg-gray-100 selection:text-blue-200 bg-white focused white gray-100
md:bg-gray-100 placeholder:text-gray-400 bg-white md and wider white gray-100
dark:bg-gray-100 placeholder:text-gray-400 bg-white dark mode white gray-100

If a screen looked right only because the variant was being ignored, remove the variant class:

- <TextInput className="dark:bg-gray-800 placeholder:text-gray-400 bg-white" />
+ <TextInput className="placeholder:text-gray-400 bg-white" />

If you reordered classes or added ! to work around this, you can now undo it.

If you write your own ::placeholder or ::selection rules, the same applies: rules on either side of one in className now cascade by specificity and stylesheet order, as in browsers, instead of by the order you wrote the classes.

sammoore added a commit to sammoore/react-native-css that referenced this pull request Nov 27, 2026
BREAKING CHANGE: (nativewind#567, affects Tailwind) Variables registered with `inherits: false` no longer
  pass to children.

BREAKING CHANGE: (nativewind#578, affects Tailwind) Transforms (`translate-*`, `rotate-*`, `scale-*`,
  `skew-*`, `rotate-x/y-*`, `transform-*`) are now combined in the order browsers use.

BREAKING CHANGE: (nativewind#580, affects Tailwind) `py-*` and `px-*` now output `paddingVertical` and
  `paddingHorizontal`. On Android, a TextInput with these classes will lose additional padding it
  was wrongly keeping.

BREAKING CHANGE: (nativewind#432, affects Tailwind) `placeholder:` and `selection:` no longer depend on
  className order.

BREAKING CHANGE: (nativewind#457, affects Tailwind) Hex colours now always clip rather than gamut-map,
  matching browser behavior. In the default Tailwind color palette, 4 of 242 colors change by 1/255.

BREAKING CHANGE: (nativewind#585) `--x: initial` can now override another rule's value or the parent's
  value, and initial `!important` wins as in browsers. This means `via-none` and `*-initial`
  Tailwind utilities now work with variants, e.g. `md:`.

BREAKING CHANGE: (nativewind#575) Lists read through `var()` keep their commas.
  - `box-shadow: var(--a) var(--b)` is now one shadow.
  - Gradient stops and `rgb(var(--c) / a)` held in variables now output correctly.
  - `transform: var(--t)` gives a flat list.

BREAKING CHANGE: (nativewind#578) Style layering is now more accurate to browser behavior:
  - An `!important` value on a target other than style now sits on top of the class and inline
    values instead of replacing them.
  - For object-form mappings, an inline value that holds only CSS variables, or an inline `[]`
    or `{ height: undefined }`, no longer removes the class's style.

BREAKING CHANGE: (nativewind#578) `Pressable` callback styles are now combined with class styles instead of
  being dropped.

BREAKING CHANGE: (nativewind#568, nativewind#418) `styled()` and types changes:
  - A `mapping` that isn't an object now throws a named error.
  - A `nativeStyleMapping` entry set to undefined is skipped instead of crashing.
danstepanov pushed a commit to sammoore/react-native-css that referenced this pull request Nov 27, 2026
BREAKING CHANGE: (nativewind#567, affects Tailwind) Variables registered with `inherits: false` no longer
  pass to children.

BREAKING CHANGE: (nativewind#578, affects Tailwind) Transforms (`translate-*`, `rotate-*`, `scale-*`,
  `skew-*`, `rotate-x/y-*`, `transform-*`) are now combined in the order browsers use.

BREAKING CHANGE: (nativewind#580, affects Tailwind) `py-*` and `px-*` now output `paddingVertical` and
  `paddingHorizontal`. On Android, a TextInput with these classes will lose additional padding it
  was wrongly keeping.

BREAKING CHANGE: (nativewind#432, affects Tailwind) `placeholder:` and `selection:` no longer depend on
  className order.

BREAKING CHANGE: (nativewind#457, affects Tailwind) Hex colours now always clip rather than gamut-map,
  matching browser behavior. In the default Tailwind color palette, 4 of 242 colors change by 1/255.

BREAKING CHANGE: (nativewind#585) `--x: initial` can now override another rule's value or the parent's
  value, and initial `!important` wins as in browsers. This means `via-none` and `*-initial`
  Tailwind utilities now work with variants, e.g. `md:`.

BREAKING CHANGE: (nativewind#575) Lists read through `var()` keep their commas.
  - `box-shadow: var(--a) var(--b)` is now one shadow.
  - Gradient stops and `rgb(var(--c) / a)` held in variables now output correctly.
  - `transform: var(--t)` gives a flat list.

BREAKING CHANGE: (nativewind#578) Style layering is now more accurate to browser behavior:
  - An `!important` value on a target other than style now sits on top of the class and inline
    values instead of replacing them.
  - For object-form mappings, an inline value that holds only CSS variables, or an inline `[]`
    or `{ height: undefined }`, no longer removes the class's style.

BREAKING CHANGE: (nativewind#578) `Pressable` callback styles are now combined with class styles instead of
  being dropped.

BREAKING CHANGE: (nativewind#568, nativewind#418) `styled()` and types changes:
  - A `mapping` that isn't an object now throws a named error.
  - A `nativeStyleMapping` entry set to undefined is skipped instead of crashing.
danstepanov pushed a commit that referenced this pull request Nov 27, 2026
BREAKING CHANGE: (#567, affects Tailwind) Variables registered with `inherits: false` no longer
  pass to children.

BREAKING CHANGE: (#578, affects Tailwind) Transforms (`translate-*`, `rotate-*`, `scale-*`,
  `skew-*`, `rotate-x/y-*`, `transform-*`) are now combined in the order browsers use.

BREAKING CHANGE: (#580, affects Tailwind) `py-*` and `px-*` now output `paddingVertical` and
  `paddingHorizontal`. On Android, a TextInput with these classes will lose additional padding it
  was wrongly keeping.

BREAKING CHANGE: (#432, affects Tailwind) `placeholder:` and `selection:` no longer depend on
  className order.

BREAKING CHANGE: (#457, affects Tailwind) Hex colours now always clip rather than gamut-map,
  matching browser behavior. In the default Tailwind color palette, 4 of 242 colors change by 1/255.

BREAKING CHANGE: (#585) `--x: initial` can now override another rule's value or the parent's
  value, and initial `!important` wins as in browsers. This means `via-none` and `*-initial`
  Tailwind utilities now work with variants, e.g. `md:`.

BREAKING CHANGE: (#575) Lists read through `var()` keep their commas.
  - `box-shadow: var(--a) var(--b)` is now one shadow.
  - Gradient stops and `rgb(var(--c) / a)` held in variables now output correctly.
  - `transform: var(--t)` gives a flat list.

BREAKING CHANGE: (#578) Style layering is now more accurate to browser behavior:
  - An `!important` value on a target other than style now sits on top of the class and inline
    values instead of replacing them.
  - For object-form mappings, an inline value that holds only CSS variables, or an inline `[]`
    or `{ height: undefined }`, no longer removes the class's style.

BREAKING CHANGE: (#578) `Pressable` callback styles are now combined with class styles instead of
  being dropped.

BREAKING CHANGE: (#568, #418) `styled()` and types changes:
  - A `mapping` that isn't an object now throws a named error.
  - A `nativeStyleMapping` entry set to undefined is skipped instead of crashing.
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.

3 participants