@@ -1277,19 +1277,20 @@ fn parse_legacy_alpha<'i, 't, P>(
1277
1277
color_parser : & P ,
1278
1278
arguments : & mut Parser < ' i , ' t > ,
1279
1279
uses_commas : bool ,
1280
- ) -> Result < f32 , ParseError < ' i , P :: Error > >
1280
+ ) -> Result < Option < f32 > , ParseError < ' i , P :: Error > >
1281
1281
where
1282
1282
P : ColorParser < ' i > ,
1283
1283
{
1284
1284
Ok ( if !arguments. is_exhausted ( ) {
1285
1285
if uses_commas {
1286
1286
arguments. expect_comma ( ) ?;
1287
+ Some ( parse_alpha_component ( color_parser, arguments) ?)
1287
1288
} else {
1288
1289
arguments. expect_delim ( '/' ) ?;
1289
- } ;
1290
- parse_alpha_component ( color_parser , arguments ) ?
1290
+ parse_none_or ( arguments , |p| parse_alpha_component ( color_parser , p ) ) ?
1291
+ }
1291
1292
} else {
1292
- OPAQUE
1293
+ Some ( OPAQUE )
1293
1294
} )
1294
1295
}
1295
1296
@@ -1303,32 +1304,58 @@ where
1303
1304
{
1304
1305
// Either integers or percentages, but all the same type.
1305
1306
// https://drafts.csswg.org/css-color/#rgb-functions
1306
- let ( red, is_number) = match color_parser. parse_number_or_percentage ( arguments) ? {
1307
- NumberOrPercentage :: Number { value } => ( clamp_floor_256_f32 ( value) , true ) ,
1308
- NumberOrPercentage :: Percentage { unit_value } => ( clamp_unit_f32 ( unit_value) , false ) ,
1309
- } ;
1310
1307
1311
- let uses_commas = arguments. try_parse ( |i| i. expect_comma ( ) ) . is_ok ( ) ;
1308
+ let maybe_red = parse_none_or ( arguments, |p| color_parser. parse_number_or_percentage ( p) ) ?;
1309
+
1310
+ let ( red, is_number, is_legacy_syntax) = if let Some ( red) = maybe_red {
1311
+ let is_legacy_syntax = arguments. try_parse ( |i| i. expect_comma ( ) ) . is_ok ( ) ;
1312
+ match red {
1313
+ NumberOrPercentage :: Number { value } => {
1314
+ ( clamp_floor_256_f32 ( value) , true , is_legacy_syntax)
1315
+ }
1316
+ NumberOrPercentage :: Percentage { unit_value } => {
1317
+ ( clamp_unit_f32 ( unit_value) , false , is_legacy_syntax)
1318
+ }
1319
+ }
1320
+ } else {
1321
+ ( 0 , true , false )
1322
+ } ;
1312
1323
1313
1324
let green;
1314
1325
let blue;
1315
1326
if is_number {
1316
- green = clamp_floor_256_f32 ( color_parser. parse_number ( arguments) ?) ;
1317
- if uses_commas {
1327
+ // parse numbers
1328
+ if is_legacy_syntax {
1329
+ green = clamp_floor_256_f32 ( color_parser. parse_number ( arguments) ?) ;
1318
1330
arguments. expect_comma ( ) ?;
1331
+ blue = clamp_floor_256_f32 ( color_parser. parse_number ( arguments) ?) ;
1332
+ } else {
1333
+ green = clamp_floor_256_f32 (
1334
+ parse_none_or ( arguments, |p| color_parser. parse_number ( p) ) ?. unwrap_or ( 0.0 ) ,
1335
+ ) ;
1336
+ blue = clamp_floor_256_f32 (
1337
+ parse_none_or ( arguments, |p| color_parser. parse_number ( p) ) ?. unwrap_or ( 0.0 ) ,
1338
+ ) ;
1319
1339
}
1320
- blue = clamp_floor_256_f32 ( color_parser. parse_number ( arguments) ?) ;
1321
1340
} else {
1322
- green = clamp_unit_f32 ( color_parser. parse_percentage ( arguments) ?) ;
1323
- if uses_commas {
1341
+ // parse percentages
1342
+ if is_legacy_syntax {
1343
+ green = clamp_unit_f32 ( color_parser. parse_percentage ( arguments) ?) ;
1324
1344
arguments. expect_comma ( ) ?;
1345
+ blue = clamp_unit_f32 ( color_parser. parse_percentage ( arguments) ?) ;
1346
+ } else {
1347
+ green = clamp_unit_f32 (
1348
+ parse_none_or ( arguments, |p| color_parser. parse_percentage ( p) ) ?. unwrap_or ( 0.0 ) ,
1349
+ ) ;
1350
+ blue = clamp_unit_f32 (
1351
+ parse_none_or ( arguments, |p| color_parser. parse_percentage ( p) ) ?. unwrap_or ( 0.0 ) ,
1352
+ ) ;
1325
1353
}
1326
- blue = clamp_unit_f32 ( color_parser. parse_percentage ( arguments) ?) ;
1327
1354
}
1328
1355
1329
- let alpha = parse_legacy_alpha ( color_parser, arguments, uses_commas ) ?;
1356
+ let alpha = parse_legacy_alpha ( color_parser, arguments, is_legacy_syntax ) ?;
1330
1357
1331
- Ok ( P :: Output :: from_rgba ( red, green, blue, alpha) )
1358
+ Ok ( P :: Output :: from_rgba ( red, green, blue, alpha. unwrap_or ( 0.0 ) ) )
1332
1359
}
1333
1360
1334
1361
/// Parses hsl syntax.
@@ -1554,7 +1581,7 @@ where
1554
1581
r2 = Some ( f2 ( color_parser, input) ?) ;
1555
1582
input. expect_comma ( ) ?;
1556
1583
r3 = Some ( f3 ( color_parser, input) ?) ;
1557
- alpha = Some ( parse_legacy_alpha ( color_parser, input, is_legacy_syntax) ?) ;
1584
+ alpha = parse_legacy_alpha ( color_parser, input, is_legacy_syntax) ?;
1558
1585
} else {
1559
1586
r2 = parse_none_or ( input, |p| f2 ( color_parser, p) ) ?;
1560
1587
r3 = parse_none_or ( input, |p| f3 ( color_parser, p) ) ?;
0 commit comments