Merge pull request #609 from agammann/fix-clone-node-namespaces

Preserve SVG and MathML namespaces when cloning nodes
This commit is contained in:
Martin Angers
2026-09-22 11:23:20 -04:00
committed by GitHub
2 changed files with 27 additions and 4 deletions
+5 -4
View File
@@ -598,10 +598,11 @@ func cloneNodes(ns []*html.Node) []*html.Node {
// children but none of its parents or siblings.
func cloneNode(n *html.Node) *html.Node {
nn := &html.Node{
Type: n.Type,
DataAtom: n.DataAtom,
Data: n.Data,
Attr: make([]html.Attribute, len(n.Attr)),
Type: n.Type,
DataAtom: n.DataAtom,
Data: n.Data,
Namespace: n.Namespace,
Attr: make([]html.Attribute, len(n.Attr)),
}
copy(nn.Attr, n.Attr)
+22
View File
@@ -9,6 +9,28 @@ const (
wrapHtml = "<div id=\"ins\">test string<div><p><em><b></b></em></p></div></div>"
)
func TestClonePreservesNamespaces(t *testing.T) {
doc := loadString(t, `<div><svg><circle></circle></svg><math><mi>x</mi></math></div>`)
for name, cloned := range map[string]*Selection{
"selection": doc.Find("div").Clone(),
"document": CloneDocument(doc).Selection,
} {
t.Run(name, func(t *testing.T) {
for selector, namespace := range map[string]string{
"svg": "svg", "circle": "svg", "math": "math", "mi": "math",
} {
node := cloned.Find(selector).Get(0)
if node == nil {
t.Fatalf("missing cloned %s", selector)
}
if node.Namespace != namespace {
t.Errorf("%s namespace = %q, want %q", selector, node.Namespace, namespace)
}
}
})
}
}
func TestAfter(t *testing.T) {
doc := Doc2Clone()
doc.Find("#main").After("#nf6")