From f11647cce390ae597eb4bca95725ced2ee26bee5 Mon Sep 17 00:00:00 2001 From: hnoerenberg Date: Mon, 16 Nov 2026 17:27:19 +0100 Subject: [PATCH] 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 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. --- crates/engine/src/cmd/file_cmds/info.rs | 4 ++- crates/engine/src/cmd/file_cmds/tests.rs | 33 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/file_cmds/info.rs b/crates/engine/src/cmd/file_cmds/info.rs index 96c8ef1da8..f1c93b0bc4 100644 --- a/crates/engine/src/cmd/file_cmds/info.rs +++ b/crates/engine/src/cmd/file_cmds/info.rs @@ -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::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; diff --git a/crates/engine/src/cmd/file_cmds/tests.rs b/crates/engine/src/cmd/file_cmds/tests.rs index 7100bb2539..4e636e2fbb 100644 --- a/crates/engine/src/cmd/file_cmds/tests.rs +++ b/crates/engine/src/cmd/file_cmds/tests.rs @@ -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 = 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#""# + ); + 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 + // ``: that closing tag names a foreign element and is not the RDF end tag. + let shadowed_end = format!( + r#""# + ); + let out = write_file_info(Some(&shadowed_end), &json!({"authorTitle": "Artist"})); + let (edit, real, foreign) = (out.find("AuthorsPosition").unwrap(), out.find("").unwrap(), out.rfind("").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).