mirror of
https://github.com/storytold/photocraft.git
synced 2026-11-21 01:18:43 +00:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
93831e9715
commit
8a1e58ee41
3 files changed
+66
-5
No files matched your search
@@ -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"="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
|
||||
),
|
||||
|
||||
@@ -174,12 +174,22 @@ enum SlicePick {
|
||||
Numbers(Vec<u64>),
|
||||
}
|
||||
|
||||
/// 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<SlicePick> {
|
||||
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<u64> = numbers
|
||||
.as_array()
|
||||
|
||||
@@ -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::<Vec<_>>();
|
||||
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}");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user