File Info: changing the status removes every xmpRights:Marked attribute form it reads (#13821)

This commit is contained in:
RickiNano committed 2026-11-13 23:10:57 +01:00
1 parent ddb4750bec
commit 3353c5de09
2 files changed
+34 -8

No files matched your search

+13 -8
View File
@@ -101,21 +101,22 @@ fn find_attr<'a>(xmp: &'a str, prop: &str) -> Option<(usize, usize, &'a str)> {
/// 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(|| marked_attr(xmp))
find_element(xmp, "xmpRights:Marked").map(|(_, _, v)| v.trim()).or_else(|| marked_attr(xmp).map(|(_, _, v)| v))
}
/// Attribute form `xmpRights:Marked="…"` with either quote, any XML whitespace before the name
/// and around the `=`, as XMP writers lay attributes out on their own lines.
fn marked_attr(xmp: &str) -> Option<&str> {
/// and around the `=`, as XMP writers lay attributes out on their own lines. Returns (start, end,
/// value) with the whitespace before the name in the span, as [`find_attr`] does, so a rewrite
/// removes every form it reads.
fn marked_attr(xmp: &str) -> Option<(usize, usize, &str)> {
const NAME: &str = "xmpRights:Marked";
xmp.match_indices(NAME).find_map(|(i, _)| {
if !xmp[..i].ends_with(|c: char| c.is_ascii_whitespace()) {
return None;
}
let start = i.checked_sub(1).filter(|&s| xmp.as_bytes().get(s).is_some_and(u8::is_ascii_whitespace))?;
let rest = xmp[i + NAME.len()..].trim_start().strip_prefix('=')?.trim_start();
let quote = rest.chars().next().filter(|&c| c == '"' || c == '\'')?;
let body = &rest[quote.len_utf8()..];
body.find(quote).map(|end| &body[..end])
let v0 = xmp.len() - rest.len() + 1;
let v1 = xmp[v0..].find(quote)? + v0;
Some((start, v1 + 1, &xmp[v0..v1]))
})
}
@@ -298,6 +299,10 @@ pub fn write_file_info(xmp: Option<&str>, info: &Value) -> String {
while let Some((a, b, _)) = find_attr(&x, prop) {
x.replace_range(a..b, "");
}
// Marked is also read in single quotes or after a tab or newline (#13821).
while let Some((a, b, _)) = (prop == "xmpRights:Marked").then(|| marked_attr(&x)).flatten() {
x.replace_range(a..b, "");
}
}
let get = |k: &str| info.get(k).filter(|_| changed(k)).cloned().unwrap_or(Value::Null);
let mut body = String::new();
@@ -80,3 +80,24 @@ fn a_file_opened_with_the_mark_already_set_shows_the_prefix() {
assert_eq!(title(&app), want, "{xmp}");
}
}
#[test]
fn changing_the_status_of_an_opened_document_clears_every_attribute_form() {
for attr in ["\n xmpRights:Marked='True'", "\txmpRights:Marked = \"True\"", " xmpRights:Marked=\"True\""] {
let xmp = format!(
r#"<x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><rdf:Description rdf:about="" xmlns:xmpRights="http://ns.adobe.com/xap/1.0/rights/"{attr}/></rdf:RDF></x:xmpmeta>"#
);
for steps in [&["unknown"][..], &["publicDomain", "unknown"]] {
let mut app = app();
std::sync::Arc::make_mut(&mut app.session.active_mut().unwrap().doc).metadata.xmp = Some(xmp.clone().into());
assert!(title(&app).starts_with("\u{a9} "), "{attr:?}");
for &s in steps {
status(&mut app, s);
}
assert!(title(&app).starts_with("oracle @ "), "{attr:?} then {steps:?}: {}", title(&app));
let packet = app.session.documents()[0].doc.metadata.xmp.as_deref().unwrap_or_default().to_string();
assert!(!packet.contains("xmpRights:Marked"), "{attr:?} then {steps:?} leaves {packet}");
assert!(packet.contains("rdf:about=\"\""), "the rest of the description stays: {packet}");
}
}
}