1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-09-28 03:06:20 -04:00

[libdom] Added map (node -> element_offset)

This commit is contained in:
Witold Filipczyk 2023-09-11 14:59:05 +02:00
parent 207dc70c87
commit e2e7b7332e
5 changed files with 74 additions and 5 deletions

View File

@ -405,6 +405,13 @@ done_document(struct document *document)
clear_map(mapa);
delete_map(mapa);
}
if (document->element_map_rev) {
void *mapa = document->element_map_rev;
clear_map_rev(mapa);
delete_map_rev(mapa);
}
#endif
free_list(document->tags);
free_list(document->nodes);

View File

@ -234,6 +234,7 @@ struct document {
#ifdef CONFIG_LIBDOM
void *dom;
void *element_map;
void *element_map_rev;
char *text;
void *forms_nodeset;
#endif

View File

@ -15,6 +15,13 @@ save_in_map(void *m, void *node, int length)
(*mapa)[length] = node;
}
void
save_offset_in_map(void *m, void *node, int offset)
{
std::map<void *, int> *mapa = static_cast<std::map<void *, int> *>(m);
(*mapa)[node] = offset;
}
void *
create_new_element_map(void)
{
@ -23,6 +30,14 @@ create_new_element_map(void)
return (void *)mapa;
}
void *
create_new_element_map_rev(void)
{
std::map<void *, int> *mapa = new std::map<void *, int>;
return (void *)mapa;
}
void
clear_map(void *m)
{
@ -30,6 +45,13 @@ clear_map(void *m)
mapa->clear();
}
void
clear_map_rev(void *m)
{
std::map<void *, int> *mapa = static_cast<std::map<void *, int> *>(m);
mapa->clear();
}
void
delete_map(void *m)
{
@ -37,6 +59,13 @@ delete_map(void *m)
delete mapa;
}
void
delete_map_rev(void *m)
{
std::map<void *, int> *mapa = static_cast<std::map<void *, int> *>(m);
delete mapa;
}
void *
find_in_map(void *m, int offset)
{
@ -52,3 +81,20 @@ find_in_map(void *m, int offset)
}
return (void *)element->second;
}
int
find_offset(void *m, void *node)
{
std::map<void *, int> *mapa = static_cast<std::map<void *, int> *>(m);
if (!mapa) {
return -1;
}
auto element = (*mapa).find(node);
if (element == (*mapa).end()) {
return -1;
}
return (int)element->second;
}

View File

@ -6,11 +6,16 @@ extern "C" {
#endif
void save_in_map(void *m, void *node, int length);
void save_offset_in_map(void *m, void *node, int offset);
void *create_new_element_map(void);
void *create_new_element_map_rev(void);
void clear_map(void *m);
void delete_map(void *m);
void clear_map_rev(void *m);
void delete_map_rev(void *m);
void *find_in_map(void *m, int offset);
int find_offset(void *m, void *node);
#ifdef __cplusplus
}

View File

@ -104,7 +104,7 @@ dump_node_element_attribute(struct string *buf, dom_node *node)
static bool
dump_dom_element(void *mapa, struct string *buf, dom_node *node, int depth)
dump_dom_element(void *mapa, void *mapa_rev, struct string *buf, dom_node *node, int depth)
{
dom_exception exc;
dom_string *node_name = NULL;
@ -147,6 +147,7 @@ dump_dom_element(void *mapa, struct string *buf, dom_node *node, int depth)
add_char_to_string(buf, '<');
save_in_map(mapa, node, buf->length);
save_offset_in_map(mapa_rev, node, buf->length);
/* Get string data and print element name */
add_bytes_to_string(buf, dom_string_data(node_name), dom_string_byte_length(node_name));
@ -185,13 +186,13 @@ dump_dom_element(void *mapa, struct string *buf, dom_node *node, int depth)
}
static bool
walk_tree(void *mapa, struct string *buf, dom_node *node, bool start, int depth)
walk_tree(void *mapa, void *mapa_rev, struct string *buf, dom_node *node, bool start, int depth)
{
dom_exception exc;
dom_node *child;
/* Print this node's entry */
if (dump_dom_element(mapa, buf, node, depth) == false) {
if (dump_dom_element(mapa, mapa_rev, buf, node, depth) == false) {
/* There was an error; return */
return false;
}
@ -210,7 +211,7 @@ walk_tree(void *mapa, struct string *buf, dom_node *node, bool start, int depth)
dom_node *next_child;
/* Visit node's descendents */
if (walk_tree(mapa, buf, child, false, depth) == false) {
if (walk_tree(mapa, mapa_rev, buf, child, false, depth) == false) {
/* There was an error; return */
dom_node_unref(child);
return false;
@ -241,6 +242,7 @@ render_xhtml_document(struct cache_entry *cached, struct document *document, str
dom_document *doc = NULL; /* document, loaded into libdom */
dom_node *root = NULL; /* root element of document */
void *mapa = NULL;
void *mapa_rev = NULL;
static int initialised = 0;
if (!initialised) {
@ -299,8 +301,16 @@ render_xhtml_document(struct cache_entry *cached, struct document *document, str
} else {
clear_map(mapa);
}
mapa_rev = document->element_map_rev;
if (walk_tree(mapa, &tt, root, true, 0) == false) {
if (!mapa_rev) {
mapa_rev = create_new_element_map_rev();
document->element_map_rev = (void *)mapa_rev;
} else {
clear_map(mapa_rev);
}
if (walk_tree(mapa, mapa_rev, &tt, root, true, 0) == false) {
fprintf(stderr, "Failed to complete DOM structure dump.\n");
dom_node_unref(root);
//dom_node_unref(doc);