stylex icon indicating copy to clipboard operation
stylex copied to clipboard

[style-value-parser] Build CSS validator

Open mellyeliu opened this issue 7 months ago • 0 comments

Describe the feature request

Proposal

The idea is to:

  • validate CSS values for correctness (eg. reject margin: "15" with no unit)
  • normalize semantically equivalent values (eg. rgb(0, 0, 0)#000)
    • reduce duplication by standardizing outputs at the compiler level
    • polyfill newer styles

We'll start with properties that are error prone and syntactically complex

  • calc() expressions
  • length-based properties (margin, padding, etc.)
  • @media queries

Usage

Two options

validate(property, value) =>

normalize(property, value) =>

OR

normalizeColor('rgb(255, 0, 127)' ) => { color: '#ff007f' }
validateMargin('15')           // throws (missing unit)

normalizeMediaQuery('@media (width >= 768px and width <= 1024px)') => '@media (min-width: 768px) and (max-width: 1024px)'
normalizeMediaQuery('@media not (not ((min-width: 600px) and (color)))') => '@media (min-width: 600px) and (color)'
validateMediaQuery('@media (min-width: var(--x))') // unsupported

mellyeliu avatar May 02 '25 01:05 mellyeliu