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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CBVyQEwtmEw3mS1MoAiwrK
This commit is contained in:
mtlcl410
2026-10-30 07:26:03 +08:00
co-authored by Claude Opus 5.5
parent 73d06b1bd7
commit 882d110053
2 changed files with 57 additions and 3 deletions
+40 -3
View File
@@ -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<String> = 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("=\"");
+17
View File
@@ -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#"<field id="P" xmlns:q="urn:prototype" q:kind="k" q:own="2" xml:lang="en" w="1in"/>"#;
let body = r##"<field name="f" xmlns:q="urn:instance" q:own="1" use="#P"/>"##;
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}");
}