1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-06-15 23:35:34 +00:00

[spidermonkey] replaceWith

It does not work without assignment in test case, but small progress.
This commit is contained in:
Witold Filipczyk 2022-11-17 21:03:13 +01:00
parent 0bb9593041
commit 72aa206ff7
2 changed files with 52 additions and 6 deletions

View File

@ -2343,6 +2343,7 @@ static bool element_querySelectorAll(JSContext *ctx, unsigned int argc, JS::Valu
static bool element_remove(JSContext *ctx, unsigned int argc, JS::Value *rval);
static bool element_removeChild(JSContext *ctx, unsigned int argc, JS::Value *rval);
static bool element_removeEventListener(JSContext *ctx, unsigned int argc, JS::Value *rval);
static bool element_replaceWith(JSContext *ctx, unsigned int argc, JS::Value *rval);
static bool element_setAttribute(JSContext *ctx, unsigned int argc, JS::Value *rval);
const spidermonkeyFunctionSpec element_funcs[] = {
@ -2365,6 +2366,7 @@ const spidermonkeyFunctionSpec element_funcs[] = {
{ "remove", element_remove, 0 },
{ "removeChild", element_removeChild, 1 },
{ "removeEventListener", element_removeEventListener, 3 },
{ "replaceWith", element_replaceWith, 1 },
{ "setAttribute", element_setAttribute, 2 },
{ NULL }
};
@ -2570,7 +2572,8 @@ element_appendChild(JSContext *ctx, unsigned int argc, JS::Value *rval)
JS::RootedObject node(ctx, &args[0].toObject());
xmlpp::Node *el2 = JS::GetMaybePtrFromReservedSlot<xmlpp::Node>(node, 0);
el->import_node(el2);
el2 = el->import_node(el2);
interpreter->changed = true;
JSObject *obj = getElement(ctx, el2);
@ -3418,6 +3421,50 @@ element_removeChild(JSContext *ctx, unsigned int argc, JS::Value *rval)
return true;
}
static bool
element_replaceWith(JSContext *ctx, unsigned int argc, JS::Value *rval)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
JS::Realm *comp = js::GetContextRealm(ctx);
if (!comp || argc < 1) {
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s %d\n", __FILE__, __FUNCTION__, __LINE__);
#endif
return false;
}
JS::CallArgs args = CallArgsFromVp(argc, rval);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS::GetRealmPrivate(comp);
if (!JS_InstanceOf(ctx, hobj, &element_class, NULL)) {
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s %d\n", __FILE__, __FUNCTION__, __LINE__);
#endif
return false;
}
xmlpp::Element *el = JS::GetMaybePtrFromReservedSlot<xmlpp::Element>(hobj, 0);
if (!el || !args[0].isObject()) {
args.rval().setUndefined();
return true;
}
JS::RootedObject replacement(ctx, &args[0].toObject());
xmlpp::Node *rep = JS::GetMaybePtrFromReservedSlot<xmlpp::Node>(replacement, 0);
xmlAddPrevSibling(el->cobj(), rep->cobj());
xmlpp::Node::remove_node(el);
interpreter->changed = true;
args.rval().setUndefined();
return true;
}
static bool
element_setAttribute(JSContext *ctx, unsigned int argc, JS::Value *rval)
{

View File

@ -1,10 +1,9 @@
<script>
const div = document.createElement("div");
const p = document.createElement("p");
div.appendChild(p);
const span = document.createElement("span");
var div = document.createElement("div");
var p = document.createElement("p");
p = div.appendChild(p); // TODO without assignment
var span = document.createElement("span");
p.replaceWith(span);
alert(div.outerHTML); //'<div><span></span></div>'
</script>