diff --git a/manipulation.go b/manipulation.go index d88114c..8bf86aa 100644 --- a/manipulation.go +++ b/manipulation.go @@ -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) diff --git a/manipulation_test.go b/manipulation_test.go index 1119689..fd5e020 100644 --- a/manipulation_test.go +++ b/manipulation_test.go @@ -9,6 +9,28 @@ const ( wrapHtml = "
test string

" ) +func TestClonePreservesNamespaces(t *testing.T) { + doc := loadString(t, `
x
`) + 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")