From 882d1100535a2762b602df39266bc0546debf32a Mon Sep 17 00:00:00 2001 From: mtlcl410 <2368091+mtlcl410@users.noreply.github.com> Date: Mon, 26 Oct 2026 00:18:49 +0800 Subject: [PATCH] M12: XFA prototypes: inherited prefixed attributes keep their namespace URI Attributes were told apart and written by prefixed name, so an inherited q:kind whose prefix the element binds to another URI read back in the element's namespace, and a same-named attribute in another namespace was dropped. Attributes are now told apart by (URI, local name); one whose prefix is bound to another URI on the merged element is written with a fresh prefix declared there. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01CBVyQEwtmEw3mS1MoAiwrK --- crates/xfa/src/proto.rs | 43 ++++++++++++++++++++++++++++++++--- crates/xfa/src/proto/tests.rs | 17 ++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/crates/xfa/src/proto.rs b/crates/xfa/src/proto.rs index 715dec9393..7ef363a142 100644 --- a/crates/xfa/src/proto.rs +++ b/crates/xfa/src/proto.rs @@ -47,6 +47,8 @@ const MAX_SEARCH: usize = 2_000_000; /// budget allows costs some 12 million: this keeps a margin of five over that, and stops a /// hostile template after about a second. const MAX_WORK: usize = 64_000_000; +/// The namespace the `xml` prefix is always bound to. +const XML_NS: &str = "http://www.w3.org/XML/1998/namespace"; /// The template written again with its prototype references resolved (`None` when it has /// none, or when resolving them would pass a budget: the template is then read as it is), and @@ -377,15 +379,50 @@ impl<'a, 'd> Writer<'a, 'd> { inner.insert(None, ""); } // Attributes: the nearest layer that has one wins; `id` and `name` are not inherited. - let mut written: HashSet = HashSet::new(); + // Attributes are told apart by namespace URI and local name. One whose layer's prefix is + // bound here to another URI (the nearest layer's binding won) is written with a fresh + // prefix, declared on this element. + let mut written: HashSet<(Option<&str>, &str)> = HashSet::new(); + let mut fresh: HashMap<&str, String> = HashMap::new(); + let mut next = 0usize; for (i, layer) in layers.iter().enumerate() { for a in layer.attributes() { let inherited = i > 0 && matches!(a.name(), "id" | "name"); let reference = a.namespace().is_none() && matches!(a.name(), "use" | "usehref"); - let name = format!("{}{}", prefix(*layer, a.namespace()), a.name()); - if inherited || reference || !written.insert(name.clone()) { + if inherited || reference || !written.insert((a.namespace(), a.name())) { continue; } + let name = match a.namespace() { + None => a.name().to_string(), + Some(XML_NS) => format!("xml:{}", a.name()), + Some(uri) => match layer.lookup_prefix(uri).filter(|p| !p.is_empty()) { + Some(p) if inner.get(&Some(p)) == Some(&uri) => format!("{p}:{}", a.name()), + _ => { + let p = match fresh.get(uri) { + Some(p) => p.clone(), + None => { + // `next` only grows, and skips the prefixes in scope here, + // so it passes at most `inner.len()` of them. + let p = loop { + let p = format!("ns{next}"); + next = next.saturating_add(1); + if !inner.contains_key(&Some(p.as_str())) { + break p; + } + }; + self.out.push_str(" xmlns:"); + self.out.push_str(&p); + self.out.push_str("=\""); + escape(uri, true, &mut self.out); + self.out.push('"'); + fresh.insert(uri, p.clone()); + p + } + }; + format!("{p}:{}", a.name()) + } + }, + }; self.out.push(' '); self.out.push_str(&name); self.out.push_str("=\""); diff --git a/crates/xfa/src/proto/tests.rs b/crates/xfa/src/proto/tests.rs index c06615b11d..f659a616ce 100644 --- a/crates/xfa/src/proto/tests.rs +++ b/crates/xfa/src/proto/tests.rs @@ -298,3 +298,20 @@ fn namespaces_bound_again_inside_the_template_keep_their_own_uri() { "{paras:?}" ); } + +#[test] +fn inherited_attributes_keep_their_namespace_where_the_prefix_is_bound_again() { + // The element binds `q` to one URI and its prototype to another: the element's binding + // wins, so the prototype's `q:` attributes need another prefix to keep their own URI. + let styles = r#""#; + let body = r##""##; + let out = expanded(&form(styles, body)); + let doc = roxmltree::Document::parse(&out).unwrap(); + let f = doc.descendants().find(|n| n.attribute("name") == Some("f")).unwrap(); + assert_eq!(f.attribute(("urn:instance", "own")), Some("1"), "{out}"); + assert_eq!(f.attribute(("urn:prototype", "own")), Some("2"), "{out}"); + assert_eq!(f.attribute(("urn:prototype", "kind")), Some("k"), "{out}"); + assert_eq!(f.attribute(("urn:instance", "kind")), None, "{out}"); + assert_eq!(f.attribute(("http://www.w3.org/XML/1998/namespace", "lang")), Some("en"), "{out}"); + assert_eq!(f.attribute("w"), Some("1in"), "{out}"); +}