Save for Web: refuse slice numbers with a whole-image path (CodeRabbit review on crates/engine/src/cmd/web_cmds/save_web.rs:237)

`path` alone writes the whole image as one file, so slice `numbers` with
it were ignored. It is refused; `dir` exports the chosen slices. Main's
`folder` destination (#5305) is the whole image too, so `folder` alone
with `numbers` on a document with slices is refused the same way.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Hector VillarrealandClaude Opus 5.5 committed 2026-11-05 22:05:34 -06:00
1 parent 8a1e58ee41
commit f1427bba65
3 files changed
+45 -1

No files matched your search

+1 -1
View File
@@ -80,7 +80,7 @@ pub fn specs() -> Vec<CommandSpec> {
"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|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}"##,
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; with `path` or `folder` alone on a document with slices, refused: use `dir`)} → no path/dir: {bytes,width,height,colors,colorTable,transparentIndex} estimate; else {files, html, bytes, warnings}"##,
has_doc,
save_for_web
),
@@ -205,6 +205,16 @@ fn slice_pick(p: &Value, cmd: &str, doc: &photocraft_doc::Document) -> Result<Sl
let what = if missing.len() == 1 { "slice" } else { "slices" };
return Err(bad(cmd, format!("no {what} numbered {list} (the document's slices are 1–{count})")));
}
// `path` alone is the whole image in one file (with `slices` too, the slices go next to it):
// the chosen slices would be ignored, so a document with slices refuses it.
let given = |k: &str| p.get(k).and_then(Value::as_str).is_some_and(|v| !v.is_empty());
if given("path") && !given("dir") && p.get("slices").is_none() && count > 1 {
return Err(bad(cmd, "`path` writes the whole image as one file: to export the slices in `numbers`, use `dir`"));
}
// `folder` alone (#5305) is the whole image too, under the document's name.
if given("folder") && !given("path") && !given("dir") && count > 1 {
return Err(bad(cmd, "`folder` writes the whole image as one file: to export the slices in `numbers`, use `dir`"));
}
Ok(SlicePick::Numbers(numbers))
}
+34
View File
@@ -1547,3 +1547,37 @@ fn slice_modes_are_validated() {
assert_eq!(decode(&path).dimensions(), (64, 48), "{mode}");
}
}
#[test]
fn a_whole_image_path_with_slice_numbers_is_refused() {
// CodeRabbit review on PR #10557: `path` alone writes the whole image as one file, so slice
// `numbers` with it were ignored and every slice came out in that file. It is refused; `dir`
// exports the chosen slices.
let dir = tmp("path-numbers");
let (mut s, left, right) = two_slices();
let path = format!("{dir}/web.png");
assert_refused(&mut s, &dir, json!({"format": "png24", "path": path, "numbers": [right]}), "use `dir`");
assert_refused(&mut s, &dir, json!({"format": "png24", "path": path, "dir": "", "numbers": [left, right]}), "use `dir`");
// `folder` alone is the whole image under the document's name (#5305): refused the same way.
assert_refused(&mut s, &dir, json!({"format": "png24", "folder": dir, "numbers": [right]}), "use `dir`");
// Valid calls still work: `path` alone is the whole image; with `slices`, the chosen slices
// go next to it; `dir` with `numbers` writes those slices.
let r = s.execute("file.export.saveForWebLegacy", json!({"format": "png24", "path": path})).unwrap();
assert_eq!(r["files"], json!([path]));
assert_eq!(decode(&path).dimensions(), (64, 48));
let names = |r: &Value| r["files"].as_array().unwrap().iter().map(|v| v.as_str().unwrap().rsplit('/').next().unwrap().to_string()).collect::<Vec<_>>();
let out = tmp("path-numbers-selected");
let r = s
.execute("file.export.saveForWebLegacy", json!({"format": "png24", "path": format!("{out}/web.png"), "slices": "selected", "numbers": [right]}))
.unwrap();
assert_eq!(names(&r), ["right.png"]);
let out = tmp("path-numbers-dir");
let r = s.execute("file.export.saveForWebLegacy", json!({"format": "png24", "path": format!("{out}/web.png"), "dir": out, "numbers": [left]})).unwrap();
assert_eq!(names(&r), ["left.png"]);
// A document without slices is one image: its only slice number is the whole image.
let mut s = session(8);
let out = tmp("path-numbers-none");
let whole = format!("{out}/whole.png");
s.execute("file.export.saveForWebLegacy", json!({"format": "png24", "path": whole, "numbers": [1]})).unwrap();
assert_eq!(decode(&whole).dimensions(), (64, 48));
}