fix(file-info): resolve a namespace prefix in the innermost scope first

rdf_end_scan searched the open scopes with iter().rev().flatten().rev(), which reverses the flattened list a second time, so an outer declaration won over an inner rebinding. An ancestor binding rdf to the RDF namespace then hid an inner xmlns:rdf="urn:other": the inserted rdf:Description landed in the foreign namespace, and a shadowed </r:RDF> could be taken for the RDF end tag. Search the scopes innermost first.

A parsed-XML regression covers an ancestor binding with an inner rebinding at the insertion point, and a shadowed closing tag in a later element.
This commit is contained in:
hnoerenberg committed 2026-11-16 17:27:19 +01:00
1 parent 952741fe7a
commit f11647cce3
2 files changed
+36 -1

No files matched your search

+3 -1
View File
@@ -340,9 +340,11 @@ fn rdf_end_scan(x: &str) -> Option<(usize, usize, bool)> {
// 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().flatten().rev().find(|(p, _)| p == prefix).is_some_and(|(_, rdf)| *rdf)
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;
+33
View File
@@ -2355,6 +2355,39 @@ fn file_info_binds_rdf_for_the_description_from_the_open_element_scope() {
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).