@@ -1277,19 +1277,20 @@ fn parse_legacy_alpha<'i, 't, P>(
12771277 color_parser : & P ,
12781278 arguments : & mut Parser < ' i , ' t > ,
12791279 uses_commas : bool ,
1280- ) -> Result < f32 , ParseError < ' i , P :: Error > >
1280+ ) -> Result < Option < f32 > , ParseError < ' i , P :: Error > >
12811281where
12821282 P : ColorParser < ' i > ,
12831283{
12841284 Ok ( if !arguments. is_exhausted ( ) {
12851285 if uses_commas {
12861286 arguments. expect_comma ( ) ?;
1287+ Some ( parse_alpha_component ( color_parser, arguments) ?)
12871288 } else {
12881289 arguments. expect_delim ( '/' ) ?;
1289- } ;
1290- parse_alpha_component ( color_parser , arguments ) ?
1290+ parse_none_or ( arguments , |p| parse_alpha_component ( color_parser , p ) ) ?
1291+ }
12911292 } else {
1292- OPAQUE
1293+ Some ( OPAQUE )
12931294 } )
12941295}
12951296
@@ -1303,32 +1304,58 @@ where
13031304{
13041305 // Either integers or percentages, but all the same type.
13051306 // 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- } ;
13101307
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+ } ;
13121323
13131324 let green;
13141325 let blue;
13151326 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) ?) ;
13181330 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+ ) ;
13191339 }
1320- blue = clamp_floor_256_f32 ( color_parser. parse_number ( arguments) ?) ;
13211340 } 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) ?) ;
13241344 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+ ) ;
13251353 }
1326- blue = clamp_unit_f32 ( color_parser. parse_percentage ( arguments) ?) ;
13271354 }
13281355
1329- let alpha = parse_legacy_alpha ( color_parser, arguments, uses_commas ) ?;
1356+ let alpha = parse_legacy_alpha ( color_parser, arguments, is_legacy_syntax ) ?;
13301357
1331- Ok ( P :: Output :: from_rgba ( red, green, blue, alpha) )
1358+ Ok ( P :: Output :: from_rgba ( red, green, blue, alpha. unwrap_or ( 0.0 ) ) )
13321359}
13331360
13341361/// Parses hsl syntax.
@@ -1554,7 +1581,7 @@ where
15541581 r2 = Some ( f2 ( color_parser, input) ?) ;
15551582 input. expect_comma ( ) ?;
15561583 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) ?;
15581585 } else {
15591586 r2 = parse_none_or ( input, |p| f2 ( color_parser, p) ) ?;
15601587 r3 = parse_none_or ( input, |p| f3 ( color_parser, p) ) ?;
0 commit comments