* xmlGetNsListSafe: * @doc: the document * @node: the current node * @out: the returned namespace array * * Find all in-scope namespaces of a node. @out returns a NULL * terminated array of namespace pointers that must be freed by * the caller. * * Available since 2.13.0. * * Returns 0 on success, 1 if no namespaces were found, -1 if a * memory allocation failed. */
| 5910 | * memory allocation failed. |
| 5911 | */ |
| 5912 | int |
| 5913 | xmlGetNsListSafe(const xmlDoc *doc ATTRIBUTE_UNUSED, const xmlNode *node, |
| 5914 | xmlNsPtr **out) |
| 5915 | { |
| 5916 | xmlNsPtr cur; |
| 5917 | xmlNsPtr *namespaces = NULL; |
| 5918 | int nbns = 0; |
| 5919 | int maxns = 0; |
| 5920 | int i; |
| 5921 | |
| 5922 | if (out == NULL) |
| 5923 | return(1); |
| 5924 | *out = NULL; |
| 5925 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
| 5926 | return(1); |
| 5927 | |
| 5928 | while (node != NULL) { |
| 5929 | if (node->type == XML_ELEMENT_NODE) { |
| 5930 | cur = node->nsDef; |
| 5931 | while (cur != NULL) { |
| 5932 | for (i = 0; i < nbns; i++) { |
| 5933 | if ((cur->prefix == namespaces[i]->prefix) || |
| 5934 | (xmlStrEqual(cur->prefix, namespaces[i]->prefix))) |
| 5935 | break; |
| 5936 | } |
| 5937 | if (i >= nbns) { |
| 5938 | if (nbns >= maxns) { |
| 5939 | xmlNsPtr *tmp; |
| 5940 | |
| 5941 | maxns = maxns ? maxns * 2 : 10; |
| 5942 | tmp = (xmlNsPtr *) xmlRealloc(namespaces, |
| 5943 | (maxns + 1) * |
| 5944 | sizeof(xmlNsPtr)); |
| 5945 | if (tmp == NULL) { |
| 5946 | xmlFree(namespaces); |
| 5947 | return(-1); |
| 5948 | } |
| 5949 | namespaces = tmp; |
| 5950 | } |
| 5951 | namespaces[nbns++] = cur; |
| 5952 | namespaces[nbns] = NULL; |
| 5953 | } |
| 5954 | |
| 5955 | cur = cur->next; |
| 5956 | } |
| 5957 | } |
| 5958 | node = node->parent; |
| 5959 | } |
| 5960 | |
| 5961 | *out = namespaces; |
| 5962 | return((namespaces == NULL) ? 1 : 0); |
| 5963 | } |
| 5964 | |
| 5965 | /** |
| 5966 | * xmlGetNsList: |
no test coverage detected