-
-
Notifications
You must be signed in to change notification settings - Fork 55
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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.
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 | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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
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") | ||
}) |
There was a problem hiding this comment.
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
}) | |
// 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") | ||
}) |
There was a problem hiding this comment.
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
}) | |
// 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") | |
}) |
fixes #102