diff --git a/src/document/document.cpp b/src/document/document.cpp index 53454d0eb..03d9cd3b8 100644 --- a/src/document/document.cpp +++ b/src/document/document.cpp @@ -310,7 +310,9 @@ reset_document(struct document *document) #if defined(CONFIG_ECMASCRIPT_SMJS) || defined(CONFIG_QUICKJS) || defined(CONFIG_MUJS) free_ecmascript_string_list(&document->onload_snippets); free_uri_list(&document->ecmascript_imports); - mem_free_set(&document->text, NULL); + if (document->text.length) { + done_string(&document->text); + } mem_free_set(&document->body_onkeypress, NULL); /// kill_timer(&document->timeout); /// free_document(document->dom); @@ -401,7 +403,9 @@ done_document(struct document *document) #endif #ifdef CONFIG_LIBDOM - mem_free_if(document->text); + if (document->text.length) { + done_string(&document->text); + } free_document(document->dom); if (document->element_map) { diff --git a/src/document/document.h b/src/document/document.h index fff228433..8396a6da7 100644 --- a/src/document/document.h +++ b/src/document/document.h @@ -24,6 +24,7 @@ struct frameset_desc; struct image; struct module; struct screen_char; +struct string; /** Nodes are used for marking areas of text on the document canvas as * searchable. */ @@ -238,7 +239,7 @@ struct document { void *dom; void *element_map; void *element_map_rev; - char *text; + struct string text; void *forms_nodeset; #endif diff --git a/src/document/html/parser/general.c b/src/document/html/parser/general.c index b3e6dc868..8dccef6e6 100644 --- a/src/document/html/parser/general.c +++ b/src/document/html/parser/general.c @@ -331,7 +331,7 @@ not_processed: /* Create URL reference onload snippet. */ insert_in_string(&import_url, 0, "^", 1); add_to_ecmascript_string_list(&html_context->part->document->onload_snippets, - import_url, -1, html_top->name - html_context->part->document->text); + import_url, -1, html_top->name - html_context->part->document->text.source); imported: /* Retreat. Do not permit nested scripts, tho'. */ @@ -411,7 +411,7 @@ imported: if (html_context->part->document && *html != '^') { add_to_ecmascript_string_list(&html_context->part->document->onload_snippets, - html, *end - html, html_top->name - html_context->part->document->text); + html, *end - html, html_top->name - html_context->part->document->text.source); } #endif } diff --git a/src/document/html/renderer.c b/src/document/html/renderer.c index b6ba35a96..4958b259b 100644 --- a/src/document/html/renderer.c +++ b/src/document/html/renderer.c @@ -1558,7 +1558,7 @@ new_link(struct html_context *html_context, const char *name, int namelen) : elformat.color.clink; #ifdef CONFIG_ECMASCRIPT - link->element_offset = elformat.top_name ? elformat.top_name - document->text : 0; + link->element_offset = (elformat.top_name && document->text.source) ? elformat.top_name - document->text.source : 0; #endif init_link_event_hooks(html_context, link); diff --git a/src/document/libdom/css.c b/src/document/libdom/css.c index 0bd3b499b..24cdbd390 100644 --- a/src/document/libdom/css.c +++ b/src/document/libdom/css.c @@ -2063,7 +2063,7 @@ select_css(struct html_context *html_context, struct html_element *html_element) css_media media = { .type = CSS_MEDIA_SCREEN, }; - int offset = html_element->name - html_context->document->text; + int offset = html_element->name - html_context->document->text.source; dom_node *el = (dom_node *)find_in_map(html_context->document->element_map, offset); if (!el) { @@ -2292,7 +2292,7 @@ parse_css_common(struct html_context *html_context, const char *text, int length void parse_css(struct html_context *html_context, char *name) { - int offset = name - html_context->document->text; + int offset = name - html_context->document->text.source; dom_node *el = (dom_node *)find_in_map(html_context->document->element_map, offset); dom_node *n, *next; dom_exception err; diff --git a/src/document/libdom/renderer.c b/src/document/libdom/renderer.c index 8e2a697b7..b2d3f708d 100644 --- a/src/document/libdom/renderer.c +++ b/src/document/libdom/renderer.c @@ -266,10 +266,8 @@ render_source_document_cxx(struct cache_entry *cached, struct document *document } if (!buffer->length) { - struct string tt; - - if (!init_string(&tt)) { - return; + if (document->text.length) { + done_string(&document->text); } mapa = document->element_map; @@ -280,14 +278,15 @@ render_source_document_cxx(struct cache_entry *cached, struct document *document clear_map(mapa); } - if (walk_tree(mapa, &tt, root, true, 0) == false) { + if (walk_tree(mapa, &document->text, root, true, 0) == false) { fprintf(stderr, "Failed to complete DOM structure dump.\n"); dom_node_unref(root); //dom_node_unref(doc); return; } - *buffer = tt; - document->text = tt.source; + dom_node_unref(root); + render_plain_document(cached, document, &document->text); + return; } dom_node_unref(root); render_plain_document(cached, document, buffer); diff --git a/src/document/libdom/renderer2.c b/src/document/libdom/renderer2.c index b0cf0ca07..5623a719e 100644 --- a/src/document/libdom/renderer2.c +++ b/src/document/libdom/renderer2.c @@ -317,10 +317,8 @@ dump_xhtml(struct cache_entry *cached, struct document *document, int parse) } if (1) { - struct string tt; - - if (!init_string(&tt)) { - return; + if (document->text.length) { + done_string(&document->text); } mapa = document->element_map; @@ -339,7 +337,7 @@ dump_xhtml(struct cache_entry *cached, struct document *document, int parse) clear_map(mapa_rev); } - if (walk_tree(mapa, mapa_rev, &tt, root, true, 0) == false) { + if (walk_tree(mapa, mapa_rev, &document->text, root, true, 0) == false) { fprintf(stderr, "Failed to complete DOM structure dump.\n"); dom_node_unref(root); //dom_node_unref(doc); @@ -350,11 +348,9 @@ dump_xhtml(struct cache_entry *cached, struct document *document, int parse) if (parse) { free_document(document); document->dom = NULL; - render_xhtml_document(cached, document, &tt); - done_string(&tt); + render_xhtml_document(cached, document, &document->text); return; } - document->text = tt.source; - render_html_document(cached, document, &tt); + render_html_document(cached, document, &document->text); } }