Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SetNamespace docs and behavior when namespace already exist #103

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

lestrrat
Copy link
Collaborator

fixes #102

Copy link

@guitarpawat guitarpawat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pulled your library and found a possible way to fix the current namespace prefix issue by using the same namespace pointer if it already exists.
I am not familiar with CGO, so I am not sure if this is the right way to do.

Comment on lines +50 to 62
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 {
return err
}

if activateflag {
if err := clib.XMLSetNs(n, wrapNamespaceNode(ns)); err != nil {
return err
}
}
}
Copy link

@guitarpawat guitarpawat Jun 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the same namespace pointer if it already exists

Suggested change
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 {
return err
}
if activateflag {
if err := clib.XMLSetNs(n, wrapNamespaceNode(ns)); err != nil {
return err
}
}
}
ns, _ := clib.XMLSearchNs(doc, n, prefix)
if ns == 0 {
// Namespace not found, create a new one
ns, err = clib.XMLNewNs(n, uri, prefix)
if err != nil {
return err
}
}
if activateflag {
if err := clib.XMLSetNs(n, wrapNamespaceNode(ns)); err != nil {
return err
}
}

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")
})
Copy link

@guitarpawat guitarpawat Jun 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add test to check namespace prefix

Suggested change
})
// root.Dump(true) should contains namespace prefix for Document
count = strings.Count(dump, "ex:Document")
require.Equal(t, 2, count, "expected Document to have 'ex' namespace prefix")
// root.Dump(true) should contains namespace prefix for Test
count = strings.Count(dump, "ex:Test")
require.Equal(t, 2, count, "expected Test to have 'ex' namespace prefix")
})

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")
})
Copy link

@guitarpawat guitarpawat Jun 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add test to check namespace prefix

Suggested change
})
// root.Dump(true) should contains namespace prefix for Document
count = strings.Count(dump, "ex:Document")
require.Equal(t, 2, count, "expected Document to have 'ex' namespace prefix")
// root.Dump(true) should contains namespace prefix for Test
count = strings.Count(dump, "ex:Test")
require.Equal(t, 2, count, "expected Test to have 'ex' namespace prefix")
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

dom.SetNamespace creates xmlns: in child elements
2 participants