mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge pull request #11173 from hnoerenberg/fix/xmp-rdf-end-tag-flex-11116
fix(xmp): File Info keeps packets with a spaced or re-prefixed RDF end tag (#11116)
This commit is contained in:
2 files changed
+432
-4
No files matched your search
@@ -312,14 +312,224 @@ pub fn camera_data(exif: Option<&[u8]>, xmp: Option<&str>) -> Value {
|
||||
Value::Object(m)
|
||||
}
|
||||
|
||||
/// The RDF namespace every XMP packet's core element belongs to.
|
||||
const RDF_NS: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
|
||||
|
||||
/// The byte range of the packet's RDF end tag, or `None` when it has none. XML allows
|
||||
/// whitespace around a declaration's `=` and before a closing tag's `>` (REC-xml 2.4), and RDF
|
||||
/// allows any prefix — or none — bound for its namespace (RDF XML Syntax 5.1), so `</rdf:RDF >`,
|
||||
/// `</r:RDF>` and `</RDF>` match just like `</rdf:RDF>`. One forward pass keeps a stack of the
|
||||
/// open elements' namespace declarations and resolves each closing tag's prefix in the scope of
|
||||
/// the element it closes, so a prefix rebound to another namespace (on a sibling or a nested
|
||||
/// element) never makes a foreign `</r:RDF>` the end tag (Namespaces in XML 1.0, section 6). The
|
||||
/// pass skips `<!-- -->` comments, `<![CDATA[ ]]>` sections, processing instructions and
|
||||
/// declarations, so nothing inside them is read as markup, and it reads declarations only inside
|
||||
/// start tags and outside quoted attribute values (#11116). The last RDF end tag wins.
|
||||
fn rdf_end_tag(x: &str) -> Option<(usize, usize)> {
|
||||
rdf_end_scan(x).map(|(a, b, _)| (a, b))
|
||||
}
|
||||
|
||||
/// [`rdf_end_tag`]'s range plus whether the `rdf` prefix is bound to the RDF namespace in the scope
|
||||
/// of the element that range closes, which is the scope a description inserted at its start sits
|
||||
/// in. The scope is the stack of the elements open at that point, so an earlier `rdf:RDF` that has
|
||||
/// already closed (or closed on the spot) contributes nothing, and a rebinding of `rdf` to another
|
||||
/// namespace on that element or an ancestor counts as unbound (#11116).
|
||||
fn rdf_end_scan(x: &str) -> Option<(usize, usize, bool)> {
|
||||
let b = x.as_bytes();
|
||||
let is_ws = |c: u8| matches!(c, b' ' | b'\t' | b'\r' | b'\n');
|
||||
// One entry per open element: the `(prefix, binds the RDF namespace)` pairs its start tag
|
||||
// declares; the default namespace is the empty prefix.
|
||||
let mut scopes: Vec<Vec<(String, bool)>> = Vec::new();
|
||||
// The innermost open element that declares the prefix decides (Namespaces in XML 1.0,
|
||||
// section 6): search the scopes from the innermost outwards, never the flattened list backwards.
|
||||
let resolves_to_rdf = |scopes: &[Vec<(String, bool)>], name: &str| {
|
||||
let (prefix, local) = name.split_once(':').unwrap_or(("", name));
|
||||
local == "RDF" && scopes.iter().rev().flat_map(|scope| scope.iter().rev()).find(|(p, _)| p == prefix).is_some_and(|(_, rdf)| *rdf)
|
||||
};
|
||||
let mut found: Option<(usize, usize, bool)> = None;
|
||||
let mut i = 0usize;
|
||||
while i < b.len() {
|
||||
if b[i] != b'<' {
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
// Skip a comment, CDATA section, processing instruction or declaration in one step: its
|
||||
// contents are character data, never a declaration or a real end tag, and a hidden or
|
||||
// fake `</rdf:RDF>` inside one must not hide the true end tag or trick the writer into
|
||||
// inserting there (#11116).
|
||||
if b[i..].starts_with(b"<!--") {
|
||||
i = x[i + 4..].find("-->").map_or(b.len(), |e| i + 7 + e);
|
||||
continue;
|
||||
}
|
||||
if b[i..].starts_with(b"<![CDATA[") {
|
||||
i = x[i + 9..].find("]]>").map_or(b.len(), |e| i + 12 + e);
|
||||
continue;
|
||||
}
|
||||
if b[i..].starts_with(b"<?") {
|
||||
i = x[i + 2..].find("?>").map_or(b.len(), |e| i + 4 + e);
|
||||
continue;
|
||||
}
|
||||
if b[i..].starts_with(b"<!") {
|
||||
i = x[i + 2..].find('>').map_or(b.len(), |e| i + 3 + e);
|
||||
continue;
|
||||
}
|
||||
if b[i..].starts_with(b"</") {
|
||||
let name_start = i + 2;
|
||||
let name_end = x[name_start..].find(|c: char| c.is_ascii() && (is_ws(c as u8) || c == '>')).map_or(b.len(), |e| name_start + e);
|
||||
let tail = x[name_end..].trim_start_matches([' ', '\t', '\r', '\n']);
|
||||
if tail.starts_with('>') {
|
||||
let end = x.len() - tail.len() + 1;
|
||||
if resolves_to_rdf(&scopes, &x[name_start..name_end]) {
|
||||
found = Some((i, end, resolves_to_rdf(&scopes, "rdf:RDF")));
|
||||
}
|
||||
scopes.pop();
|
||||
i = end;
|
||||
} else {
|
||||
i = name_end.max(i + 2);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
// A start tag: the element name, then attributes up to the closing `>`.
|
||||
let name_start = i + 1;
|
||||
let name_end = x[name_start..].find(|c: char| c.is_ascii() && (is_ws(c as u8) || c == '/' || c == '>')).map_or(b.len(), |e| name_start + e);
|
||||
if name_end == name_start {
|
||||
// A lone `<` (not followed by a name) opens nothing.
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
let mut decls: Vec<(String, bool)> = Vec::new();
|
||||
let mut quote: Option<u8> = None;
|
||||
let mut self_closed = false;
|
||||
let mut closed = false;
|
||||
let mut j = name_end;
|
||||
while let Some(&c) = b.get(j) {
|
||||
match quote {
|
||||
Some(q) => {
|
||||
if c == q {
|
||||
quote = None;
|
||||
}
|
||||
j += 1;
|
||||
}
|
||||
None if c == b'"' || c == b'\'' => {
|
||||
quote = Some(c);
|
||||
j += 1;
|
||||
}
|
||||
None if c == b'>' => {
|
||||
self_closed = b.get(j - 1) == Some(&b'/');
|
||||
closed = true;
|
||||
j += 1;
|
||||
break;
|
||||
}
|
||||
// A declaration starts an attribute: after whitespace, never inside a longer name.
|
||||
None if b[j..].starts_with(b"xmlns") && b.get(j - 1).is_some_and(|&p| is_ws(p)) => match parse_xmlns(x, j) {
|
||||
Some((prefix, rdf, next)) => {
|
||||
decls.push((prefix, rdf));
|
||||
j = next;
|
||||
}
|
||||
None => j += 1,
|
||||
},
|
||||
None => j += 1,
|
||||
}
|
||||
}
|
||||
if !closed {
|
||||
// A truncated start tag: nothing after it is markup.
|
||||
break;
|
||||
}
|
||||
if !self_closed {
|
||||
scopes.push(decls);
|
||||
}
|
||||
i = j;
|
||||
}
|
||||
found
|
||||
}
|
||||
/// Parse the `xmlns[:prefix] = "uri"` namespace declaration starting at `i` in `x` (`x[i..]` is
|
||||
/// known to begin with `xmlns`, inside a start tag). Returns the declared prefix (empty for the
|
||||
/// default namespace), whether the URI is the RDF namespace, and the byte index just past the
|
||||
/// declaration, or `None` when the text is no declaration. Whitespace around `=` and either quote style are accepted (REC-xml 3.1,
|
||||
/// 2.3); a prefix may contain any XML Name character, Unicode letters included (Namespaces in
|
||||
/// XML 1.0, NCName); and a URI written with character references binds what its decoded form
|
||||
/// names (REC-xml 4.1 attribute-value normalization).
|
||||
fn parse_xmlns(x: &str, i: usize) -> Option<(String, bool, usize)> {
|
||||
let b = x.as_bytes();
|
||||
let ws = |j: &mut usize| {
|
||||
while matches!(b.get(*j), Some(b' ') | Some(b'\t') | Some(b'\r') | Some(b'\n')) {
|
||||
*j += 1
|
||||
}
|
||||
};
|
||||
let mut j = i + "xmlns".len();
|
||||
let mut prefix = String::new();
|
||||
if b.get(j) == Some(&b':') {
|
||||
j += 1;
|
||||
let s = j;
|
||||
// ASCII name characters, and any non-ASCII byte: only UTF-8 continuation and lead bytes
|
||||
// are ≥ 0x80, and consuming every byte of a sequence keeps `j` on a char boundary.
|
||||
while matches!(b.get(j), Some(c) if c.is_ascii_alphanumeric() || matches!(c, b'.' | b'-' | b'_') || *c >= 0x80) {
|
||||
j += 1;
|
||||
}
|
||||
if j == s {
|
||||
return None;
|
||||
}
|
||||
prefix.push_str(&x[s..j]);
|
||||
}
|
||||
ws(&mut j);
|
||||
if b.get(j) != Some(&b'=') {
|
||||
return None;
|
||||
}
|
||||
j += 1;
|
||||
ws(&mut j);
|
||||
let quote = *b.get(j)?;
|
||||
if quote != b'"' && quote != b'\'' {
|
||||
return None;
|
||||
}
|
||||
j += 1;
|
||||
let s = j;
|
||||
while b.get(j).is_some_and(|&c| c != quote) {
|
||||
j += 1;
|
||||
}
|
||||
if b.get(j) != Some("e) {
|
||||
return None;
|
||||
}
|
||||
let uri = &x[s..j];
|
||||
j += 1;
|
||||
Some((prefix, decode_char_refs(uri).as_deref() == Some(RDF_NS), j))
|
||||
}
|
||||
|
||||
/// The value of an attribute after replacing its numeric character references (`#`, `#`)
|
||||
/// — what an XML reader binds, which the raw text may spell differently. `None` when a reference
|
||||
/// is malformed or names no character, which no valid declaration contains.
|
||||
fn decode_char_refs(s: &str) -> Option<String> {
|
||||
if !s.contains("&#") {
|
||||
return Some(s.to_string());
|
||||
}
|
||||
let mut out = String::with_capacity(s.len());
|
||||
let mut rest = s;
|
||||
while let Some(at) = rest.find("&#") {
|
||||
out.push_str(&rest[..at]);
|
||||
let after = &rest[at + 2..];
|
||||
let (digits, base) = match after.strip_prefix('x').or_else(|| after.strip_prefix('X')) {
|
||||
Some(hex) => (hex, 16),
|
||||
None => (after, 10),
|
||||
};
|
||||
let semi = digits.find(';')?;
|
||||
let (body, tail) = digits.split_at(semi);
|
||||
let c = char::from_u32(u32::from_str_radix(body, base).ok()?)?;
|
||||
out.push(c);
|
||||
rest = &tail[1..];
|
||||
}
|
||||
out.push_str(rest);
|
||||
Some(out)
|
||||
}
|
||||
|
||||
/// Rewrites only changed File Info fields of an XMP packet (creating one when there is none),
|
||||
/// keeping the other properties verbatim. Edited properties move into their own
|
||||
/// `rdf:Description` (several descriptions per packet are valid XMP).
|
||||
/// `rdf:Description` (several descriptions per packet are valid XMP). A packet whose RDF element
|
||||
/// ends in a valid but non-canonical spelling is kept too: the other properties survive a field
|
||||
/// edit whatever prefix or spacing the producer used (#11116).
|
||||
pub fn write_file_info(xmp: Option<&str>, info: &Value) -> String {
|
||||
let cur = read_file_info(xmp);
|
||||
// The dialog submits all fields, including unchanged language alternatives.
|
||||
let changed = |key: &str| info.get(key).is_some_and(|v| cur.get(key) != Some(v));
|
||||
let mut x = xmp.filter(|s| s.contains("</rdf:RDF>")).map(str::to_string).unwrap_or_else(|| {
|
||||
let mut x = xmp.filter(|s| rdf_end_tag(s).is_some()).map(str::to_string).unwrap_or_else(|| {
|
||||
"<?xpacket begin=\"\u{feff}\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n<x:xmpmeta xmlns:x=\"adobe:ns:meta/\">\n <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n </rdf:RDF>\n</x:xmpmeta>\n<?xpacket end=\"w\"?>".to_string()
|
||||
});
|
||||
for (key, prop, _) in INFO_FIELDS.into_iter().chain([("copyrightStatus", "xmpRights:Marked", ""), ("copyrightUrl", "xmpRights:WebStatement", "")]) {
|
||||
@@ -369,10 +579,13 @@ pub fn write_file_info(xmp: Option<&str>, info: &Value) -> String {
|
||||
body.push_str(&format!(" <xmpRights:WebStatement>{}</xmpRights:WebStatement>\n", xml_escape(u)));
|
||||
}
|
||||
if !body.is_empty() {
|
||||
let (at, bound) = rdf_end_scan(&x).map_or((x.len(), false), |(a, _, bound)| (a, bound));
|
||||
// The inserted description uses the `rdf:` prefix, so it must bind that prefix itself
|
||||
// unless the elements open at the insertion point already bind it (#11116).
|
||||
let bind = if bound { String::new() } else { format!(" xmlns:rdf=\"{RDF_NS}\"") };
|
||||
let desc = format!(
|
||||
" <rdf:Description rdf:about=\"\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:photoshop=\"http://ns.adobe.com/photoshop/1.0/\" xmlns:xmpRights=\"http://ns.adobe.com/xap/1.0/rights/\">\n{body} </rdf:Description>\n "
|
||||
" <rdf:Description rdf:about=\"\"{bind} xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:photoshop=\"http://ns.adobe.com/photoshop/1.0/\" xmlns:xmpRights=\"http://ns.adobe.com/xap/1.0/rights/\">\n{body} </rdf:Description>\n "
|
||||
);
|
||||
let at = x.rfind("</rdf:RDF>").unwrap_or(x.len());
|
||||
x.insert_str(at, &desc);
|
||||
}
|
||||
x
|
||||
|
||||
@@ -2153,6 +2153,221 @@ fn file_info_round_trips_through_xmp_and_psd() {
|
||||
assert_eq!(read_file_info(Some(&out))["authorTitle"], "Artist");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn file_info_keeps_a_packet_whose_rdf_end_tag_is_spaced_or_prefixed() {
|
||||
// #11116: only the literal `</rdf:RDF>` retained the imported packet, so the valid
|
||||
// spellings below — XML allows whitespace before the `>`, RDF allows any declared
|
||||
// prefix — were replaced by a blank packet and unrelated properties were lost.
|
||||
const NS: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
|
||||
let cases = [
|
||||
format!(
|
||||
r#"<x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="{NS}"><rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmp:CreatorTool="Other App"/></rdf:RDF ></x:xmpmeta>"#
|
||||
),
|
||||
format!(
|
||||
r#"<x:xmpmeta xmlns:x="adobe:ns:meta/"><r:RDF xmlns:r="{NS}"><r:Description r:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmp:CreatorTool="Other App"/></r:RDF></x:xmpmeta>"#
|
||||
),
|
||||
format!(
|
||||
r#"<x:xmpmeta xmlns:x="adobe:ns:meta/"><RDF xmlns="{NS}"><Description about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmp:CreatorTool="Other App"/></RDF></x:xmpmeta>"#
|
||||
),
|
||||
// Whitespace around the declaration's `=` (REC-xml 3.1 allows it) still binds the prefix.
|
||||
format!(
|
||||
r#"<x:xmpmeta xmlns:x="adobe:ns:meta/"><r:RDF xmlns:r = "{NS}"><r:Description r:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmp:CreatorTool="Other App"/></r:RDF></x:xmpmeta>"#
|
||||
),
|
||||
];
|
||||
for packet in cases {
|
||||
let out = write_file_info(Some(&packet), &json!({"authorTitle": "Artist"}));
|
||||
assert!(out.contains("xmp:CreatorTool=\"Other App\""), "the unrelated property survives: {out}");
|
||||
assert_eq!(read_file_info(Some(&out))["authorTitle"], "Artist", "the edit landed: {out}");
|
||||
// Whatever the packet binds, the inserted description binds every prefix it uses.
|
||||
let desc = out[out.find("<rdf:Description rdf:about=\"\"").unwrap()..].lines().next().unwrap().to_string();
|
||||
for p in ["rdf", "dc", "photoshop", "xmpRights"] {
|
||||
assert!(desc.contains(&format!("xmlns:{p}=\"")) || packet.contains(&format!("xmlns:{p}=\"")), "prefix {p} unbound: {desc}");
|
||||
}
|
||||
}
|
||||
// A `</rdf:RDF>` that only appears inside a comment must not be taken as the packet's end
|
||||
// tag: the edit inserts before the real one, keeping it inside the RDF element (#11116).
|
||||
let commented = format!(
|
||||
r#"<x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="{NS}"><rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmp:CreatorTool="Other App"/></rdf:RDF><!-- </rdf:RDF> --></x:xmpmeta>"#
|
||||
);
|
||||
let out = write_file_info(Some(&commented), &json!({"authorTitle": "Artist"}));
|
||||
assert!(out.find("AuthorsPosition").unwrap() < out.find("<!--").unwrap(), "the edit is not inside the comment: {out}");
|
||||
assert_eq!(read_file_info(Some(&out))["authorTitle"], "Artist");
|
||||
// A `</rdf:RDF>` inside a CDATA section neither hides the real end tag (which would replace
|
||||
// the packet) nor tricks the writer into inserting inside the section (#11116).
|
||||
let cdata = format!(
|
||||
r#"<x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="{NS}"><rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmp:CreatorTool="Other App"/><rdf:li><![CDATA[ </rdf:RDF> fake ]]></rdf:li></rdf:RDF></x:xmpmeta>"#
|
||||
);
|
||||
let out = write_file_info(Some(&cdata), &json!({"authorTitle": "Artist"}));
|
||||
assert!(out.contains("xmp:CreatorTool=\"Other App\""), "the CDATA packet keeps its properties: {out}");
|
||||
assert!(out.find("AuthorsPosition").unwrap() < out.rfind("</rdf:RDF>").unwrap(), "the edit is before the real end tag: {out}");
|
||||
assert_eq!(read_file_info(Some(&out))["authorTitle"], "Artist");
|
||||
// A foreign element whose name merely starts like the RDF one (`<rdf:RDFjunk>`) is not the
|
||||
// RDF start tag, and its `</rdf:RDFjunk>` is not the end tag (#11116).
|
||||
let lookalike = format!(
|
||||
r#"<x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="{NS}"><rdf:RDFjunk xmlns:rdf="http://example.com/other"><r:Thing/></rdf:RDFjunk></rdf:RDF></x:xmpmeta>"#
|
||||
);
|
||||
let out = write_file_info(Some(&lookalike), &json!({"authorTitle": "Artist"}));
|
||||
assert_eq!(read_file_info(Some(&out))["authorTitle"], "Artist", "{out}");
|
||||
assert!(out.rfind("<rdf:Description rdf:about=\"\"").unwrap() < out.rfind("</rdf:RDF>").unwrap(), "the edit sits inside the RDF element: {out}");
|
||||
// A packet with no RDF end tag at all (self-closing) still gets the canonical packet.
|
||||
let selfclosed = format!(r#"<x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="{NS}"/></x:xmpmeta>"#);
|
||||
let out = write_file_info(Some(&selfclosed), &json!({"authorTitle": "Artist"}));
|
||||
assert!(out.contains("<?xpacket begin="), "the canonical packet: {out}");
|
||||
assert_eq!(read_file_info(Some(&out))["authorTitle"], "Artist");
|
||||
}
|
||||
|
||||
/// Review follow-ups on the RDF end-tag finder (#11116): an XML Name prefix is not ASCII-only;
|
||||
/// character references decode before a namespace URI is compared; an `xmlns` mention inside
|
||||
/// character data binds nothing; and only the start tag of the element that is still open
|
||||
/// reaches the insertion point.
|
||||
#[test]
|
||||
fn file_info_reads_declarations_the_way_an_xml_reader_does() {
|
||||
const NS: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
|
||||
// A Unicode prefix (NCName covers XML letters, not just ASCII): the packet is kept.
|
||||
let unicode_prefix = format!(
|
||||
r#"<x:xmpmeta xmlns:x="adobe:ns:meta/"><ü:RDF xmlns:ü="{NS}"><ü:Description about="" xmlns:rdf="{NS}" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmp:CreatorTool="Other App"/></ü:RDF></x:xmpmeta>"#
|
||||
);
|
||||
let out = write_file_info(Some(&unicode_prefix), &json!({"authorTitle": "Artist"}));
|
||||
assert!(out.contains("xmp:CreatorTool=\"Other App\""), "the Unicode-prefixed packet keeps its properties: {out}");
|
||||
assert_eq!(read_file_info(Some(&out))["authorTitle"], "Artist");
|
||||
// `#` decodes to `#`: the declaration binds the RDF namespace as far as XML is concerned.
|
||||
let encoded = 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:xmp="http://ns.adobe.com/xap/1.0/" xmp:CreatorTool="Other App"/></rdf:RDF></x:xmpmeta>"#.to_string();
|
||||
let out = write_file_info(Some(&encoded), &json!({"authorTitle": "Artist"}));
|
||||
assert!(!out.contains("<?xpacket begin="), "the packet is kept, not rebuilt: {out}");
|
||||
assert!(out.contains("xmp:CreatorTool=\"Other App\""), "{out}");
|
||||
assert_eq!(read_file_info(Some(&out))["authorTitle"], "Artist");
|
||||
// An `xmlns` mention in the document's own text — after the real end tag — binds nothing:
|
||||
// a fake `</z:RDF>` written there must not capture the insertion.
|
||||
let textual = format!(
|
||||
r#"<x:xmpmeta xmlns:x="adobe:ns:meta/"><r:RDF xmlns:r="{NS}"><r:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmp:CreatorTool="Other App"/></r:RDF>note: xmlns:z="{NS}" then </z:RDF> as words</x:xmpmeta>"#
|
||||
);
|
||||
let out = write_file_info(Some(&textual), &json!({"authorTitle": "Artist"}));
|
||||
let edit = out.find("AuthorsPosition").unwrap();
|
||||
assert!(edit < out.find("</r:RDF>").unwrap(), "the edit inserts inside the RDF element: {out}");
|
||||
assert!(out.find("</r:RDF>").unwrap() < out.find("</z:RDF>").unwrap(), "the text mention stays where it was written: {out}");
|
||||
assert_eq!(read_file_info(Some(&out))["authorTitle"], "Artist");
|
||||
// Declarations written inside a processing instruction or a quoted attribute value are text,
|
||||
// and bind nothing: a later `</q:RDF>` must not capture the insertion.
|
||||
let hidden = format!(
|
||||
r#"<?xml-stylesheet xmlns:q="{NS}"?><x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="xmlns:q='{NS}'"><r:RDF xmlns:r="{NS}"><r:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmp:CreatorTool="Other App"/></r:RDF></x:xmpmeta>trailing words</q:RDF>"#
|
||||
);
|
||||
let out = write_file_info(Some(&hidden), &json!({"authorTitle": "Artist"}));
|
||||
assert!(out.contains("xmp:CreatorTool=\"Other App\""), "the packet is kept: {out}");
|
||||
assert!(out.find("AuthorsPosition").unwrap() < out.find("</r:RDF>").unwrap(), "the edit inserts inside the RDF element: {out}");
|
||||
assert_eq!(read_file_info(Some(&out))["authorTitle"], "Artist");
|
||||
// A self-closed `<rdf:RDF>` earlier in the packet does not reach the insertion point in a
|
||||
// later RDF-spelled element: the inserted description re-declares its `rdf` prefix.
|
||||
let re_opened = format!(
|
||||
r#"<x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="{NS}"/><q:RDF xmlns:q="{NS}"><q:Description about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmp:CreatorTool="Other App"/></q:RDF></x:xmpmeta>"#
|
||||
);
|
||||
let out = write_file_info(Some(&re_opened), &json!({"authorTitle": "Artist"}));
|
||||
assert!(out.contains("xmp:CreatorTool=\"Other App\""), "the second RDF-spelled element is kept: {out}");
|
||||
let desc = out[out.rfind("<rdf:Description rdf:about=\"\"").unwrap()..].lines().next().unwrap().to_string();
|
||||
assert!(desc.contains(&format!("xmlns:rdf=\"{NS}\"")), "the inserted description binds rdf itself: {desc}");
|
||||
assert_eq!(read_file_info(Some(&out))["authorTitle"], "Artist");
|
||||
}
|
||||
|
||||
/// The `rdf` prefix of the inserted description is resolved in the scope of the element it lands
|
||||
/// in, not taken from an earlier `rdf:RDF` that already closed (#11116). Each output is parsed
|
||||
/// as namespace-aware XML: a description whose `rdf` prefix is unbound reads as a plain
|
||||
/// `Description`, so two `rdf:Description` elements prove the prefix resolved.
|
||||
#[test]
|
||||
fn file_info_binds_rdf_for_the_description_from_the_open_element_scope() {
|
||||
const NS: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
|
||||
let rdf_descriptions = |out: &str| {
|
||||
let namespaces = [("rdf", NS), ("xmp", "http://ns.adobe.com/xap/1.0/"), ("photoshop", "http://ns.adobe.com/photoshop/1.0/"), ("x", "adobe:ns:meta/")];
|
||||
let canonical = craft_xml::Canonical::new(&namespaces);
|
||||
let doc = craft_xml::Options::canonical(&canonical).parse(out.as_bytes()).expect("the output is well-formed XML");
|
||||
let names: Vec<String> = doc.descendants().filter(|e| e.name.ends_with("Description")).map(|e| e.name.clone()).collect();
|
||||
names
|
||||
};
|
||||
// An ordinary closed `rdf:RDF` sibling comes first; the edit lands in the later `q:RDF`, which
|
||||
// does not bind `rdf` at all.
|
||||
let closed_sibling = format!(
|
||||
r#"<x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="{NS}"></rdf:RDF><q:RDF xmlns:q="{NS}"><q:Description q:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmp:CreatorTool="Other App"/></q:RDF></x:xmpmeta>"#
|
||||
);
|
||||
let out = write_file_info(Some(&closed_sibling), &json!({"authorTitle": "Artist"}));
|
||||
assert!(out.contains("xmp:CreatorTool=\"Other App\""), "the later RDF element is kept: {out}");
|
||||
assert_eq!(rdf_descriptions(&out), ["rdf:Description", "rdf:Description"], "the inserted description binds rdf: {out}");
|
||||
assert_eq!(read_file_info(Some(&out))["authorTitle"], "Artist");
|
||||
// The element the edit lands in binds `rdf` to a foreign namespace: re-declare it.
|
||||
let foreign_rdf = format!(
|
||||
r#"<x:xmpmeta xmlns:x="adobe:ns:meta/"><q:RDF xmlns:q="{NS}" xmlns:rdf="urn:other"><q:Description q:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmp:CreatorTool="Other App"/></q:RDF></x:xmpmeta>"#
|
||||
);
|
||||
let out = write_file_info(Some(&foreign_rdf), &json!({"authorTitle": "Artist"}));
|
||||
assert_eq!(rdf_descriptions(&out), ["rdf:Description", "rdf:Description"], "{out}");
|
||||
// The canonical case keeps its single declaration: the element binds `rdf` and the inserted
|
||||
// description does not repeat it.
|
||||
let canonical = format!(r#"<x:xmpmeta xmlns:x="adobe:ns:meta/"><rdf:RDF xmlns:rdf="{NS}"><rdf:Description rdf:about=""/></rdf:RDF></x:xmpmeta>"#);
|
||||
let out = write_file_info(Some(&canonical), &json!({"authorTitle": "Artist"}));
|
||||
assert_eq!(rdf_descriptions(&out), ["rdf:Description", "rdf:Description"], "{out}");
|
||||
assert_eq!(out.matches(&format!("xmlns:rdf=\"{NS}\"")).count(), 1, "{out}");
|
||||
}
|
||||
|
||||
/// A prefix that an ancestor binds and an inner element rebinds resolves to the INNER binding
|
||||
/// (Namespaces in XML 1.0, section 6): the innermost open scope is searched first (#11116,
|
||||
/// review of 952741fe). Both outputs are parsed as namespace-aware XML.
|
||||
#[test]
|
||||
fn file_info_resolves_a_prefix_in_the_innermost_scope_that_binds_it() {
|
||||
const NS: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
|
||||
let rdf_descriptions = |out: &str| {
|
||||
let namespaces = [("rdf", NS), ("xmp", "http://ns.adobe.com/xap/1.0/"), ("photoshop", "http://ns.adobe.com/photoshop/1.0/"), ("x", "adobe:ns:meta/")];
|
||||
let canonical = craft_xml::Canonical::new(&namespaces);
|
||||
let doc = craft_xml::Options::canonical(&canonical).parse(out.as_bytes()).expect("the output is well-formed XML");
|
||||
let names: Vec<String> = doc.descendants().filter(|e| e.name.ends_with("Description")).map(|e| e.name.clone()).collect();
|
||||
names
|
||||
};
|
||||
// The ancestor binds `rdf` to the RDF namespace, the element the edit lands in rebinds it to a
|
||||
// foreign one: the inserted description must declare `rdf` itself.
|
||||
let shadowed = format!(
|
||||
r#"<x:xmpmeta xmlns:x="adobe:ns:meta/" xmlns:rdf="{NS}"><q:RDF xmlns:q="{NS}" xmlns:rdf="urn:other"><q:Description q:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmp:CreatorTool="Other App"/></q:RDF></x:xmpmeta>"#
|
||||
);
|
||||
let out = write_file_info(Some(&shadowed), &json!({"authorTitle": "Artist"}));
|
||||
assert!(out.contains("xmp:CreatorTool=\"Other App\""), "the packet is kept: {out}");
|
||||
assert_eq!(rdf_descriptions(&out), ["rdf:Description", "rdf:Description"], "the inserted description belongs to the RDF namespace: {out}");
|
||||
assert_eq!(read_file_info(Some(&out))["authorTitle"], "Artist");
|
||||
// The ancestor binds `r` to the RDF namespace; a later element rebinds `r` and holds its own
|
||||
// `</r:RDF>`: that closing tag names a foreign element and is not the RDF end tag.
|
||||
let shadowed_end = format!(
|
||||
r#"<x:xmpmeta xmlns:x="adobe:ns:meta/" xmlns:r="{NS}"><r:RDF><r:Description r:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmp:CreatorTool="Other App"/></r:RDF><w xmlns:r="urn:other"><r:RDF></r:RDF></w></x:xmpmeta>"#
|
||||
);
|
||||
let out = write_file_info(Some(&shadowed_end), &json!({"authorTitle": "Artist"}));
|
||||
let (edit, real, foreign) = (out.find("AuthorsPosition").unwrap(), out.find("</r:RDF>").unwrap(), out.rfind("</r:RDF>").unwrap());
|
||||
assert!(edit < real && real < foreign, "the edit lands in the RDF element, not in the shadowed one: {out}");
|
||||
assert_eq!(read_file_info(Some(&out))["authorTitle"], "Artist");
|
||||
}
|
||||
|
||||
/// A prefix bound to the RDF namespace in one element and rebound to another namespace in a later
|
||||
/// sibling is two different names: only the closing tag that resolves to the RDF namespace in its
|
||||
/// own scope is the RDF end tag, so the edit never lands in the foreign element (#11116).
|
||||
#[test]
|
||||
fn file_info_resolves_the_rdf_end_tag_in_namespace_scope() {
|
||||
const NS: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
|
||||
// The prefix is rebound to a foreign namespace on a later sibling `r:RDF`.
|
||||
let rebound = format!(
|
||||
r#"<x:xmpmeta xmlns:x="adobe:ns:meta/"><r:RDF xmlns:r="{NS}"><r:Description r:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmp:CreatorTool="Other App"/></r:RDF><r:RDF xmlns:r="urn:other"></r:RDF></x:xmpmeta>"#
|
||||
);
|
||||
let out = write_file_info(Some(&rebound), &json!({"authorTitle": "Artist"}));
|
||||
let (edit, real, foreign) = (out.find("AuthorsPosition").unwrap(), out.find("</r:RDF>").unwrap(), out.rfind("</r:RDF>").unwrap());
|
||||
assert!(edit < real && real < foreign, "the edit sits in the RDF-namespace element, not the foreign one: {out}");
|
||||
assert!(out.contains("xmp:CreatorTool=\"Other App\""), "{out}");
|
||||
// The same through a default namespace: `<RDF>` is foreign where `xmlns` is rebound.
|
||||
let default_rebound = format!(
|
||||
r#"<x:xmpmeta xmlns:x="adobe:ns:meta/"><RDF xmlns="{NS}"><Description about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmp:CreatorTool="Other App"/></RDF><RDF xmlns="urn:other"></RDF></x:xmpmeta>"#
|
||||
);
|
||||
let out = write_file_info(Some(&default_rebound), &json!({"authorTitle": "Artist"}));
|
||||
let (edit, real) = (out.find("AuthorsPosition").unwrap(), out.find("</RDF>").unwrap());
|
||||
assert!(edit < real, "the edit sits in the RDF-namespace element: {out}");
|
||||
// An inner rebinding does not leak out of its element: the outer `r:RDF` still closes the RDF.
|
||||
let nested = format!(
|
||||
r#"<x:xmpmeta xmlns:x="adobe:ns:meta/"><r:RDF xmlns:r="{NS}"><r:Description r:about=""><f:g xmlns:f="urn:f" xmlns:r="urn:other"><r:RDF></r:RDF></f:g></r:Description></r:RDF></x:xmpmeta>"#
|
||||
);
|
||||
let out = write_file_info(Some(&nested), &json!({"authorTitle": "Artist"}));
|
||||
let inner = out.find("<r:RDF></r:RDF>").unwrap();
|
||||
assert!(out.find("AuthorsPosition").unwrap() > inner, "the edit is after the foreign element, in the real RDF: {out}");
|
||||
assert!(out.find("AuthorsPosition").unwrap() < out.rfind("</r:RDF>").unwrap(), "{out}");
|
||||
}
|
||||
|
||||
fn camera_exif() -> Vec<u8> {
|
||||
photocraft_algo::exif::build(&photocraft_algo::exif::CameraInfo {
|
||||
make: Some("SONY".into()),
|
||||
|
||||
Reference in new issue
Block a user