From e2e7b7332e08e607b66ef4cd2fb8135cc6564fdb Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Mon, 11 Sep 2023 14:59:05 +0200 Subject: [PATCH] [libdom] Added map (node -> element_offset) --- src/document/document.cpp | 7 +++++ src/document/document.h | 1 + src/document/libdom/mapa.cpp | 46 +++++++++++++++++++++++++++++++++ src/document/libdom/mapa.h | 5 ++++ src/document/libdom/renderer2.c | 20 ++++++++++---- 5 files changed, 74 insertions(+), 5 deletions(-) diff --git a/src/document/document.cpp b/src/document/document.cpp index a9f1f0db..8ae7fdaf 100644 --- a/src/document/document.cpp +++ b/src/document/document.cpp @@ -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); diff --git a/src/document/document.h b/src/document/document.h index cf3a08a8..893f6a49 100644 --- a/src/document/document.h +++ b/src/document/document.h @@ -234,6 +234,7 @@ struct document { #ifdef CONFIG_LIBDOM void *dom; void *element_map; + void *element_map_rev; char *text; void *forms_nodeset; #endif diff --git a/src/document/libdom/mapa.cpp b/src/document/libdom/mapa.cpp index 11e8438d..abeeaaf5 100644 --- a/src/document/libdom/mapa.cpp +++ b/src/document/libdom/mapa.cpp @@ -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 *mapa = static_cast *>(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 *mapa = new std::map; + + 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 *mapa = static_cast *>(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 *mapa = static_cast *>(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 *mapa = static_cast *>(m); + + if (!mapa) { + return -1; + } + + auto element = (*mapa).find(node); + + if (element == (*mapa).end()) { + return -1; + } + return (int)element->second; +} diff --git a/src/document/libdom/mapa.h b/src/document/libdom/mapa.h index 8eedb024..325df76e 100644 --- a/src/document/libdom/mapa.h +++ b/src/document/libdom/mapa.h @@ -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 } diff --git a/src/document/libdom/renderer2.c b/src/document/libdom/renderer2.c index bb8f2085..3fe59595 100644 --- a/src/document/libdom/renderer2.c +++ b/src/document/libdom/renderer2.c @@ -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);