mirror of
https://github.com/PuerkitoBio/goquery.git
synced 2026-10-19 16:15:33 +00:00
Preserve SVG and MathML namespaces when cloning nodes
This commit is contained in:
+5
-4
@@ -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)
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user