/** * Extract an imagemap from html source * * \param node XML node containing map * \param c Content containing document * \param entry List of map entries * \return false on memory exhaustion, true otherwise */ bool imagemap_extract_map(xmlNode *node, html_content *c, struct mapentry **entry) { xmlNode *this_node; assert(c != NULL); assert(entry != NULL); if (node->type == XML_ELEMENT_NODE) { /** \todo ignore <area> elements if there are other * block-level elements present in map */ if (strcmp((const char *) node->name, "area") == 0 || strcmp((const char *) node->name, "a") == 0) { if (imagemap_addtolist(node, c->base_url, entry) == false) return false; } } else { return true; } for (this_node = node->children; this_node != NULL; this_node = this_node->next) { if (imagemap_extract_map(this_node, c, entry) == false) return false; } return true; }
/** * Extract all imagemaps from a document tree * * \param node Root node of tree * \param c The containing content * \return false on memory exhaustion, true otherwise */ bool imagemap_extract(xmlNode *node, html_content *c) { xmlNode *this_node; struct mapentry *entry = NULL; char *name; assert(node != NULL); assert(c != NULL); if (node->type == XML_ELEMENT_NODE && strcmp((const char *) node->name, "map") == 0) { /* Ignore maps with no id or name */ if ((name = (char *) xmlGetProp(node, (const xmlChar *) "id")) == NULL && (name = (char *) xmlGetProp(node, (const xmlChar *) "name")) == NULL) { return true; } if (imagemap_extract_map(node, c, &entry) == false) { xmlFree(name); return false; } /* imagemap_extract_map may not extract anything, * so entry can still be NULL here. This isn't an * error as it just means that we've encountered * an incorrectly defined <map>...</map> block */ if (entry && imagemap_add(c, name, entry) == false) { xmlFree(name); return false; } xmlFree(name); return true; } else if (node->type != XML_ELEMENT_NODE) { return true; } /* now recurse */ for (this_node = node->children; this_node != NULL; this_node = this_node->next) { if (imagemap_extract(this_node, c) == false) return false; } return true; }
/** * Extract all imagemaps from a document tree * * \param c The content to extract imagemaps from. * \return false on memory exhaustion, true otherwise */ nserror imagemap_extract(html_content *c) { dom_nodelist *nlist; dom_exception exc; unsigned long mapnr; uint32_t maybe_maps; nserror ret = NSERROR_OK; exc = dom_document_get_elements_by_tag_name(c->document, corestring_dom_map, &nlist); if (exc != DOM_NO_ERR) { return NSERROR_DOM; } exc = dom_nodelist_get_length(nlist, &maybe_maps); if (exc != DOM_NO_ERR) { ret = NSERROR_DOM; goto out_nlist; } for (mapnr = 0; mapnr < maybe_maps; ++mapnr) { dom_node *node; dom_string *name; exc = dom_nodelist_item(nlist, mapnr, &node); if (exc != DOM_NO_ERR) { ret = NSERROR_DOM; goto out_nlist; } exc = dom_element_get_attribute(node, corestring_dom_id, &name); if (exc != DOM_NO_ERR) { dom_node_unref(node); ret = NSERROR_DOM; goto out_nlist; } if (name == NULL) { exc = dom_element_get_attribute(node, corestring_dom_name, &name); if (exc != DOM_NO_ERR) { dom_node_unref(node); ret = NSERROR_DOM; goto out_nlist; } } if (name != NULL) { struct mapentry *entry = NULL; if (imagemap_extract_map(node, c, &entry) == false) { if (entry != NULL) { imagemap_freelist(entry); } dom_string_unref(name); dom_node_unref(node); ret = NSERROR_NOMEM; /** @todo check this */ goto out_nlist; } /* imagemap_extract_map may not extract anything, * so entry can still be NULL here. This isn't an * error as it just means that we've encountered * an incorrectly defined <map>...</map> block */ if ((entry != NULL) && (imagemap_add(c, name, entry) == false)) { imagemap_freelist(entry); dom_string_unref(name); dom_node_unref(node); ret = NSERROR_NOMEM; /** @todo check this */ goto out_nlist; } } dom_string_unref(name); dom_node_unref(node); } out_nlist: dom_nodelist_unref(nlist); return ret; }