Keep File Info attribute handling scoped to Camera Data

This commit is contained in:
yoriswind committed 2026-11-28 09:54:27 -08:00
1 parent e9307b2144
commit 05bdefdeb1
2 files changed
+26 -8

No files matched your search

+21 -5
View File
@@ -90,15 +90,29 @@ fn li_values(inner: &str) -> Vec<String> {
out
}
/// Legacy Basic attribute spelling; keep its read/write acceptance unchanged.
fn find_attr<'a>(xmp: &'a str, prop: &str) -> Option<(usize, usize, &'a str)> {
let key = format!(" {prop}=\"");
let i = xmp.find(&key)?;
let v0 = i + key.len();
let v1 = xmp[v0..].find('"')? + v0;
Some((i, v1 + 1, &xmp[v0..v1]))
}
/// The packet's `xmpRights:Marked` value, as an element or an attribute.
fn rights_marked(xmp: &str) -> Option<&str> {
find_element(xmp, "xmpRights:Marked").map(|(_, _, v)| v.trim()).or_else(|| find_attr(xmp, "xmpRights:Marked").map(|(_, _, v)| v))
find_element(xmp, "xmpRights:Marked").map(|(_, _, v)| v.trim()).or_else(|| marked_attr(xmp).map(|(_, _, v)| v))
}
// The existing Marked path retains its quote/whitespace and rewrite-span behavior.
fn marked_attr(xmp: &str) -> Option<(usize, usize, &str)> {
tag_attr(xmp, "xmpRights:Marked")
}
/// Attribute form `prop="…"` with either quote and XML whitespace before the name and around `=`.
/// Only names inside a tag count, not text in another attribute's value, element text, a comment
/// or CDATA. The span includes the whitespace before the name, so rewriting removes what is read.
fn find_attr<'a>(xmp: &'a str, prop: &str) -> Option<(usize, usize, &'a str)> {
fn tag_attr<'a>(xmp: &'a str, prop: &str) -> Option<(usize, usize, &'a str)> {
// The tab title asks every frame, and packets can be megabytes: walk only when the name is there.
if !xmp.contains(prop) {
return None;
@@ -257,7 +271,7 @@ fn date_text(s: &str) -> String {
fn xmp_value(x: &str, prop: &str) -> Option<String> {
let v = match find_element(x, prop) {
Some((_, _, inner)) => li_values(inner).into_iter().next(),
None => find_attr(x, prop).map(|(_, _, v)| xml_unescape(v)),
None => tag_attr(x, prop).map(|(_, _, v)| xml_unescape(v)),
}?;
let v = v.trim().to_string();
(!v.is_empty()).then_some(v)
@@ -564,8 +578,10 @@ pub fn write_file_info(xmp: Option<&str>, info: &Value) -> String {
while let Some((a, b, _)) = find_element(&x, prop) {
x.replace_range(a..b, "");
}
// Remove the same attribute forms the reader recognizes, and only from tags (#13821).
while let Some((a, b, _)) = find_attr(&x, prop) {
// Marked is removed in every attribute form it is read in, and only from tags (#13821).
loop {
let attr = if prop == "xmpRights:Marked" { marked_attr(&x) } else { find_attr(&x, prop) };
let Some((a, b, _)) = attr else { break };
x.replace_range(a..b, "");
}
}
@@ -433,7 +433,7 @@ fn no_op_basic_confirmation_preserves_dates_and_matches_the_current_command_hist
}
#[test]
fn xmp_attribute_forms_supply_camera_values_and_are_removed_when_basic_fields_change() {
fn xmp_attribute_forms_supply_camera_values_without_changing_basic_behavior() {
// Our own synthetic XML: ordinary double quotes, single quotes, and XML whitespace variants.
for (before, around, quote) in [(" ", "", '"'), (" ", "", '\''), ("\t", " \t ", '"'), ("\n", "\r\n", '\''), ("\r", " ", '"')] {
let other = if quote == '"' { '\'' } else { '"' };
@@ -453,7 +453,9 @@ fn xmp_attribute_forms_supply_camera_values_and_are_removed_when_basic_fields_ch
let read = session.execute("file.fileInfo", json!({})).unwrap();
assert_eq!(read["camera"]["owner"], "Ada & Co", "{before:?} {around:?} {quote}");
assert_eq!(read["camera"]["make"], "Fixture make");
assert_eq!(read["authorTitle"], "Old role");
// Basic retains its pre-PR literal double-quoted attribute acceptance.
let legacy_role = if before == " " && around.is_empty() && quote == '"' { "Old role" } else { "" };
assert_eq!(read["authorTitle"], legacy_role);
assert_eq!(read["copyrightStatus"], "copyrighted", "existing Marked behavior");
assert_eq!(session.active().unwrap().doc.metadata.xmp, original, "reading must not edit metadata");
@@ -462,7 +464,7 @@ fn xmp_attribute_forms_supply_camera_values_and_are_removed_when_basic_fields_ch
assert_eq!(changed["camera"], read["camera"], "a Basic edit keeps Camera Data");
assert_eq!(changed["copyrightStatus"], "copyrighted");
let xmp = session.active().unwrap().doc.metadata.xmp.as_deref().unwrap();
assert!(!xmp.contains("Old role"), "the old attribute span must be removed, not merely shadowed");
assert_eq!(xmp.contains("Old role"), legacy_role.is_empty(), "Basic keeps its existing attribute-removal behavior");
assert!(xmp.contains(&note), "unrelated quoted attribute content survives");
assert!(xmp.contains("fixture:opaque=\"preserve-me\""));
assert!(xmp.contains(&format!("<!-- aux:OwnerName={quote}comment-decoy{quote} -->")));