From 8a1e58ee41ae511d1437f06c87dc894476f10d99 Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sun, 1 Nov 2026 00:08:12 -0600 Subject: [PATCH] Save for Web: refuse unknown Slices choices (FredApps review) The engine read `slices` loosely: any value but "user", including the dialog's "selected" and a typo, exported every slice when `numbers` was absent. `slices` is now all, user or selected (documented in the command's parameters), anything else is refused, and Selected Slices without `numbers` on a document with slices is refused as the dialog already does ("No slices are selected"). A document without slices still exports the whole image whatever the choice, as the dialog sends it then. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/web_cmds.rs | 2 +- crates/engine/src/cmd/web_cmds/save_web.rs | 18 ++++++-- crates/engine/src/cmd/web_cmds/tests.rs | 51 ++++++++++++++++++++++ 3 files changed, 66 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/cmd/web_cmds.rs b/crates/engine/src/cmd/web_cmds.rs index 3bd4f5a6c3..a206e6bc74 100644 --- a/crates/engine/src/cmd/web_cmds.rs +++ b/crates/engine/src/cmd/web_cmds.rs @@ -80,7 +80,7 @@ pub fn specs() -> Vec { "Save for Web (Legacy)…", &["File", "Export"], Some("Cmd+Alt+Shift+S"), - r##"{"preset":"GIF 128 Dithered|JPEG High|PNG-24|…"?,"format":"gif|png8|png24|jpeg|wbmp|webp"="png24","palette":"perceptual|selective|adaptive|restrictive|exact|systemMac|systemWindows|uniform"="selective","colors":2..256=256,"colorTable":["#rrggbb",…]? (fixed opaque source palette, 1–256 entries; 255 with transparency),"colorTableEdits":{"0":"#rrggbb",…}? (replace entries after indexing; requires colorTable),"dither":"none|diffusion|pattern|noise"="diffusion","ditherAmount":0..100=88,"transparency":bool=true,"matte":"#rrggbb|none"="none" (a preset's own: white; formats without transparency go over white),"interlaced":bool=false,"webSnap":0..100=0,"quality":0..100=60 (JPEG, lossy WebP),"lossless":bool=false (WebP),"progressive":bool=false,"optimized":bool=true,"embedIcc":bool=false,"metadata":"none|copyright|copyrightAndContact|allExceptCameraInfo|all"="copyright" (EXIF only with all),"convertToSrgb":bool=true,"width"|"height"|"percent"? (image size),"resample":"bicubic|bilinear|nearest"?,"path":file? (whole image),"folder":folder? (whole image, named after the document; ignored with "path" or "dir"),"dir":folder? (one file per slice in images/),"imagesFolder":relative-folder?="images","html":bool=false,"trustedSliceHtml":bool=false (only for trusted documents; emit raw no-image cell HTML),"slices":"all|user"="all","numbers":[n]?} → no path/dir: {bytes,width,height,colors,colorTable,transparentIndex} estimate; else {files, html, bytes, warnings}"##, + r##"{"preset":"GIF 128 Dithered|JPEG High|PNG-24|…"?,"format":"gif|png8|png24|jpeg|wbmp|webp"="png24","palette":"perceptual|selective|adaptive|restrictive|exact|systemMac|systemWindows|uniform"="selective","colors":2..256=256,"colorTable":["#rrggbb",…]? (fixed opaque source palette, 1–256 entries; 255 with transparency),"colorTableEdits":{"0":"#rrggbb",…}? (replace entries after indexing; requires colorTable),"dither":"none|diffusion|pattern|noise"="diffusion","ditherAmount":0..100=88,"transparency":bool=true,"matte":"#rrggbb|none"="none" (a preset's own: white; formats without transparency go over white),"interlaced":bool=false,"webSnap":0..100=0,"quality":0..100=60 (JPEG, lossy WebP),"lossless":bool=false (WebP),"progressive":bool=false,"optimized":bool=true,"embedIcc":bool=false,"metadata":"none|copyright|copyrightAndContact|allExceptCameraInfo|all"="copyright" (EXIF only with all),"convertToSrgb":bool=true,"width"|"height"|"percent"? (image size),"resample":"bicubic|bilinear|nearest"?,"path":file? (whole image),"folder":folder? (whole image, named after the document; ignored with "path" or "dir"),"dir":folder? (one file per slice in images/),"imagesFolder":relative-folder?="images","html":bool=false,"trustedSliceHtml":bool=false (only for trusted documents; emit raw no-image cell HTML),"slices":"all|user|selected"="all" (selected requires numbers when the document has slices),"numbers":[n]? (slice numbers, each an existing slice; chooses the slices whatever the mode)} → no path/dir: {bytes,width,height,colors,colorTable,transparentIndex} estimate; else {files, html, bytes, warnings}"##, has_doc, save_for_web ), diff --git a/crates/engine/src/cmd/web_cmds/save_web.rs b/crates/engine/src/cmd/web_cmds/save_web.rs index e85c80c3e9..b614e7689e 100644 --- a/crates/engine/src/cmd/web_cmds/save_web.rs +++ b/crates/engine/src/cmd/web_cmds/save_web.rs @@ -174,12 +174,22 @@ enum SlicePick { Numbers(Vec), } -/// The slices chosen by `slices` and `numbers`. Photoshop's dialog can only select slices it -/// shows, so a script naming one that doesn't exist is refused rather than exporting too few -/// files. +/// The slices chosen by `slices` (`all|user|selected`) and `numbers`. Photoshop's dialog can +/// only select slices it shows, so a script naming one that doesn't exist, an unknown mode, or +/// Selected Slices with nothing selected is refused rather than exporting too few or too many +/// files. A document without slices is one image whatever the mode, as in the dialog. fn slice_pick(p: &Value, cmd: &str, doc: &photocraft_doc::Document) -> Result { + let mode = match p.get("slices") { + None | Some(Value::Null) => "all", + Some(Value::String(m)) if matches!(m.as_str(), "all" | "user" | "selected") => m.as_str(), + Some(v) => return Err(bad(cmd, format!("unknown slices `{}` (all|user|selected)", v.as_str().map_or_else(|| v.to_string(), str::to_string)))), + }; let Some(numbers) = p.get("numbers").filter(|v| !v.is_null()) else { - return Ok(if p.get("slices").and_then(Value::as_str) == Some("user") { SlicePick::User } else { SlicePick::All }); + return match mode { + "selected" if !doc.slices.is_empty() => Err(bad(cmd, "no slices are selected: Selected Slices needs `numbers`")), + "user" => Ok(SlicePick::User), + _ => Ok(SlicePick::All), + }; }; let numbers: Vec = numbers .as_array() diff --git a/crates/engine/src/cmd/web_cmds/tests.rs b/crates/engine/src/cmd/web_cmds/tests.rs index 2e0026bc2e..1ea3b60107 100644 --- a/crates/engine/src/cmd/web_cmds/tests.rs +++ b/crates/engine/src/cmd/web_cmds/tests.rs @@ -1496,3 +1496,54 @@ fn slice_numbers_that_do_not_exist_are_refused() { files.sort_unstable(); assert_eq!(files, ["left.png", "right.png"]); } + +#[test] +fn slice_modes_are_validated() { + // FredApps review on PR #10557: any `slices` value but "user" exported every slice, + // including the dialog's "selected" without `numbers`. + let dir = tmp("slice-modes"); + let (mut s, left, right) = two_slices(); + for (p, expect) in [ + (json!({"slices": "selcted"}), "unknown slices `selcted` (all|user|selected)"), + (json!({"slices": 2}), "unknown slices `2` (all|user|selected)"), + (json!({"slices": "selected"}), "no slices are selected"), + (json!({"slices": "selected", "numbers": []}), "numbers must be a list of slice numbers"), + (json!({"numbers": ["1"]}), "numbers must be a list of slice numbers"), + (json!({"numbers": 1}), "numbers must be a list of slice numbers"), + ] { + let mut p = p; + p["format"] = json!("png24"); + p["dir"] = json!(dir); + assert_refused(&mut s, &dir, p, expect); + } + // Every valid call still works. + let count = photocraft_doc::slices::resolve(&s.active().unwrap().doc).len(); + let names = |r: &Value| r["files"].as_array().unwrap().iter().map(|v| v.as_str().unwrap().rsplit('/').next().unwrap().to_string()).collect::>(); + for (mode, expect) in [(json!(null), count), (json!("all"), count), (json!("user"), 2)] { + let out = tmp("slice-modes-ok"); + let mut p = json!({"format": "png24", "dir": out}); + if !mode.is_null() { + p["slices"] = mode.clone(); + } + assert_eq!(names(&s.execute("file.export.saveForWebLegacy", p).unwrap()).len(), expect, "{mode}"); + } + let out = tmp("slice-modes-selected"); + let r = s.execute("file.export.saveForWebLegacy", json!({"format": "png24", "dir": out, "slices": "selected", "numbers": [right]})).unwrap(); + assert_eq!(names(&r), ["right.png"]); + let r = s.execute("file.export.saveForWebLegacy", json!({"format": "png24", "dir": out, "slices": "user", "numbers": [left]})).unwrap(); + assert_eq!(names(&r), ["left.png"], "numbers choose the slices whatever the mode"); + // A document without slices exports the whole image as one, whatever the dialog's Slices + // choice: the dialog sends "selected" without numbers then (`web_params`). + let mut s = session(8); + for mode in ["all", "user", "selected"] { + let out = tmp("slice-modes-none"); + if mode != "user" { + let r = s.execute("file.export.saveForWebLegacy", json!({"format": "png24", "dir": out, "slices": mode})).unwrap(); + assert_eq!(names(&r), ["web.png"], "{mode}"); + assert_eq!(decode(r["files"][0].as_str().unwrap()).dimensions(), (64, 48), "{mode}"); + } + let path = format!("{out}/whole.png"); + s.execute("file.export.saveForWebLegacy", json!({"format": "png24", "path": path, "slices": mode})).unwrap(); + assert_eq!(decode(&path).dimensions(), (64, 48), "{mode}"); + } +}