Skip to content

Commit

Permalink
fix viewbox with decimals (#4765)
Browse files Browse the repository at this point in the history
### Description

see vercel/next.js#48947 (comment)

thanks @joacub
  • Loading branch information
sokra committed May 1, 2023
1 parent c2f0540 commit d4cb88f
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions crates/turbopack-image/src/process/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static UNITS: Lazy<HashMap<&str, f64>> = Lazy::new(|| {
});

static UNIT_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"^([0-9.]+(?:e\d+)?)((?:in|cm|em|ex|m|mm|pc|pt|px)?)$").unwrap());
Lazy::new(|| Regex::new(r"^([0-9.]+(?:e-?\d+)?)((?:in|cm|em|ex|m|mm|pc|pt|px)?)$").unwrap());

static ROOT_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r#"<svg\s([^>"']|"[^"]*"|'[^']*')*>"#).unwrap());
Expand All @@ -33,8 +33,10 @@ static HEIGHT_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r#"\sheight=['"]([^%]+?)['"]"#).unwrap());
static VIEW_BOX_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r#"\sviewBox=['"](.+?)['"]"#).unwrap());
static VIEW_BOX_CONTENT_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r#"^\s*(\w+)\s+(\w+)\s+(\w+)\s+(\w+)\s*$"#).unwrap());
static VIEW_BOX_CONTENT_REGEX: Lazy<Regex> = Lazy::new(|| {
Regex::new(r#"^\s*((?:\w|\.|-)+)\s+((?:\w|\.|-)+)\s+((?:\w|\.|-)+)\s+((?:\w|\.|-)+)\s*$"#)
.unwrap()
});

fn parse_length(len: &str) -> Result<f64> {
let captures = UNIT_REGEX
Expand Down Expand Up @@ -159,4 +161,12 @@ mod tests {
assert_eq!(result, (160, 80));
Ok(())
}

#[test]
fn test_calculate_complex_viewbox() -> Result<()> {
let svg = r#"<svg viewBox="-100 -10.5 5000e-2 50.42e3"></svg>"#;
let result = calculate(svg)?;
assert_eq!(result, (50, 50420));
Ok(())
}
}

0 comments on commit d4cb88f

Please sign in to comment.