@@ -1276,21 +1276,15 @@ where
1276
1276
fn parse_legacy_alpha < ' i , ' t , P > (
1277
1277
color_parser : & P ,
1278
1278
arguments : & mut Parser < ' i , ' t > ,
1279
- uses_commas : bool ,
1280
- ) -> Result < Option < f32 > , ParseError < ' i , P :: Error > >
1279
+ ) -> Result < f32 , ParseError < ' i , P :: Error > >
1281
1280
where
1282
1281
P : ColorParser < ' i > ,
1283
1282
{
1284
1283
Ok ( if !arguments. is_exhausted ( ) {
1285
- if uses_commas {
1286
- arguments. expect_comma ( ) ?;
1287
- Some ( parse_alpha_component ( color_parser, arguments) ?)
1288
- } else {
1289
- arguments. expect_delim ( '/' ) ?;
1290
- parse_none_or ( arguments, |p| parse_alpha_component ( color_parser, p) ) ?
1291
- }
1284
+ arguments. expect_comma ( ) ?;
1285
+ parse_alpha_component ( color_parser, arguments) ?
1292
1286
} else {
1293
- Some ( OPAQUE )
1287
+ OPAQUE
1294
1288
} )
1295
1289
}
1296
1290
@@ -1302,60 +1296,62 @@ fn parse_rgb<'i, 't, P>(
1302
1296
where
1303
1297
P : ColorParser < ' i > ,
1304
1298
{
1305
- // Either integers or percentages, but all the same type.
1306
- // https://drafts.csswg.org/css-color/#rgb-functions
1307
-
1308
1299
let maybe_red = parse_none_or ( arguments, |p| color_parser. parse_number_or_percentage ( p) ) ?;
1309
1300
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 {
1301
+ // If the first component is not "none" and is followed by a comma, then we
1302
+ // are parsing the legacy syntax.
1303
+ let is_legacy_syntax = maybe_red. is_some ( ) && arguments. try_parse ( |p| p. expect_comma ( ) ) . is_ok ( ) ;
1304
+
1305
+ let red: u8 ;
1306
+ let green: u8 ;
1307
+ let blue: u8 ;
1308
+
1309
+ let alpha = if is_legacy_syntax {
1310
+ match maybe_red. unwrap ( ) {
1313
1311
NumberOrPercentage :: Number { value } => {
1314
- ( clamp_floor_256_f32 ( value) , true , is_legacy_syntax)
1312
+ red = clamp_floor_256_f32 ( value) ;
1313
+ green = clamp_floor_256_f32 ( color_parser. parse_number ( arguments) ?) ;
1314
+ arguments. expect_comma ( ) ?;
1315
+ blue = clamp_floor_256_f32 ( color_parser. parse_number ( arguments) ?) ;
1315
1316
}
1316
1317
NumberOrPercentage :: Percentage { unit_value } => {
1317
- ( clamp_unit_f32 ( unit_value) , false , is_legacy_syntax)
1318
+ red = clamp_unit_f32 ( unit_value) ;
1319
+ green = clamp_unit_f32 ( color_parser. parse_percentage ( arguments) ?) ;
1320
+ arguments. expect_comma ( ) ?;
1321
+ blue = clamp_unit_f32 ( color_parser. parse_percentage ( arguments) ?) ;
1318
1322
}
1319
1323
}
1320
- } else {
1321
- ( 0 , true , false )
1322
- } ;
1323
1324
1324
- let green;
1325
- let blue;
1326
- if is_number {
1327
- // parse numbers
1328
- if is_legacy_syntax {
1329
- green = clamp_floor_256_f32 ( color_parser. parse_number ( arguments) ?) ;
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
- ) ;
1339
- }
1325
+ parse_legacy_alpha ( color_parser, arguments) ?
1340
1326
} else {
1341
- // parse percentages
1342
- if is_legacy_syntax {
1343
- green = clamp_unit_f32 ( color_parser. parse_percentage ( arguments) ?) ;
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
- ) ;
1327
+ #[ inline]
1328
+ fn get_component_value ( c : & Option < NumberOrPercentage > ) -> u8 {
1329
+ match * c {
1330
+ Some ( NumberOrPercentage :: Number { value } ) => clamp_floor_256_f32 ( value) ,
1331
+ Some ( NumberOrPercentage :: Percentage { unit_value } ) => clamp_unit_f32 ( unit_value) ,
1332
+ None => 0 ,
1333
+ }
1353
1334
}
1354
- }
1355
1335
1356
- let alpha = parse_legacy_alpha ( color_parser , arguments , is_legacy_syntax ) ? ;
1336
+ red = get_component_value ( & maybe_red ) ;
1357
1337
1358
- Ok ( P :: Output :: from_rgba ( red, green, blue, alpha. unwrap_or ( 0.0 ) ) )
1338
+ green = get_component_value ( & parse_none_or ( arguments, |p| {
1339
+ color_parser. parse_number_or_percentage ( p)
1340
+ } ) ?) ;
1341
+
1342
+ blue = get_component_value ( & parse_none_or ( arguments, |p| {
1343
+ color_parser. parse_number_or_percentage ( p)
1344
+ } ) ?) ;
1345
+
1346
+ if !arguments. is_exhausted ( ) {
1347
+ arguments. expect_delim ( '/' ) ?;
1348
+ parse_none_or ( arguments, |p| parse_alpha_component ( color_parser, p) ) ?. unwrap_or ( 0.0 )
1349
+ } else {
1350
+ OPAQUE
1351
+ }
1352
+ } ;
1353
+
1354
+ Ok ( P :: Output :: from_rgba ( red, green, blue, alpha) )
1359
1355
}
1360
1356
1361
1357
/// Parses hsl syntax.
@@ -1369,15 +1365,33 @@ fn parse_hsl<'i, 't, P>(
1369
1365
where
1370
1366
P : ColorParser < ' i > ,
1371
1367
{
1372
- let ( hue, saturation, lightness, alpha) = parse_legacy_components (
1373
- color_parser,
1374
- arguments,
1375
- P :: parse_angle_or_number,
1376
- P :: parse_percentage,
1377
- P :: parse_percentage,
1378
- ) ?;
1368
+ let maybe_hue = parse_none_or ( arguments, |p| color_parser. parse_angle_or_number ( p) ) ?;
1379
1369
1380
- let hue = hue. map ( |h| normalize_hue ( h. degrees ( ) ) ) ;
1370
+ // If the hue is not "none" and is followed by a comma, then we are parsing
1371
+ // the legacy syntax.
1372
+ let is_legacy_syntax = maybe_hue. is_some ( ) && arguments. try_parse ( |p| p. expect_comma ( ) ) . is_ok ( ) ;
1373
+
1374
+ let saturation: Option < f32 > ;
1375
+ let lightness: Option < f32 > ;
1376
+
1377
+ let alpha = if is_legacy_syntax {
1378
+ saturation = Some ( color_parser. parse_percentage ( arguments) ?) ;
1379
+ arguments. expect_comma ( ) ?;
1380
+ lightness = Some ( color_parser. parse_percentage ( arguments) ?) ;
1381
+ Some ( parse_legacy_alpha ( color_parser, arguments) ?)
1382
+ } else {
1383
+ saturation = parse_none_or ( arguments, |p| color_parser. parse_percentage ( p) ) ?;
1384
+ lightness = parse_none_or ( arguments, |p| color_parser. parse_percentage ( p) ) ?;
1385
+
1386
+ if !arguments. is_exhausted ( ) {
1387
+ arguments. expect_delim ( '/' ) ?;
1388
+ parse_none_or ( arguments, |p| parse_alpha_component ( color_parser, p) ) ?
1389
+ } else {
1390
+ Some ( OPAQUE )
1391
+ }
1392
+ } ;
1393
+
1394
+ let hue = maybe_hue. map ( |h| normalize_hue ( h. degrees ( ) ) ) ;
1381
1395
let saturation = saturation. map ( |s| s. clamp ( 0.0 , 1.0 ) ) ;
1382
1396
let lightness = lightness. map ( |s| s. clamp ( 0.0 , 1.0 ) ) ;
1383
1397
@@ -1553,50 +1567,7 @@ where
1553
1567
) )
1554
1568
}
1555
1569
1556
- /// Try to parse the components and alpha with the legacy syntax, but also allow
1557
- /// the [color-4] syntax if that fails.
1558
- /// https://drafts.csswg.org/css-color-4/#color-syntax-legacy
1559
- pub fn parse_legacy_components < ' i , ' t , P , F1 , F2 , F3 , R1 , R2 , R3 > (
1560
- color_parser : & P ,
1561
- input : & mut Parser < ' i , ' t > ,
1562
- f1 : F1 ,
1563
- f2 : F2 ,
1564
- f3 : F3 ,
1565
- ) -> Result < ( Option < R1 > , Option < R2 > , Option < R3 > , Option < f32 > ) , ParseError < ' i , P :: Error > >
1566
- where
1567
- P : ColorParser < ' i > ,
1568
- F1 : FnOnce ( & P , & mut Parser < ' i , ' t > ) -> Result < R1 , ParseError < ' i , P :: Error > > ,
1569
- F2 : FnOnce ( & P , & mut Parser < ' i , ' t > ) -> Result < R2 , ParseError < ' i , P :: Error > > ,
1570
- F3 : FnOnce ( & P , & mut Parser < ' i , ' t > ) -> Result < R3 , ParseError < ' i , P :: Error > > ,
1571
- {
1572
- let r1 = parse_none_or ( input, |p| f1 ( color_parser, p) ) ?;
1573
- // If the first argument is not none and we are separating with commas,
1574
- // only then are we parsing legacy syntax.
1575
- let is_legacy_syntax = r1. is_some ( ) && input. try_parse ( |i| i. expect_comma ( ) ) . is_ok ( ) ;
1576
-
1577
- let r2;
1578
- let r3;
1579
- let alpha;
1580
- if is_legacy_syntax {
1581
- r2 = Some ( f2 ( color_parser, input) ?) ;
1582
- input. expect_comma ( ) ?;
1583
- r3 = Some ( f3 ( color_parser, input) ?) ;
1584
- alpha = parse_legacy_alpha ( color_parser, input, is_legacy_syntax) ?;
1585
- } else {
1586
- r2 = parse_none_or ( input, |p| f2 ( color_parser, p) ) ?;
1587
- r3 = parse_none_or ( input, |p| f3 ( color_parser, p) ) ?;
1588
- if !input. is_exhausted ( ) {
1589
- input. expect_delim ( '/' ) ?;
1590
- alpha = parse_none_or ( input, |p| parse_alpha_component ( color_parser, p) ) ?;
1591
- } else {
1592
- alpha = Some ( OPAQUE ) ;
1593
- }
1594
- } ;
1595
-
1596
- Ok ( ( r1, r2, r3, alpha) )
1597
- }
1598
-
1599
- /// Parse the color components and alpha with the [color-4] syntax.
1570
+ /// Parse the color components and alpha with the modern [color-4] syntax.
1600
1571
pub fn parse_components < ' i , ' t , P , F1 , F2 , F3 , R1 , R2 , R3 > (
1601
1572
color_parser : & P ,
1602
1573
input : & mut Parser < ' i , ' t > ,
0 commit comments