Keep full precision when converting px to rem in code export

Found while testing crates/engine/src/codegen.rs:122-150 (lengths_to_rem calls num, which rounds to 2 decimals, at :112)

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
NetterSilverandClaude Haiku 5.5 committed 2026-10-26 06:24:14 +00:00
1 parent 26291aff2c
commit ca6426b9a1
2 files changed
+15 -2

No files matched your search

+7 -1
View File
@@ -137,6 +137,12 @@ pub(crate) fn num(v: f64) -> String {
}
}
/// `v` without trailing zeros (6 decimals), for lengths that must convert back to the same pixels.
fn num6(v: f64) -> String {
let r = (v * 1_000_000.0).round() / 1_000_000.0;
if r.is_finite() && r != 0.0 { format!("{r}") } else { "0".into() }
}
/// `code` with every pixel length (`12px`) in rem of `root` pixels (`0.75rem`); names that end
/// in "px", markup lines (layer text), string literals and comments are left alone.
pub fn px_to_rem(code: &str, root: f64) -> String {
@@ -202,7 +208,7 @@ fn lengths_to_rem(code: &str, root: f64) -> String {
match number.parse::<f64>() {
Ok(v) if free && v.is_finite() => {
out.push_str(before);
out.push_str(&format!("{}rem", num(v / root)));
out.push_str(&format!("{}rem", num6(v / root)));
}
_ => {
out.push_str(head);
+8 -1
View File
@@ -340,6 +340,13 @@ fn rem_conversion_leaves_names_alone() {
let f = crate::codegen::px_to_rem;
// Lengths convert, names ending in "px" don't.
assert_eq!(f("width: 32px; --gap-8px: 1; margin: -8px 0px;", 16.0), "width: 2rem; --gap-8px: 1; margin: -0.5rem 0rem;");
assert_eq!(f("épx 1px px", 16.0), "épx 0.06rem px");
assert_eq!(f("épx 1px px", 16.0), "épx 0.0625rem px");
assert_eq!(f("", 16.0), "");
}
#[test]
fn rem_conversion_keeps_the_original_pixels() {
let f = crate::codegen::px_to_rem;
assert_eq!(f("padding: 3px;", 16.0), "padding: 0.1875rem;");
assert_eq!(f("gap: 0.5px;", 16.0), "gap: 0.03125rem;");
}