Drop out-of-range font sizes on DOCX import

Found while testing crates/textimport/src/docx.rs:183-185

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:
NetterSilver
2026-10-19 11:56:51 +00:00
co-authored by Claude Haiku 5.5
parent 443ea4640c
commit a5ff17356d
+16 -2
View File
@@ -180,8 +180,9 @@ fn run_attrs(rpr: Option<&El>) -> CharAttrs {
if let Some(s) = r.on("strike") {
a.strikethrough = Some(s);
}
if let Some(sz) = r.val("sz").and_then(|v| v.parse::<f64>().ok()).filter(|v| v.is_finite()) {
a.size = Some(sz / 2.0);
// Half-points; sizes outside the engine's 0.1–1296 pt range (or non-finite) are dropped.
if let Some(pt) = r.val("sz").and_then(|v| v.parse::<f64>().ok()).map(|sz| sz / 2.0).filter(|pt| (0.1..=1296.0).contains(pt)) {
a.size = Some(pt);
}
if let Some(f) = r.child("rFonts").and_then(|f| f.attr("ascii").or(f.attr("hAnsi"))) {
a.font_family = Some(f.to_string());
@@ -702,6 +703,19 @@ mod tests {
}
}
#[test]
fn out_of_range_sizes_are_dropped() {
let document = format!(
r#"<w:document {W}><w:body>
<w:p><w:r><w:rPr><w:sz w:val="0"/></w:rPr><w:t>a</w:t></w:r><w:r><w:rPr><w:sz w:val="99999999"/></w:rPr><w:t>b</w:t></w:r></w:p>
</w:body></w:document>"#
);
let st = import(&docx(&document, "", "")).unwrap().story;
for at in [0, 1] {
assert_eq!(st.format_after(at).over.size, None, "run {at}");
}
}
#[test]
fn tracked_changes_import_as_accepted_including_moves() {
// Regression (#256): text in a tracked move destination (`w:moveTo`) vanished on import.