mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
File Info: read and remove xmpRights:Marked only as an attribute inside a tag (#13821)
This commit is contained in:
1 parent
3353c5de09
commit
f876d52647
2 files changed
+77
-17
No files matched your search
@@ -105,19 +105,52 @@ fn rights_marked(xmp: &str) -> Option<&str> {
|
||||
}
|
||||
|
||||
/// 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. 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.
|
||||
/// and around the `=`, as XMP writers lay attributes out on their own lines. Only names inside a
|
||||
/// tag count, not text in another attribute's value, element text, a comment or CDATA. 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, _)| {
|
||||
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 v0 = xmp.len() - rest.len() + 1;
|
||||
let v1 = xmp[v0..].find(quote)? + v0;
|
||||
Some((start, v1 + 1, &xmp[v0..v1]))
|
||||
})
|
||||
// The tab title asks every frame, and packets can be megabytes: walk only when the name is there.
|
||||
if !xmp.contains("xmpRights:Marked") {
|
||||
return None;
|
||||
}
|
||||
let b = xmp.as_bytes();
|
||||
let (mut i, mut in_tag, mut quote) = (0, false, None);
|
||||
while let Some(&c) = b.get(i) {
|
||||
match (in_tag, quote) {
|
||||
(_, Some(q)) => quote = (c != q).then_some(q),
|
||||
(false, None) if c == b'<' => {
|
||||
let rest = &xmp[i..];
|
||||
let skip = [("<!--", "-->"), ("<![CDATA[", "]]>")].into_iter().find(|(open, _)| rest.starts_with(open));
|
||||
match skip {
|
||||
Some((_, close)) => i += rest.find(close).map_or(rest.len(), |e| e + close.len()) - 1,
|
||||
None => in_tag = true,
|
||||
}
|
||||
}
|
||||
(false, None) => {}
|
||||
(true, None) => match c {
|
||||
b'>' => in_tag = false,
|
||||
b'"' | b'\'' => quote = Some(c),
|
||||
_ if c.is_ascii_whitespace() => {
|
||||
if let Some((end, v)) = marked_value(xmp, i + 1) {
|
||||
return Some((i, end, v));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// `xmpRights:Marked = "value"` starting at byte `at`: (end after the closing quote, value).
|
||||
fn marked_value(xmp: &str, at: usize) -> Option<(usize, &str)> {
|
||||
let rest = xmp.get(at..)?.strip_prefix("xmpRights:Marked")?.trim_start().strip_prefix('=')?.trim_start();
|
||||
let quote = rest.chars().next().filter(|&c| c == '"' || c == '\'')?;
|
||||
let v0 = xmp.len() - rest.len() + 1;
|
||||
let v1 = xmp[v0..].find(quote)? + v0;
|
||||
Some((v1 + 1, &xmp[v0..v1]))
|
||||
}
|
||||
|
||||
/// Whether File Info's Copyright Status reads Copyrighted (`xmpRights:Marked` is `True`): the
|
||||
@@ -296,11 +329,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, "");
|
||||
}
|
||||
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() {
|
||||
// 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, "");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,3 +101,31 @@ fn changing_the_status_of_an_opened_document_clears_every_attribute_form() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_name_inside_other_text_is_neither_read_nor_removed() {
|
||||
const RDF: &str = r#"<x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">"#;
|
||||
const END: &str = "</rdf:RDF></x:xmpmeta>";
|
||||
let headline = r#"photoshop:Headline="Example xmpRights:Marked='True' text""#;
|
||||
let quoted = [
|
||||
format!("<rdf:Description rdf:about=\"\" {headline}/>"),
|
||||
"<rdf:Description rdf:about=\"\"><photoshop:Credit>a xmpRights:Marked=\"True\" b</photoshop:Credit></rdf:Description>".to_string(),
|
||||
"<rdf:Description rdf:about=\"\"><!-- <x xmpRights:Marked='True'/> --></rdf:Description>".to_string(),
|
||||
"<rdf:Description rdf:about=\"\"><![CDATA[<x xmpRights:Marked='True'/>]]></rdf:Description>".to_string(),
|
||||
];
|
||||
for desc in quoted {
|
||||
let mut app = app();
|
||||
std::sync::Arc::make_mut(&mut app.session.active_mut().unwrap().doc).metadata.xmp = Some(format!("{RDF}{desc}{END}").into());
|
||||
assert!(title(&app).starts_with("oracle @ "), "text is not the mark: {desc}");
|
||||
}
|
||||
// Beside a real attribute: the status change removes the attribute and keeps the headline.
|
||||
let mut app = app();
|
||||
let xmp = format!("{RDF}<rdf:Description rdf:about=\"\" {headline}\n xmpRights:Marked='True'/>{END}");
|
||||
std::sync::Arc::make_mut(&mut app.session.active_mut().unwrap().doc).metadata.xmp = Some(xmp.into());
|
||||
assert!(title(&app).starts_with("\u{a9} "));
|
||||
status(&mut app, "unknown");
|
||||
assert!(title(&app).starts_with("oracle @ "), "{}", title(&app));
|
||||
let packet = app.session.documents()[0].doc.metadata.xmp.as_deref().unwrap_or_default().to_string();
|
||||
assert!(packet.contains(headline), "the headline is unchanged: {packet}");
|
||||
assert_eq!(packet.matches("xmpRights:Marked").count(), 1, "only the headline's text is left: {packet}");
|
||||
}
|
||||
Reference in new issue
Block a user