jsdom / cssstyle

A Node.js implementation of the CSS Object Model CSSStyleDeclaration interface
MIT License
109 stars 70 forks source link

Modernization to the current CSS rgb/rgba color parser #172

Open themojache opened 7 months ago

themojache commented 7 months ago

https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/rgb#values https://developer.mozilla.org/en-US/docs/Web/CSS/alpha-value

Both rgb and rgba contain the optional parameter of alpha despite naming convention. R, G, B are required; A is optional for both.

Test cases: ["rgb(30% 20% 50%)","rgb(153,255,153,0.5)","rgb(253, 255, 153)","rgb(255 255 255 / 50.75%)","rgba(255, 255, 255 / 50%)","rgb( 255, 255, 255, none )","rgba(none, 255, 255 / none)"]

Remains unsupported: Relative value syntax (rgb(from R G B[ / A]))

It seems like Math.round is used where you use Math.floor in browser implementations of handling percent byte rgb values (see MDN's test case of the first test case).

Thanks for the work in maintaining this library. Hope a few touch-ups are appreciated. Reach out/add commits if you notice any impactful bugs. I focused on making the parser more flexible and reducing code (for individual cases). ✌️

Alternative: https://github.com/jsdom/cssstyle/pull/171