Skip to content

Commit

Permalink
Make legacy rgb parsing more compliant
Browse files Browse the repository at this point in the history
  • Loading branch information
eero-lehtinen committed Nov 28, 2024
1 parent c41279a commit f52ef82
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/formats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ fn css_alpha_parser(input: &mut &str) -> PResult<f32> {
fn css_legacy_alpha_parser(input: &mut &str) -> PResult<f32> {
opt(delimited(
(space0, ',', space0),
css_num_parser.map(|n| n.apply()),
css_legacy_num_parser.map(|n| n.apply()),
space0,
))
.map(|n| n.unwrap_or(1.))
Expand Down Expand Up @@ -481,17 +481,22 @@ fn rgb_legacy_parser(input: &mut &str) -> PResult<Srgba> {
color_read_parser(
("rgb", opt('a')).void(),
(
terminated(
css_legacy_num_parser.map(|n| n.as_u8()),
(space0, ',', space0),
),
terminated(
css_legacy_num_parser.map(|n| n.as_u8()),
(space0, ',', space0),
),
css_legacy_num_parser.map(|n| n.as_u8()),
terminated(css_legacy_num_parser, (space0, ',', space0)),
terminated(css_legacy_num_parser, (space0, ',', space0)),
css_legacy_num_parser,
css_legacy_alpha_parser,
),
)
.verify(|(r, g, b, _)| {
matches!(
(r, g, b),
(
CssNum::Percentage(_),
CssNum::Percentage(_),
CssNum::Percentage(_)
) | (CssNum::Num(_), CssNum::Num(_), CssNum::Num(_))
)
})
.map(|(r, g, b, a)| (r.as_u8(), g.as_u8(), b.as_u8(), a)),
)
.parse_next(input)
}
Expand Down Expand Up @@ -628,7 +633,6 @@ mod tests {
fn fail_rgb2() {
assert_eq!(parse_color("rgb(1 2)", ColorFormat::Rgb), None);
}

#[test]
fn fail_rgb3() {
assert_eq!(parse_color("rgb()", ColorFormat::Rgb), None);
Expand All @@ -647,6 +651,11 @@ mod tests {
);
}

#[test]
fn fail_rgb_legacy_mixed_units() {
assert_eq!(parse_color("rgb(1.0%, 1, 1)", ColorFormat::Rgb), None);
}

#[test]
fn hsl_legacy() {
assert_eq!(
Expand Down

0 comments on commit f52ef82

Please sign in to comment.