The headless browser for AI agents and web scraping
fix(dom): write xml, xlink and xmlns attributes with their fixed prefix
404Humanf2bf4c33d1cb
diff --git a/crates/obscura-dom/src/serialize.rs b/crates/obscura-dom/src/serialize.rsindex 38bf21e9..37eeb411 100644--- a/crates/obscura-dom/src/serialize.rs+++ b/crates/obscura-dom/src/serialize.rs@@ -106,11 +106,7 @@ impl DomTree {buf.push_str(tag);for attr in attrs {buf.push(' ');- if let Some(prefix) = &attr.name.prefix {- buf.push_str(prefix);- buf.push(':');- }- buf.push_str(attr.name.local.as_ref());+ push_attribute_name(&attr.name, buf);buf.push_str("=\"");escape_attr(&attr.value, buf);buf.push('"');@@ -218,6 +214,28 @@ fn escape(s: &str, attribute: bool, buf: &mut String) {buf.push_str(&s[copied..]);}+/// An attribute's serialized name. The three namespaces HTML knows are+/// written with their fixed prefix whatever prefix the attribute carries.+fn push_attribute_name(name: &QualName, buf: &mut String) {+ let local = name.local.as_ref();+ let prefix = if name.ns == ns!() {+ None+ } else if name.ns == ns!(xml) {+ Some("xml")+ } else if name.ns == ns!(xlink) {+ Some("xlink")+ } else if name.ns == ns!(xmlns) {+ (local != "xmlns").then_some("xmlns")+ } else {+ name.prefix.as_ref().map(|prefix| prefix.as_ref())+ };+ if let Some(prefix) = prefix {+ buf.push_str(prefix);+ buf.push(':');+ }+ buf.push_str(local);+}+/// "Serializes as void": the void elements plus four obsolete ones. Such an/// element gets no end tag and its children are not written. HTML elements/// only: `<br>` created in another namespace is an ordinary element.@@ -411,6 +429,32 @@ mod tests {assert_eq!(tree.inner_html(br), "<g></g>");}+ #[test]+ fn namespaced_attributes_are_written_with_their_fixed_prefix() {+ let tree = parse_html("<span id=host></span>");+ let attr = |ns, prefix: Option<&str>, local: &str| crate::tree::Attribute {+ name: QualName::new(prefix.map(html5ever::Prefix::from), ns, LocalName::from(local)),+ value: "v".to_string(),+ };+ let svg = tree.new_node(NodeData::Element {+ name: QualName::new(None, ns!(svg), LocalName::from("svg")),+ attrs: vec![+ attr(ns!(xml), Some("abc"), "lang"),+ attr(ns!(xlink), None, "href"),+ attr(ns!(xmlns), None, "xmlns"),+ attr(ns!(xmlns), Some("x"), "foo"),+ attr(ns!(), None, "plain"),+ attr(html5ever::Namespace::from("urn:other"), Some("o"), "k"),+ ],+ template_contents: None,+ mathml_annotation_xml_integration_point: false,+ });+ assert_eq!(+ tree.outer_html(svg),+ r#"<svg xml:lang="v" xlink:href="v" xmlns="v" xmlns:foo="v" plain="v" o:k="v"></svg>"#+ );+ }+#[test]fn test_void_elements() {let tree = parse_html(r#"<img src="test.png"><br>"#);