Skip to content

fix/css-processor-inline-css-validation - #61

Open
5ZYSZ3K wants to merge 5 commits into
mainfrom
fix/css-processor-inline-css-validation
Open

5ZYSZ3K wants to merge 5 commits into
mainfrom
fix/css-processor-inline-css-validation

Conversation

@5ZYSZ3K

@5ZYSZ3K 5ZYSZ3K commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

Fixes #59

Summary

This PR fixes the three bugs from the issue, in one commit each. A fourth commit makes inline CSS ignore !important. Below: how the validators and the inline parser changed, and which behavior differs from the issue's expected output or from main beyond it.

Validators

The diff below leaves out intermediate base classes.

 GenericCSSPropertyValidator
+  lowercaseInlineCSSKeywords(value)          # by default returns value: no second attempt
   LongSizeCSSPropertyValidator
+    allowsNegativeValues() → false
+    LongSignedSizeCSSPropertyValidator       # → true: margin*, top, right, bottom, left, letterSpacing
   LongColorCSSPropertyValidator
-    COLOR_REGEX
+    hex | rgb/rgba/hsl/hsla | NAMED_COLORS     # css-color-keywords + transparent, cyan, magenta
   LongEnumerationListCSSPropertyValidator
+    matches each item lowercased
   ShortCSSToReactNativeValidator
+    returns null when css-to-react-native can't expand the value
     ShortFontCSSValidator
+      lowercases only up to the font size

lowercaseInlineCSSKeywords is overridden by validators of sizes, colors, enumerations and css-to-react-native shorthands (border, background, flex-flow, text-decoration, font). They call the new lowercaseCSSKeywords helper, which leaves hex colors as written.

Inline parsing

 CSSInlineParseRun
   parseInlineCSS
+    strip a trailing !important (any case, any whitespace)
   normalizeProp(rawName, rawValue)
-    getPropertyName(rawName)
+    resolvePropertyName(rawName)
+      getPropertyName(rawName), if registered
+      else getPropertyName(rawName.toLowerCase())
     validator.normalizeInlineCSSValue(rawValue)
+    if null: validator.normalizeInlineCSSValue(
+               validator.lowercaseInlineCSSKeywords(rawValue))

Design decisions

  • Try as written, lowercase only on failure. Anything valid on main still gives the same output. camelCase names such as paddingBottom still resolve, and a test in render relies on that. #FFF keeps its case.
  • Each validator opts in to lowercasing. It is only safe where the value is a keyword:
    • font-family is left out because fonts are matched by exact name.
    • list-style-type is left out because it may name a user-defined customListStyleSpecs entry.
    • transform is left out because css-to-react-native expects camelCase function names such as translateX.
    • In the font shorthand, only the part up to the font size is lowercased, so the family keeps its case.
  • Enumeration lists lowercase each item instead. fontVariant keeps the valid items and drops the rest. In small-caps OLDSTYLE-NUMS, small-caps is already valid as written, so the second attempt would never run and the second item would be lost.
  • css-to-react-native shorthands return null on failure. Before, a failed expansion was wrapped in a merge request that merged nothing. Returning null lets normalizeProp see the failure and try again lowercased.
  • The validator decides whether a negative value is allowed. The length regex now accepts a sign, and splitValueAndUnit rejects negative values unless allowsNegativeValues() returns true.
  • Colors are checked against a closed list, not \w+. css-color-keywords was already installed through css-to-react-native. It is now a direct dependency, with a local type declaration.

Behavior changes

Input main This branch Notes
color: red !important { color: 'red !important' } { color: 'red' } The issue expects {}. Instead the flag is stripped from every declaration, so margin-top: 5px !important now applies too.
display: FLEX, border: 1PX SOLID RED {} lowercased and applied The issue only covers property names. Keyword values are case-insensitive in CSS too.
color: currentColor, color: inherit passed through {} Not in the color list. iOS and Android already rejected them. On web, border-color: currentColor now falls back to black.
margin-top: px { marginTop: 0 } {} A length needs at least one digit. +5px, .5px and -0 are accepted.
tagsStyles: { marginTop: '-5px' } {} { marginTop: -5 } Strings in native styles go through the same length regex. 'px' is now dropped there as well.

Unchanged on purpose:

  • Native style values (tagsStyles, classesStyles, idsStyles):
    • They are not lowercased, because React Native is case-sensitive: { display: 'FLEX' } fails there.
    • Numbers are not checked for sign: { paddingTop: -5 } is kept.
    • Colors are not validated, as on main.
  • !important within one style attribute: a later declaration still overrides an earlier important one. color: red !important; color: blue gives blue, where a browser shows red. The docs now say that the flag is accepted and ignored.

Evidence

  • css-processor passes yarn test (1847 jest tests, tsc and lint) on each of the four commits. New tests cover each row of the table above, and the cases left unchanged on purpose.
  • On the last commit, transient-render-engine (191 tests) and render (265 tests) pass, and the type check and lint pass for doc-pages.

