Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
lestrrat committed Jun 26, 2024
1 parent b1f63e9 commit 38fc65c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
6 changes: 1 addition & 5 deletions dom/node_element.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ func (n *Element) SetNamespace(uri, prefix string, activate ...bool) error {
return errors.New("missing uri for SetNamespace")
}

root, err := doc.DocumentElement()
if err != nil {
return err
}
if _, err := clib.XMLSearchNs(doc, root, prefix); err != nil {
if oldNs, _ := clib.XMLSearchNs(doc, n, prefix); oldNs == 0 {
// Namespace not found, create a new one
ns, err := clib.XMLNewNs(n, uri, prefix)
if err != nil {
Expand Down
46 changes: 32 additions & 14 deletions dom/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,19 +243,37 @@ func TestCreateElementNS(t *testing.T) {
}

func TestGH102(t *testing.T) {
root := CreateDocument()
doc, _ := root.CreateElement("Document")
_ = doc.SetNamespace("https://example.com", "ex")
_ = root.SetDocumentElement(doc)
test, _ := root.CreateElement("Test")
_ = test.SetNamespace("https://example.com", "ex")
_ = doc.AddChild(test)
test.SetNodeValue("test")
t.Run("child ns after parent ns", func(t *testing.T) {
root := CreateDocument()
doc, _ := root.CreateElement("Document")
_ = doc.SetNamespace("https://example.com", "ex")
test, _ := root.CreateElement("Test")
_ = doc.AddChild(test)
_ = root.SetDocumentElement(doc)
_ = test.SetNamespace("https://example.com", "ex")
test.SetNodeValue("test")

// root.Dump(true) should only contain one namespace declaration
// for the 'ex' prefix
dump := root.Dump(true)
count := strings.Count(dump, `xmlns:ex="https://example.com"`)
t.Logf("%s", dump)
require.Equal(t, 1, count, "expected only one namespace declaration for 'ex' prefix")
// root.Dump(true) should only contain one namespace declaration
// for the 'ex' prefix
dump := root.Dump(true)
count := strings.Count(dump, `xmlns:ex="https://example.com"`)
t.Logf("%s", dump)
require.Equal(t, 1, count, "expected only one namespace declaration for 'ex' prefix")
})

t.Run("parent ns after child ns", func(t *testing.T) {
root := CreateDocument()
doc, _ := root.CreateElement("Document")
//_ = doc.SetNamespace("https://example.com", "ex") // delete
_ = root.SetDocumentElement(doc)
test, _ := root.CreateElement("Test")
_ = test.SetNamespace("https://example.com", "ex")
_ = doc.SetNamespace("https://example.com", "ex") // move to here
_ = doc.AddChild(test)
test.SetNodeValue("test")
dump := root.Dump(true)
count := strings.Count(dump, `xmlns:ex="https://example.com"`)
t.Logf("%s", dump)
require.Equal(t, 2, count, "expected two namespace declaration for 'ex' prefix")
})
}

0 comments on commit 38fc65c

Please sign in to comment.