diff --git a/crates/ui-egui/src/app/commands.rs b/crates/ui-egui/src/app/commands.rs index 86bed7fe6d..9c5775806d 100644 --- a/crates/ui-egui/src/app/commands.rs +++ b/crates/ui-egui/src/app/commands.rs @@ -145,7 +145,9 @@ impl WordApp { // Web: saving and exporting become downloads. if self.services.download.is_some() && matches!(id, "file.save" | "file.saveAs" | "file.exportPdf" | "file.exportPng") { let name = params.get("path").and_then(Value::as_str).map(str::to_string).unwrap_or_else(|| self.web_save_name(id)); - let name = if id == "file.exportPdf" && !name.ends_with(".pdf") { format!("{name}.pdf") } else { name }; + // Inspect only the suffix bytes: no full-name allocation or UTF-8 slicing. + let has_pdf_suffix = name.as_bytes().last_chunk::<4>().is_some_and(|suffix| suffix.eq_ignore_ascii_case(b".pdf")); + let name = if id == "file.exportPdf" && !has_pdf_suffix { format!("{name}.pdf") } else { name }; // A save with a password encrypts this and later saves (`file.save`'s `password`). if matches!(id, "file.save" | "file.saveAs") && let Some(pw) = params.get("password").filter(|v| !v.is_null()) diff --git a/crates/ui-egui/src/file_dialogs/tests.rs b/crates/ui-egui/src/file_dialogs/tests.rs index 6ab3e77552..000aca1108 100644 --- a/crates/ui-egui/src/file_dialogs/tests.rs +++ b/crates/ui-egui/src/file_dialogs/tests.rs @@ -465,6 +465,39 @@ fn export_pdf_without_a_path_asks_where_to_save() { let _ = std::fs::remove_dir_all(&dir); } +/// An explicit web PDF export keeps an existing PDF suffix regardless of ASCII case. +#[test] +fn web_pdf_export_keeps_an_existing_suffix_in_any_case() { + let downloaded = Rc::new(RefCell::new(Vec::new())); + let captured = downloaded.clone(); + let services = Services { + download: Some(Box::new(move |name, bytes| { + captured.borrow_mut().push((name.to_string(), bytes.starts_with(b"%PDF"))); + Ok(()) + })), + ..Default::default() + }; + let mut a = WordApp::new(Session::new(wordcraft_doc::Document::new()), services); + for (name, expected) in [ + ("Report.pdf", "Report.pdf"), + ("Report.PDF", "Report.PDF"), + ("Résumé.PdF", "Résumé.PdF"), + ("😀.PDF", "😀.PDF"), + (".hidden.PDF", ".hidden.PDF"), + ("Report..PDF", "Report..PDF"), + ("é", "é.pdf"), + ("😀", "😀.pdf"), + ("Report.pdf.exe", "Report.pdf.exe.pdf"), + ("Report.PDF", "Report.PDF.pdf"), + ("Report.pdf.", "Report.pdf..pdf"), + ("Report", "Report.pdf"), + ] { + let result = a.execute("file.exportPdf", json!({"path": name})).unwrap(); + assert_eq!(result["downloaded"], expected, "{name}"); + assert_eq!(downloaded.borrow().last().cloned(), Some((expected.to_string(), true)), "{name}"); + } +} + /// Open, Save As and Export PNG from the command search (no path) ask for a file the same way. #[test] fn file_commands_without_a_path_ask_for_one() {