Merge Danger

Door: Two-way

The changes stay inside css-processor validation code. Validators aren't exported, so no public API changes. Reverting the commits restores the old output.

Blast Radius: Styling

  • Inline declarations that main dropped now apply: negative margins and offsets, uppercase names and keywords, and !important values. Existing content can shift.
  • Values that main passed through are now dropped: invalid colors, currentColor and inherit.

5ZYSZ3K and others added 4 commits November 17, 2026 23:52
…them

Negative lengths such as `margin-top: -5px`, `top: -10%` or
`letter-spacing: -0.5px` were discarded because the length regex had no
sign, which also dropped whole shorthands like `margin: -10px 0`.

The length regex now accepts an optional sign and requires at least one
digit, so `px` and `.px` are no longer parsed as `0` and `NaN`. Size
validators reject negative values by default, while the new
LongSignedSizeCSSPropertyValidator allows them for margins, offsets
(top, right, bottom, left) and letter-spacing. `-0` is normalized to `0`.

Refs #59

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The color regex alternation was not grouped, so its first branch was
only anchored at the start and its second branch only at the end.
Invalid values such as `red !important`, `not a color` or
`url(x.png) blue` passed validation and reached React Native styles.

Colors are now validated as a whole against:
- hex colors with 3, 4, 6 or 8 digits;
- rgb(), rgba(), hsl() and hsla() functions, with the same grammar as
  before;
- named colors from css-color-keywords (already a transitive dependency
  via css-to-react-native), plus `transparent`, `cyan` and `magenta`
  which React Native supports.

Other keywords are discarded, including `inherit` and `currentColor`,
which iOS and Android reject. `color` is already inherited by the render
engine, so discarding `inherit` gives the expected result.

Refs #59

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The `!important` flag was kept as part of the value, so declarations
like `margin-top: 5px !important` or `display: flex !important` were
discarded, while forgiving properties such as `font-family` kept the
flag in their value.

The flag is now stripped from every inline declaration, regardless of
case and whitespace, and ignored. Between style sources, this makes no
difference: inline styles are merged last, and tagsStyles,
classesStyles and idsStyles are JS objects which can't carry the flag.
Within one style attribute, declarations are still applied in order,
so `color: red !important; color: blue` gives blue where a browser
shows red, as it did before this change.

The CSS concepts documentation page now states that the flag is
accepted and ignored in inline styles.

Refs #59

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nsitively

CSS property names and keywords are ASCII case-insensitive, but
uppercase or mixed-case names (`COLOR: red`, `Font-Size: 20px`) failed
registry lookup, and uppercase keywords (`display: FLEX`,
`margin-top: 5PX`, `color: RED`) were either discarded or passed through
to React Native, which rejects them.

Names and values are first matched as written, so values which are
valid as written are kept unchanged (e.g. `#FFF`) and camelCase
property names such as `paddingBottom` keep working. On failure, they
get a second attempt lowercased:
- property names are lowercased before being camelCased;
- validators opt into keyword lowercasing with the new
  lowercaseInlineCSSKeywords method: enumerations, sizes (units, `auto`,
  border width and font size keywords), colors and shorthands relying on
  css-to-react-native (border, background, flex-flow, text-decoration);
- hex colors are never lowercased, and the font shorthand only
  lowercases the part preceding the font family;
- font-family, list-style-type and transform values are left untouched,
  since lowercasing would change what they refer to: font families are
  matched by exact name against the available fonts, list style types
  may name user-defined customListStyleSpecs entries, and
  css-to-react-native expects camelCase transform functions such as
  `translateX`.

This only applies to inline CSS: React Native style values stay
case-sensitive.

Refs #59

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@5ZYSZ3K
5ZYSZ3K requested a review from jsamr November 18, 2026 14:19
@5ZYSZ3K 5ZYSZ3K self-assigned this Nov 18, 2026
@codecov

codecov Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.69%. Comparing base (1cc7e15) to head (9bfbb49).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #61      +/-   ##
==========================================
+ Coverage   98.65%   98.69%   +0.03%     
==========================================
  Files         140      141       +1     
  Lines        2088     2143      +55     
  Branches      638      648      +10     
==========================================
+ Hits         2060     2115      +55     
  Misses         27       27              
  Partials        1        1              
Flag Coverage Δ
css-processor 100.00% <100.00%> (ø)
render 97.85% <ø> (ø)
transient-render-engine 98.95% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

…x values

Two lines were left uncovered:
- ShortFontCSSValidator.lowercaseInlineCSSKeywords returns the value
  unchanged when it has no font size;
- ShortMergeRequest.entries with null properties is no longer reached
  through ShortCSSToReactNativeValidator, which now returns null when
  css-to-react-native can't expand a value. It is still reached through
  the flex shorthand.

Also reformat a test changed by the case-insensitivity commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

css-processor: negative lengths and uppercase property names are dropped, and color validator accepts invalid strings

1 participant