@@ -139,212 +139,205 @@ describe("hsla", () => {
139139 } ) ;
140140} ) ;
141141
142- describe ( "unresolved alpha" , ( ) => {
143- test ( "rgb with number channels" , ( ) => {
144- registerCSS ( `.my-class {
145- background-color: rgb(255 0 0 / var(--a, 0.5));
146- }` ) ;
142+ // Chromium 153, Firefox 155 and WebKit 26.6 compute each colour below with an alpha of 0.5
143+ const BROWSER_COLORS = [
144+ [ "rgb(255 0 0)" , "rgba(255, 0, 0, 0.5)" ] ,
145+ [ "rgb(100% 0% 0%)" , "rgba(255, 0, 0, 0.5)" ] ,
146+ [ "rgb(51 102 153)" , "rgba(51, 102, 153, 0.5)" ] ,
147+ [ "rgb(127.5 0 0)" , "rgba(128, 0, 0, 0.5)" ] ,
148+ [ "rgb(50% 25% 10%)" , "rgba(128, 64, 26, 0.5)" ] ,
149+ [ "rgb(50% 25% 75%)" , "rgba(128, 64, 191, 0.5)" ] ,
150+ [ "rgb(300 -20 0)" , "rgba(255, 0, 0, 0.5)" ] ,
151+ [ "hsl(120 50% 50%)" , "rgba(64, 191, 64, 0.5)" ] ,
152+ [ "hsl(120deg 100% 25%)" , "rgba(0, 128, 0, 0.5)" ] ,
153+ [ "hsl(0.5turn 50% 50%)" , "rgba(64, 191, 191, 0.5)" ] ,
154+ [ "hsl(3.14159rad 50% 50%)" , "rgba(64, 191, 191, 0.5)" ] ,
155+ [ "hsl(200grad 50% 50%)" , "rgba(64, 191, 191, 0.5)" ] ,
156+ [ "hsl(480 50% 50%)" , "rgba(64, 191, 64, 0.5)" ] ,
157+ [ "hsl(-120 50% 50%)" , "rgba(64, 64, 191, 0.5)" ] ,
158+ [ "hsl(-600 100% 50%)" , "rgba(0, 255, 0, 0.5)" ] ,
159+ [ "hsl(1e7 100% 50%)" , "rgba(170, 0, 255, 0.5)" ] ,
160+ [ "hsl(120 150% 50%)" , "rgba(0, 255, 0, 0.5)" ] ,
161+ [ "hsl(120 50% 120%)" , "rgba(255, 255, 255, 0.5)" ] ,
162+ [ "hsl(120 -10% 50%)" , "rgba(128, 128, 128, 0.5)" ] ,
163+ [ "hsl(0 84.2% 60.2%)" , "rgba(239, 68, 68, 0.5)" ] ,
164+ [ "hsl(33.3 47.4% 11.2%)" , "rgba(42, 30, 15, 0.5)" ] ,
165+ ] as const ;
166+
167+ // The same browsers render an infinite or NaN hue as hue 0
168+ const NON_FINITE_HUE_COLORS = [
169+ [ "hsl(calc(infinity) 50% 50%)" , "rgba(191, 64, 64, 0.5)" ] ,
170+ [ "hsl(calc(-infinity) 50% 50%)" , "rgba(191, 64, 64, 0.5)" ] ,
171+ [ "hsl(calc(NaN) 50% 50%)" , "rgba(191, 64, 64, 0.5)" ] ,
172+ ] as const ;
173+
174+ function withAlpha ( color : string , alpha : string ) {
175+ return `${ color . slice ( 0 , - 1 ) } / ${ alpha } )` ;
176+ }
177+
178+ function renderedBackground ( id = testID ) {
179+ const style : unknown = screen . getByTestId ( id ) . props . style ;
180+
181+ return typeof style === "object" &&
182+ style !== null &&
183+ "backgroundColor" in style &&
184+ typeof style . backgroundColor === "string"
185+ ? processColor ( style . backgroundColor )
186+ : undefined ;
187+ }
188+
189+ function renderInParent ( ) {
190+ render (
191+ < View className = "parent" >
192+ < View testID = { testID } className = "my-class" />
193+ </ View > ,
194+ ) ;
195+ }
196+
197+ describe ( "a colour with an unresolved alpha renders what browsers compute" , ( ) => {
198+ test . each ( [ ...BROWSER_COLORS , ...NON_FINITE_HUE_COLORS ] ) (
199+ "%s, alpha read at runtime" ,
200+ ( color , expected ) => {
201+ registerCSS (
202+ `.parent { --a: 0.5; } .my-class { background-color: ${ withAlpha ( color , "var(--a)" ) } ; }` ,
203+ { inlineVariables : false } ,
204+ ) ;
147205
148- render ( < View testID = { testID } className = "my-class" /> ) ;
149- const component = screen . getByTestId ( testID ) ;
206+ renderInParent ( ) ;
150207
151- expect ( component . props . style ) . toStrictEqual ( {
152- backgroundColor : "rgba(255, 0, 0, 0.5)" ,
153- } ) ;
154- } ) ;
208+ expect ( renderedBackground ( ) ) . toBe ( processColor ( expected ) ) ;
209+ } ,
210+ ) ;
155211
156- test ( "rgb with percentage channels" , ( ) => {
157- registerCSS ( `.my-class {
158- background-color: rgb(50% 25% 10% / var(--a, 0.5));
159- }` ) ;
212+ test . each ( [ ...BROWSER_COLORS , ...NON_FINITE_HUE_COLORS ] ) (
213+ "%s, alpha read from a fallback" ,
214+ ( color , expected ) => {
215+ registerCSS (
216+ `.my-class { background-color: ${ withAlpha ( color , "var(--a, 0.5)" ) } ; }` ,
217+ ) ;
160218
161- render ( < View testID = { testID } className = "my-class" /> ) ;
162- const component = screen . getByTestId ( testID ) ;
219+ renderInParent ( ) ;
163220
164- expect ( component . props . style ) . toStrictEqual ( {
165- backgroundColor : "rgba(128, 64, 26, 0.5)" ,
166- } ) ;
167- } ) ;
221+ expect ( renderedBackground ( ) ) . toBe ( processColor ( expected ) ) ;
222+ } ,
223+ ) ;
168224
169- // The resolved path compiles the same channels to `#ef4444`, and React Native
170- // rejects both `hsl()` carrying an alpha and `hsla()` missing one.
171- test ( "hsl resolves to the same channels as the resolved path" , ( ) => {
172- registerCSS ( `.my-class {
173- background-color: hsl(0 84.2% 60.2% / var(--a, 0.5));
174- }` ) ;
225+ test . each ( BROWSER_COLORS ) (
226+ "%s, alpha folded at compile time" ,
227+ ( color , expected ) => {
228+ registerCSS (
229+ `.parent { --a: 0.5; } .my-class { background-color: ${ withAlpha ( color , " var(--a)" ) } ; }` ,
230+ ) ;
175231
176- render ( < View testID = { testID } className = "my-class" /> ) ;
177- const component = screen . getByTestId ( testID ) ;
232+ renderInParent ( ) ;
178233
179- expect ( component . props . style ) . toStrictEqual ( {
180- backgroundColor : "rgba(239, 68, 68, 0.5)" ,
181- } ) ;
182- } ) ;
234+ expect ( renderedBackground ( ) ) . toBe ( processColor ( expected ) ) ;
235+ } ,
236+ ) ;
183237
184- // lightningcss clamps saturation, lightness and every rgb channel, so the hue
185- // is the only channel an out-of-range `calc()` reaches the compiler through.
186- // It arrives as a 32-bit float, and past 2**32 one step of that grid covers
187- // more than a turn, so the value no longer names an angle. Each row below is a
188- // different way of landing past it and they all compile to one colour.
189- test . each ( [
190- "calc(NaN)" , // serialized past the float range, reparses to Infinity
191- "calc(infinity)" , // reparses saturated, at 9223372036854776000
192- "calc(-infinity)" ,
193- "4294967296" , // 2**32, where one step of the grid first covers a turn
194- "1e20" , // saturates too, arriving as 9223369837831520000
195- "1e38" , // the same value: past the ceiling the hue is no longer carried
196- ] ) ( "hsl with a hue the float grid cannot name: %s" , ( hue ) => {
197- registerCSS ( `.my-class {
198- background-color: hsl(${ hue } 100% 50% / var(--a, 0.5));
199- }` ) ;
238+ test . each ( BROWSER_COLORS ) (
239+ "%s, resolved at compile time" ,
240+ ( color , expected ) => {
241+ registerCSS (
242+ `.my-class { background-color: ${ withAlpha ( color , "0.5" ) } ; }` ,
243+ ) ;
200244
201- render ( < View testID = { testID } className = "my-class" /> ) ;
202- const component = screen . getByTestId ( testID ) ;
245+ renderInParent ( ) ;
203246
204- expect ( component . props . style ) . toStrictEqual ( {
205- backgroundColor : "rgba(255, 0, 0, 0.5)" ,
206- } ) ;
207- } ) ;
247+ expect ( renderedBackground ( ) ) . toBe ( processColor ( expected ) ) ;
248+ } ,
249+ ) ;
208250
209- // The other side of that boundary: a hue far outside [0, 360) but still on a
210- // part of the grid that resolves finer than a turn is reduced, never clamped.
211- //
212- // Every row has to land on a colour the clamp does NOT also produce, or it
213- // cannot tell reduction from clamping — a hue that reduces to 0 agrees with
214- // the clamp and passes either way. `3e9` is also what bounds the threshold
215- // from below: it sits between 2**31 and 2**32, where the float32 ULP is 256
216- // and so still finer than a turn, and it arrives exactly because one
217- // significant digit survives any serializer. Together with the 2**32 row
218- // above it brackets `SMALLEST_UNNAMEABLE_HUE` to within a factor of two.
219- test . each ( [
220- [ "-600" , "rgba(0, 255, 0, 0.5)" ] ,
221- [ "3e9" , "rgba(0, 255, 0, 0.5)" ] ,
222- [ "1e7" , "rgba(170, 0, 255, 0.5)" ] ,
223- ] ) ( "hsl reduces a large nameable hue: %s" , ( hue , expected ) => {
224- registerCSS ( `.my-class {
225- background-color: hsl(${ hue } 100% 50% / var(--a, 0.5));
226- }` ) ;
251+ test ( "the alpha is the variable's value on the element" , ( ) => {
252+ registerCSS (
253+ `.parent { --a: 0.5; } .my-class { --a: 0.25; background-color: ${ withAlpha ( "rgb(255 0 0)" , "var(--a)" ) } ; }` ,
254+ { inlineVariables : false } ,
255+ ) ;
227256
228- render ( < View testID = { testID } className = "my-class" /> ) ;
229- const component = screen . getByTestId ( testID ) ;
257+ renderInParent ( ) ;
230258
231- expect ( component . props . style ) . toStrictEqual ( {
232- backgroundColor : expected ,
233- } ) ;
259+ expect ( renderedBackground ( ) ) . toBe ( processColor ( "rgba(255, 0, 0, 0.25)" ) ) ;
234260 } ) ;
261+ } ) ;
235262
236- test ( "light-dark carries an unresolved alpha into both schemes" , ( ) => {
237- registerCSS ( `.my-class {
238- background-color: light-dark(
239- rgb(50% 25% 10% / var(--a, 0.5)),
240- hsl(120 100% 50% / var(--a, 0.5))
241- );
242- }` ) ;
243-
244- render ( < View testID = { testID } className = "my-class" /> ) ;
245- const component = screen . getByTestId ( testID ) ;
246-
247- expect ( component . props . style ) . toStrictEqual ( {
248- backgroundColor : "rgba(128, 64, 26, 0.5)" ,
249- } ) ;
263+ describe ( "a hue the second lightningcss pass reads as a float32" , ( ) => {
264+ // Chromium and WebKit reduce 3e9 to 120deg; Firefox renders it as hue 0
265+ test ( "below 2 ** 32 the hue reduces" , ( ) => {
266+ registerCSS (
267+ `.my-class { background-color: ${ withAlpha ( "hsl(3e9 100% 50%)" , "var(--a, 0.5)" ) } ; }` ,
268+ ) ;
250269
251- act ( ( ) => {
252- colorScheme . set ( "dark" ) ;
253- } ) ;
270+ renderInParent ( ) ;
254271
255- expect ( component . props . style ) . toStrictEqual ( {
256- backgroundColor : "rgba(0, 255, 0, 0.5)" ,
257- } ) ;
272+ expect ( renderedBackground ( ) ) . toBe ( processColor ( "rgba(0, 255, 0, 0.5)" ) ) ;
258273 } ) ;
259274
260- afterEach ( ( ) => {
261- act ( ( ) => {
262- colorScheme . set ( "light" ) ;
263- } ) ;
264- } ) ;
265- } ) ;
275+ test . each ( [ "4294967296" , "1e20" , "1e38" ] ) (
276+ "%s names no angle and renders as hue 0" ,
277+ ( hue ) => {
278+ registerCSS ( `
279+ .my-class { background-color: ${ withAlpha ( `hsl(${ hue } 100% 50%)` , "var(--a, 0.5)" ) } ; }
280+ .resolved { background-color: hsl(${ hue } 100% 50% / 0.5); }
281+ ` ) ;
282+
283+ render (
284+ < >
285+ < View testID = { testID } className = "my-class" />
286+ < View testID = "resolved" className = "resolved" />
287+ </ > ,
288+ ) ;
289+
290+ expect ( renderedBackground ( ) ) . toBe ( processColor ( "rgba(255, 0, 0, 0.5)" ) ) ;
291+ expect ( renderedBackground ( "resolved" ) ) . toBe ( renderedBackground ( ) ) ;
292+ } ,
293+ ) ;
266294
267- // `parseColor` compiles a fully resolved colour and `parseUnresolvedColor`
268- // compiles the same channels with the alpha left open. An opaque fallback makes
269- // the two spellings the same colour, so React Native has to read one number
270- // from both.
271- describe ( "unresolved alpha matches the resolved spelling" , ( ) => {
272- function renderedColor ( id : string ) {
273- const style : unknown = screen . getByTestId ( id ) . props . style ;
274-
275- return typeof style === "object" &&
276- style !== null &&
277- "backgroundColor" in style &&
278- typeof style . backgroundColor === "string"
279- ? processColor ( style . backgroundColor )
280- : undefined ;
281- }
282-
283- const colors = [
284- "rgb(255 0 0)" ,
285- "rgb(100% 0% 0%)" ,
286- "rgb(50% 25% 10%)" ,
287- "hsl(0 84.2% 60.2%)" ,
288- "hsl(120 100% 50%)" ,
289- "hsl(-600 100% 50%)" ,
290- "hsl(1e7 100% 50%)" ,
291- "hsl(calc(NaN) 100% 50%)" ,
292- "hsl(4294967296 100% 50%)" ,
293- "hsl(1e20 100% 50%)" ,
294- "hsl(1e38 100% 50%)" ,
295- ] as const ;
296-
297- test . each ( colors ) ( "%s" , ( color ) => {
295+ // lightningcss's first pass resolves an infinite hue to an achromatic colour; browsers render hue 0
296+ test ( "lightningcss resolves calc(infinity) to black where the unresolved spelling renders hue 0" , ( ) => {
298297 registerCSS ( `
299- .resolved { background-color: ${ color } ; }
300- .unresolved { background-color: ${ color . slice ( 0 , - 1 ) } / var(--a, 1)) ; }
298+ .resolved { background-color: hsl(calc(infinity) 100% 50%) ; }
299+ .my-class { background-color: ${ withAlpha ( "hsl(calc(infinity) 100% 50%)" , " var(--a, 1)" ) } ; }
301300 ` ) ;
302301
303302 render (
304303 < >
305304 < View testID = "resolved" className = "resolved" />
306- < View testID = "unresolved" className = "unresolved " />
305+ < View testID = { testID } className = "my-class " />
307306 </ > ,
308307 ) ;
309308
310- const expected = renderedColor ( "resolved" ) ;
311-
312- // A colour React Native rejects reads as `undefined`, which would make the
313- // comparison below pass while neither spelling renders anything.
314- expect ( typeof expected ) . toBe ( "number" ) ;
309+ expect ( renderedBackground ( "resolved" ) ) . toBe ( processColor ( "#000" ) ) ;
310+ expect ( renderedBackground ( ) ) . toBe ( processColor ( "rgb(255, 0, 0)" ) ) ;
311+ } ) ;
312+ } ) ;
315313
316- expect ( renderedColor ( "unresolved" ) ) . toBe ( expected ) ;
314+ describe ( "light-dark()" , ( ) => {
315+ afterEach ( ( ) => {
316+ act ( ( ) => {
317+ colorScheme . set ( "light" ) ;
318+ } ) ;
317319 } ) ;
318320
319- // `calc(infinity)` stands for a family, not a special case: sampling f32 hues
320- // above 2**32, about one in seven resolves to something other than the red the
321- // compiler emits, `5e10`, `1e12`, `1.44e38` and `calc(-infinity)` among them.
322- //
323- // What makes the family unmatchable is not that lightningcss is erratic — it
324- // is that the distinguishing information never reaches this compiler.
325- // Seventeen authored hues from `1e19` to `9223372036854775807` all arrive as
326- // the single value `9223369837831520000`, and lightningcss's resolved path
327- // splits that one arriving value twelve red to five black. No function of the
328- // hue this compiler receives can separate inputs it receives as one number.
329- //
330- // So the compiler emits the answer that IS a function of the arriving hue and
331- // the divergence is pinned here rather than reproduced. This goes red if
332- // lightningcss stabilises, which is when the row belongs in the list above.
333- test ( "a saturated hue diverges from lightningcss's own resolution" , ( ) => {
321+ test ( "carries an unresolved alpha into both schemes" , ( ) => {
334322 registerCSS ( `
335- .resolved { background-color: hsl(calc(infinity) 100% 50%); }
336- .unresolved { background-color: hsl(calc(infinity) 100% 50% / var(--a, 1)); }
323+ :root { color-scheme: light dark; }
324+ .my-class {
325+ background-color: light-dark(
326+ rgb(50% 25% 10% / var(--a, 0.5)),
327+ hsl(120 100% 50% / var(--a, 0.5))
328+ );
329+ }
337330 ` ) ;
338331
339- render (
340- < >
341- < View testID = "resolved" className = "resolved" />
342- < View testID = "unresolved" className = "unresolved" />
343- </ > ,
344- ) ;
332+ render ( < View testID = { testID } className = "my-class" /> ) ;
333+
334+ expect ( renderedBackground ( ) ) . toBe ( processColor ( "rgba(128, 64, 26, 0.5)" ) ) ;
335+
336+ act ( ( ) => {
337+ colorScheme . set ( "dark" ) ;
338+ } ) ;
345339
346- expect ( renderedColor ( "resolved" ) ) . toBe ( processColor ( "#000" ) ) ;
347- expect ( renderedColor ( "unresolved" ) ) . toBe ( processColor ( "rgb(255, 0, 0)" ) ) ;
340+ expect ( renderedBackground ( ) ) . toBe ( processColor ( "rgba(0, 255, 0, 0.5)" ) ) ;
348341 } ) ;
349342} ) ;
350343
0 commit comments