Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(svg): normalize length unit case in scalar and list parsing

ldm02110db6ae621
diff --git a/moli-svg/src/length.rs b/moli-svg/src/length.rs
index a1dfbe9f9..d5b60eea0 100644
--- a/moli-svg/src/length.rs
+++ b/moli-svg/src/length.rs
@@ -55,11 +55,16 @@ impl SvgLength {
}
pub fn parse_length(raw: &str) -> Option<SvgLength> {
- raw.trim().parse::<SvgTypesLength>().ok().map(svg_length)
+ raw.trim()
+ .to_ascii_lowercase()
+ .parse::<SvgTypesLength>()
+ .ok()
+ .map(svg_length)
}
pub fn parse_length_list(raw: &str) -> Option<Vec<SvgLength>> {
- LengthListParser::from(raw)
+ let normalized = raw.to_ascii_lowercase();
+ LengthListParser::from(normalized.as_str())
.map(|length| length.ok().map(svg_length))
.collect()
}
diff --git a/moli-svg/src/lib.rs b/moli-svg/src/lib.rs
index 7da2930fb..998af75e0 100644
--- a/moli-svg/src/lib.rs
+++ b/moli-svg/src/lib.rs
@@ -30,6 +30,22 @@ mod tests {
segments_for_element, serialize_number, serialize_transform_list,
};
+ #[test]
+ fn length_units_are_ascii_case_insensitive_in_scalars_and_lists() {
+ assert_eq!(parse_length(" 1pX ").unwrap().serialize(), "1px");
+ assert_eq!(parse_length("2EM").unwrap().unit, crate::SvgLengthUnit::Ems);
+ let values = parse_length_list("1pX, 2EM 3cM").unwrap();
+ assert_eq!(
+ values
+ .iter()
+ .map(|value| value.serialize())
+ .collect::<Vec<_>>(),
+ ["1px", "2em", "3cm"]
+ );
+ assert!(parse_length("1PXjunk").is_none());
+ assert!(parse_length_list("1PX 2not-a-unit").is_none());
+ }
+
fn path_segments(raw: &str) -> Vec<SvgGeometrySegment> {
segments_for_element(SvgGeometryElement::Path { d: raw.to_owned() })
}
@@ -392,6 +408,7 @@ mod tests {
assert_close(length.value, 1.5);
assert_eq!(length.unit, SvgLengthUnit::Ems);
assert_eq!(length.serialize(), "1.5em");
+ assert_eq!(parse_length("1pX").unwrap().unit, SvgLengthUnit::Px);
assert_eq!(serialize_number(3.0), "3");
assert_eq!(serialize_number(3.25), "3.25");
assert!(parse_length("1 px").is_none());