From 2ad19e7e9584f1fcb39409ba006f8528d4dfbf5d Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Fri, 18 Nov 2022 16:43:16 +0100 Subject: [PATCH] [mujs] replaceWith --- src/ecmascript/mujs/element.cpp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/ecmascript/mujs/element.cpp b/src/ecmascript/mujs/element.cpp index 9a8e286e..4589e1a8 100644 --- a/src/ecmascript/mujs/element.cpp +++ b/src/ecmascript/mujs/element.cpp @@ -1,4 +1,4 @@ -/* The QuickJS html element objects implementation. */ +/* The MuJS html element objects implementation. */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -1153,7 +1153,7 @@ mjs_element_appendChild(js_State *J) return; } xmlpp::Node *el2 = static_cast(mjs_getprivate(J, 1)); - el->import_node(el2); + el2 = el->import_node(el2); interpreter->changed = true; mjs_push_element(J, el2); @@ -1632,6 +1632,27 @@ mjs_element_removeChild(js_State *J) js_pushnull(J); } +static void +mjs_element_replaceWith(js_State *J) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)js_getcontext(J); + xmlpp::Element *el = static_cast(mjs_getprivate(J, 0)); + xmlpp::Node *rep = static_cast(mjs_getprivate(J, 1)); + + if (!el || !rep) { + js_pushundefined(J); + return; + } + xmlAddPrevSibling(el->cobj(), rep->cobj()); + xmlpp::Node::remove_node(el); + interpreter->changed = true; + + js_pushundefined(J); +} + static void mjs_element_setAttribute(js_State *J) { @@ -1742,6 +1763,7 @@ mjs_push_element(js_State *J, void *node) addmethod(J, "remove", mjs_element_remove, 0); addmethod(J, "removeChild", mjs_element_removeChild, 1); addmethod(J, "removeEventListener", mjs_element_removeEventListener, 3); + addmethod(J, "replaceWith", mjs_element_replaceWith, 1); addmethod(J, "setAttribute", mjs_element_setAttribute, 2); addmethod(J, "toString", mjs_element_toString, 0);