From 7885fb0797bf1311379fc892e4d4598782d47743 Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Fri, 17 May 2024 22:05:25 +0200 Subject: [PATCH] [libdom] debug_dump_xhtml Helper function which print current state of document, called after modification of DOM tree. --- src/document/libdom/renderer2.c | 50 ++++++++++++++++++++- src/document/libdom/renderer2.h | 1 + src/ecmascript/ecmascript.h | 2 - src/ecmascript/spidermonkey.cpp | 3 +- src/ecmascript/spidermonkey/document.cpp | 6 +++ src/ecmascript/spidermonkey/element.cpp | 57 +++++++++++++++++++++++- src/ecmascript/spidermonkey/style.cpp | 7 +++ src/setup.h | 2 + 8 files changed, 120 insertions(+), 8 deletions(-) diff --git a/src/document/libdom/renderer2.c b/src/document/libdom/renderer2.c index dcef7d22..56748cff 100644 --- a/src/document/libdom/renderer2.c +++ b/src/document/libdom/renderer2.c @@ -166,8 +166,12 @@ dump_dom_element(void *mapa, void *mapa_rev, struct string *buf, dom_node *node, } add_char_to_string(buf, '<'); - save_in_map(mapa, node, buf->length); - save_offset_in_map(mapa_rev, node, buf->length); + if (mapa) { + save_in_map(mapa, node, buf->length); + } + if (mapa_rev) { + save_offset_in_map(mapa_rev, node, buf->length); + } /* Get string data and print element name */ add_lowercase_to_string(buf, dom_string_data(node_name), dom_string_byte_length(node_name)); @@ -475,6 +479,48 @@ walk_tree2_color(struct terminal *term, struct el_box *box, struct document *doc } #endif +void +debug_dump_xhtml(void *d) +{ +#ifdef ECMASCRIPT_DEBUG + dom_document *doc = (dom_document *)d; + + if (!doc) { + return; + } + dom_node *root = NULL; + dom_exception exc = dom_document_get_document_element(doc, &root); + + if (exc != DOM_NO_ERR) { + fprintf(stderr, "Exception raised for get_document_element\n"); + //dom_node_unref(doc); + return; + } else if (root == NULL) { + fprintf(stderr, "Broken: root == NULL\n"); + //dom_node_unref(doc); + return; + } + struct string text; + + if (!init_string(&text)) { + dom_node_unref(root); + return; + } + + if (walk_tree(NULL, NULL, &text, root, true, 0) == false) { + fprintf(stderr, "Failed to complete DOM structure dump.\n"); + dom_node_unref(root); + done_string(&text); + //dom_node_unref(doc); + return; + } + + fprintf(stderr, "\n---%s\n", text.source); + done_string(&text); + dom_node_unref(root); +#endif +} + void dump_xhtml(struct cache_entry *cached, struct document *document, int parse) { diff --git a/src/document/libdom/renderer2.h b/src/document/libdom/renderer2.h index 4ae8aea0..cfef1c8d 100644 --- a/src/document/libdom/renderer2.h +++ b/src/document/libdom/renderer2.h @@ -20,6 +20,7 @@ void render_xhtml_document(struct cache_entry *cached, struct document *document void dump_xhtml(struct cache_entry *cached, struct document *document, int parse); void free_libdom(void); +void debug_dump_xhtml(void *doc); #if 0 void walk2(struct document *document); diff --git a/src/ecmascript/ecmascript.h b/src/ecmascript/ecmascript.h index b6601e67..093960da 100644 --- a/src/ecmascript/ecmascript.h +++ b/src/ecmascript/ecmascript.h @@ -30,8 +30,6 @@ #include "util/string.h" #include "util/time.h" -//#define ECMASCRIPT_DEBUG 1 - #ifdef ECMASCRIPT_DEBUG #include #endif diff --git a/src/ecmascript/spidermonkey.cpp b/src/ecmascript/spidermonkey.cpp index ce016785..5cfcd5d0 100644 --- a/src/ecmascript/spidermonkey.cpp +++ b/src/ecmascript/spidermonkey.cpp @@ -175,12 +175,11 @@ spidermonkey_get_interpreter(struct ecmascript_interpreter *interpreter) JS::Heap *window_obj = new JS::Heap(JS_NewGlobalObject(ctx, &window_class, NULL, JS::FireOnNewGlobalHook, options)); global = window_obj->get(); - interpreter->ar = new JSAutoRealm(ctx, global); if (!global) { goto release_and_fail; } - + interpreter->ar = new JSAutoRealm(ctx, global); interpreter->ac = window_obj; if (!JS::InitRealmStandardClasses(ctx)) { diff --git a/src/ecmascript/spidermonkey/document.cpp b/src/ecmascript/spidermonkey/document.cpp index 9a045b3a..58cf13e0 100644 --- a/src/ecmascript/spidermonkey/document.cpp +++ b/src/ecmascript/spidermonkey/document.cpp @@ -25,6 +25,7 @@ #include "document/document.h" #include "document/forms.h" #include "document/libdom/doc.h" +#include "document/libdom/renderer2.h" #include "document/view.h" #include "ecmascript/css2xpath.h" #include "ecmascript/ecmascript.h" @@ -1197,6 +1198,10 @@ document_write_do(JSContext *ctx, unsigned int argc, JS::Value *rval, int newlin } struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS::GetRealmPrivate(comp); + struct view_state *vs = interpreter->vs; + struct document_view *doc_view = vs->doc_view; + struct document *document = doc_view->document; + JS::CallArgs args = JS::CallArgsFromVp(argc, rval); if (argc >= 1) { @@ -1227,6 +1232,7 @@ document_write_do(JSContext *ctx, unsigned int argc, JS::Value *rval, int newlin } interpreter->changed = 1; interpreter->was_write = 1; + debug_dump_xhtml(document->dom); } } diff --git a/src/ecmascript/spidermonkey/element.cpp b/src/ecmascript/spidermonkey/element.cpp index b436d6c7..fc784bcd 100644 --- a/src/ecmascript/spidermonkey/element.cpp +++ b/src/ecmascript/spidermonkey/element.cpp @@ -2904,7 +2904,6 @@ element_set_property_className(JSContext *ctx, unsigned int argc, JS::Value *vp) } struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS::GetRealmPrivate(comp); - /* This can be called if @obj if not itself an instance of the * appropriate class but has one in its prototype chain. Fail * such calls. */ @@ -2921,6 +2920,8 @@ element_set_property_className(JSContext *ctx, unsigned int argc, JS::Value *vp) if (!vs) { return true; } + struct document_view *doc_view = vs->doc_view; + struct document *document = doc_view->document; dom_node *el = (dom_node *)JS::GetMaybePtrFromReservedSlot(hobj, 0); @@ -2940,6 +2941,7 @@ element_set_property_className(JSContext *ctx, unsigned int argc, JS::Value *vp) exc = dom_element_set_attribute(el, corestring_dom_class, classstr); interpreter->changed = 1; dom_string_unref(classstr); + debug_dump_xhtml(document->dom); } mem_free(str); @@ -2965,7 +2967,6 @@ element_set_property_dir(JSContext *ctx, unsigned int argc, JS::Value *vp) } struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS::GetRealmPrivate(comp); - /* This can be called if @obj if not itself an instance of the * appropriate class but has one in its prototype chain. Fail * such calls. */ @@ -2981,6 +2982,8 @@ element_set_property_dir(JSContext *ctx, unsigned int argc, JS::Value *vp) if (!vs) { return true; } + struct document_view *doc_view = vs->doc_view; + struct document *document = doc_view->document; dom_node *el = (dom_node *)JS::GetMaybePtrFromReservedSlot(hobj, 0); @@ -3002,6 +3005,7 @@ element_set_property_dir(JSContext *ctx, unsigned int argc, JS::Value *vp) exc = dom_element_set_attribute(el, corestring_dom_dir, dir); interpreter->changed = 1; dom_string_unref(dir); + debug_dump_xhtml(document->dom); } } mem_free(str); @@ -3045,6 +3049,8 @@ element_set_property_id(JSContext *ctx, unsigned int argc, JS::Value *vp) if (!vs) { return true; } + struct document_view *doc_view = vs->doc_view; + struct document *document = doc_view->document; dom_node *el = (dom_node *)JS::GetMaybePtrFromReservedSlot(hobj, 0); @@ -3064,6 +3070,7 @@ element_set_property_id(JSContext *ctx, unsigned int argc, JS::Value *vp) exc = dom_element_set_attribute(el, corestring_dom_id, idstr); interpreter->changed = 1; dom_string_unref(idstr); + debug_dump_xhtml(document->dom); } mem_free(str); @@ -3105,6 +3112,8 @@ element_set_property_innerHtml(JSContext *ctx, unsigned int argc, JS::Value *vp) if (!vs) { return true; } + struct document_view *doc_view = vs->doc_view; + struct document *document = doc_view->document; dom_node *el = (dom_node *)JS::GetMaybePtrFromReservedSlot(hobj, 0); @@ -3118,6 +3127,9 @@ element_set_property_innerHtml(JSContext *ctx, unsigned int argc, JS::Value *vp) } size_t size = strlen(s); + fprintf(stderr, "set innerHTML:%s\n", s); + + dom_hubbub_parser_params parse_params; dom_hubbub_error error; dom_hubbub_parser *parser = NULL; @@ -3230,6 +3242,7 @@ out: } mem_free(s); interpreter->changed = 1; + debug_dump_xhtml(document->dom); return true; } @@ -3269,6 +3282,8 @@ element_set_property_innerText(JSContext *ctx, unsigned int argc, JS::Value *vp) if (!vs) { return true; } + struct document_view *doc_view = vs->doc_view; + struct document *document = doc_view->document; dom_node *el = JS::GetMaybePtrFromReservedSlot(hobj, 0); if (!el) { @@ -3286,6 +3301,7 @@ element_set_property_innerText(JSContext *ctx, unsigned int argc, JS::Value *vp) el->add_child_text(text); interpreter->changed = 1; mem_free_if(text); + debug_dump_xhtml(document->dom); #endif return true; @@ -3326,6 +3342,9 @@ element_set_property_lang(JSContext *ctx, unsigned int argc, JS::Value *vp) if (!vs) { return true; } + struct document_view *doc_view = vs->doc_view; + struct document *document = doc_view->document; + dom_node *el = (dom_node *)JS::GetMaybePtrFromReservedSlot(hobj, 0); @@ -3345,6 +3364,7 @@ element_set_property_lang(JSContext *ctx, unsigned int argc, JS::Value *vp) exc = dom_element_set_attribute(el, corestring_dom_lang, langstr); interpreter->changed = 1; dom_string_unref(langstr); + debug_dump_xhtml(document->dom); } mem_free(str); @@ -3386,6 +3406,9 @@ element_set_property_outerHtml(JSContext *ctx, unsigned int argc, JS::Value *vp) if (!vs) { return true; } + struct document_view *doc_view = vs->doc_view; + struct document *document = doc_view->document; + dom_node *el = (dom_node *)JS::GetMaybePtrFromReservedSlot(hobj, 0); if (!el) { @@ -3512,6 +3535,7 @@ out: mem_free(s); interpreter->changed = 1; + debug_dump_xhtml(document->dom); return true; } @@ -3589,6 +3613,8 @@ element_set_property_title(JSContext *ctx, unsigned int argc, JS::Value *vp) if (!vs) { return true; } + struct document_view *doc_view = vs->doc_view; + struct document *document = doc_view->document; dom_string *titlestr = NULL; dom_exception exc; @@ -3610,6 +3636,7 @@ element_set_property_title(JSContext *ctx, unsigned int argc, JS::Value *vp) exc = dom_element_set_attribute(el, corestring_dom_title, titlestr); interpreter->changed = 1; dom_string_unref(titlestr); + debug_dump_xhtml(document->dom); } mem_free(str); @@ -3942,6 +3969,9 @@ element_appendChild(JSContext *ctx, unsigned int argc, JS::Value *rval) JS::RootedObject hobj(ctx, &args.thisv().toObject()); struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS::GetRealmPrivate(comp); + struct view_state *vs = interpreter->vs; + struct document_view *doc_view = vs->doc_view; + struct document *document = doc_view->document; if (!JS_InstanceOf(ctx, hobj, &element_class, NULL)) { #ifdef ECMASCRIPT_DEBUG @@ -3972,6 +4002,7 @@ element_appendChild(JSContext *ctx, unsigned int argc, JS::Value *rval) interpreter->changed = 1; JSObject *obj = getElement(ctx, res); args.rval().setObject(*obj); + debug_dump_xhtml(document->dom); return true; } args.rval().setNull(); @@ -4654,6 +4685,9 @@ element_insertBefore(JSContext *ctx, unsigned int argc, JS::Value *rval) } struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS::GetRealmPrivate(comp); + struct view_state *vs = interpreter->vs; + struct document_view *doc_view = vs->doc_view; + struct document *document = doc_view->document; JS::CallArgs args = CallArgsFromVp(argc, rval); JS::RootedObject hobj(ctx, &args.thisv().toObject()); @@ -4691,6 +4725,7 @@ element_insertBefore(JSContext *ctx, unsigned int argc, JS::Value *rval) JSObject *obj = getElement(ctx, spare); args.rval().setObject(*obj); interpreter->changed = 1; + debug_dump_xhtml(document->dom); return true; } @@ -4973,6 +5008,10 @@ element_remove(JSContext *ctx, unsigned int argc, JS::Value *rval) // TODO #if 0 + struct view_state *vs = interpreter->vs; + struct document_view *doc_view = vs->doc_view; + struct document *document = doc_view->document; + xmlpp::Element *el = JS::GetMaybePtrFromReservedSlot(hobj, 0); if (!el) { @@ -4981,6 +5020,7 @@ element_remove(JSContext *ctx, unsigned int argc, JS::Value *rval) xmlpp::Node::remove_node(el); interpreter->changed = 1; + debug_dump_xhtml(document->dom); #endif return true; } @@ -5004,6 +5044,9 @@ element_removeChild(JSContext *ctx, unsigned int argc, JS::Value *rval) JS::RootedObject hobj(ctx, &args.thisv().toObject()); struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS::GetRealmPrivate(comp); + struct view_state *vs = interpreter->vs; + struct document_view *doc_view = vs->doc_view; + struct document *document = doc_view->document; if (!JS_InstanceOf(ctx, hobj, &element_class, NULL)) { #ifdef ECMASCRIPT_DEBUG @@ -5027,6 +5070,7 @@ element_removeChild(JSContext *ctx, unsigned int argc, JS::Value *rval) interpreter->changed = 1; JSObject *obj = getElement(ctx, spare); args.rval().setObject(*obj); + debug_dump_xhtml(document->dom); return true; } args.rval().setNull(); @@ -5060,6 +5104,10 @@ element_replaceWith(JSContext *ctx, unsigned int argc, JS::Value *rval) // TODO #if 0 + struct view_state *vs = interpreter->vs; + struct document_view *doc_view = vs->doc_view; + struct document *document = doc_view->document; + xmlpp::Element *el = JS::GetMaybePtrFromReservedSlot(hobj, 0); if (!el || !args[0].isObject()) { @@ -5074,6 +5122,7 @@ element_replaceWith(JSContext *ctx, unsigned int argc, JS::Value *rval) xmlpp::Node::remove_node(el); interpreter->changed = 1; args.rval().setUndefined(); + debug_dump_xhtml(document->dom); #endif return true; } @@ -5097,6 +5146,9 @@ element_setAttribute(JSContext *ctx, unsigned int argc, JS::Value *rval) JS::RootedObject hobj(ctx, &args.thisv().toObject()); struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS::GetRealmPrivate(comp); + struct view_state *vs = interpreter->vs; + struct document_view *doc_view = vs->doc_view; + struct document *document = doc_view->document; if (!JS_InstanceOf(ctx, hobj, &element_class, NULL)) { #ifdef ECMASCRIPT_DEBUG @@ -5152,6 +5204,7 @@ element_setAttribute(JSContext *ctx, unsigned int argc, JS::Value *rval) return true; } interpreter->changed = 1; + debug_dump_xhtml(document->dom); return true; } diff --git a/src/ecmascript/spidermonkey/style.cpp b/src/ecmascript/spidermonkey/style.cpp index bb3641ad..26fea085 100644 --- a/src/ecmascript/spidermonkey/style.cpp +++ b/src/ecmascript/spidermonkey/style.cpp @@ -24,6 +24,7 @@ #include "document/document.h" #include "document/forms.h" #include "document/libdom/corestrings.h" +#include "document/libdom/renderer2.h" #include "document/view.h" #include "ecmascript/ecmascript.h" #include "ecmascript/libdom/dom.h" @@ -638,6 +639,8 @@ style_set_property_cssText(JSContext *ctx, unsigned int argc, JS::Value *vp) return false; } struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS::GetRealmPrivate(comp); + struct document_view *doc_view = interpreter->vs->doc_view; + struct document *document = doc_view->document; /* This can be called if @obj if not itself an instance of the * appropriate class but has one in its prototype chain. Fail @@ -679,6 +682,7 @@ style_set_property_cssText(JSContext *ctx, unsigned int argc, JS::Value *vp) exc = dom_element_set_attribute(el, corestring_dom_style, stylestr); interpreter->changed = 1; dom_string_unref(stylestr); + debug_dump_xhtml(document->dom); } mem_free(res); @@ -702,6 +706,8 @@ style_set_style(JSContext *ctx, unsigned int argc, JS::Value *vp, const char *pr return false; } struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS::GetRealmPrivate(comp); + struct document_view *doc_view = interpreter->vs->doc_view; + struct document *document = doc_view->document; /* This can be called if @obj if not itself an instance of the * appropriate class but has one in its prototype chain. Fail @@ -752,6 +758,7 @@ style_set_style(JSContext *ctx, unsigned int argc, JS::Value *vp, const char *pr exc = dom_element_set_attribute(el, corestring_dom_style, stylestr); interpreter->changed = 1; dom_string_unref(stylestr); + debug_dump_xhtml(document->dom); } mem_free(res); return true; diff --git a/src/setup.h b/src/setup.h index b13516f5..965db6e4 100644 --- a/src/setup.h +++ b/src/setup.h @@ -125,4 +125,6 @@ #define FG_POLL_TIME 500 #define TERMINAL_POLL_TIMEOUT 1000 +//#define ECMASCRIPT_DEBUG 1 + #endif