-
-
Notifications
You must be signed in to change notification settings - Fork 55
Fix SetNamespace docs and behavior when namespace already exist #103
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") | |
}) |
This PR is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 14 days. |
This PR was closed because it has been stalled for 14 days with no activity. This does not mean your PR is rejected, but rather it is done to hide it from the view of the maintainers for the time being. Feel free to reopen if you have new comments or chnages that you would like to include. |
fixes #102