Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

KEYC-METADATA: keyword.setShortcut refuses parameters that are not an object, and Shift+K checks its targets first

dotCooCoo902987e3acba
diff --git a/crates/engine/src/cmd/keywords.rs b/crates/engine/src/cmd/keywords.rs
index b77bba8de..5f45b3978 100644
--- a/crates/engine/src/cmd/keywords.rs
+++ b/crates/engine/src/cmd/keywords.rs
@@ -144,8 +144,12 @@ pub fn slots(typed: &[String]) -> Vec<String> {
/// cleaned, a new name goes inside the default parent as the Keywording box puts it, and each is
/// kept once whatever its case; empty ones are dropped. The text Set Keyword Shortcut… shows (the
/// keyword shortcut's keywords joined with ", ") keeps them as they are, commas in a name included.
-/// Any other value is an error, and the keyword shortcut is left as it was.
+/// Any other value, or parameters that are not an object, is an error, and the keyword shortcut is
+/// left as it was.
fn shortcut_keywords(s: &Session, p: &Value) -> Result<Vec<String>> {
+ if !p.is_object() {
+ return Err(bad("keyword.setShortcut", "parameters must be an object"));
+ }
let now = s.catalog.keyword_shortcut();
let invalid = || bad("keyword.setShortcut", "`keywords` must be a string or an array of strings");
let typed: Vec<String> = match p.get("keywords") {
@@ -361,11 +365,11 @@ pub fn specs() -> Vec<CommandSpec> {
super::has_selection,
|s, p| {
let keywords = s.catalog.keyword_shortcut();
+ let ids = s.targets(p)?;
// with no keyword shortcut set, ⇧K returns without changing any photo
if keywords.is_empty() {
return Ok(json!({"changed": 0, "keywords": keywords}));
}
- let ids = s.targets(p)?;
let has = |ph: &lightcraft_catalog::Photo, k: &str| {
ph.meta.keywords.iter().any(|x| lightcraft_catalog::keywords::same(&clean(x), &clean(k)))
};
diff --git a/crates/engine/src/tests_keyword_list.rs b/crates/engine/src/tests_keyword_list.rs
index b2b2414e4..e50d0d7b1 100644
--- a/crates/engine/src/tests_keyword_list.rs
+++ b/crates/engine/src/tests_keyword_list.rs
@@ -240,6 +240,27 @@ fn the_keyword_shortcut_refuses_keywords_that_are_not_text() {
assert!(e.contains("`keywords` must be a string or an array of strings"), "{keywords}: {e}");
assert_eq!(s.execute("keyword.shortcut", &json!({})).unwrap()["keywords"], json!(["beach", "sunset"]), "{keywords}");
}
+ // parameters that are not an object are an error too, not a request to clear it
+ for params in [json!(7), json!("beach"), json!(["beach"])] {
+ let e = s.execute("keyword.setShortcut", &params).unwrap_err().to_string();
+ assert!(e.contains("parameters must be an object"), "{params}: {e}");
+ assert_eq!(s.execute("keyword.shortcut", &json!({})).unwrap()["keywords"], json!(["beach", "sunset"]), "{params}");
+ }
+}
+
+/// ⇧K with an `ids` or `id` that is not photo ids is an error, with or without a keyword shortcut set.
+#[test]
+fn the_keyword_shortcut_refuses_targets_that_are_not_photo_ids() {
+ let mut s = Session::with_demo();
+ let id = s.visible_cloned()[0].0;
+ s.execute("library.select", &json!({"ids": [id]})).unwrap();
+ for shortcut in [json!([]), json!(["beach"])] {
+ s.execute("keyword.setShortcut", &json!({"keywords": shortcut})).unwrap();
+ for params in [json!({"ids": "x"}), json!({"ids": ["x"]}), json!({"id": "x"})] {
+ let e = s.execute("keyword.toggleShortcut", &params).unwrap_err().to_string();
+ assert!(e.contains("must be photo ids"), "{shortcut} {params}: {e}");
+ }
+ }
}
/// The keyword shortcut follows its keywords when they are moved, renamed or merged, and loses one