Skip to content

Commit 4abcd36

Browse files
committed
feat: impl insertAdjacentElement API.
1 parent d15f61a commit 4abcd36

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

bridge/bindings/qjs/dom/element.cc

+25-11
Original file line numberDiff line numberDiff line change
@@ -163,23 +163,37 @@ JSValue Element::insertAdjacentElement(JSContext* ctx, JSValue this_val, int arg
163163
if (argc < 2) {
164164
return JS_ThrowTypeError(ctx, "Failed to execute 'insertAdjacentElement' on 'Element': 2 argument required.");
165165
}
166-
JSValue position = argv[0];
166+
JSValue positionValue = argv[0];
167167
JSValue target = argv[1];
168168

169-
ElementInstance* targetElementInstance = nullptr;
170-
171-
if (JS_IsObject(target)) {
172-
targetElementInstance = static_cast<ElementInstance*>(JS_GetOpaque(target, Element::classId()));
173-
} else if (!JS_IsNull(target)) {
169+
if (!JS_IsNull(target)) {
174170
return JS_ThrowTypeError(ctx, "TypeError: Failed to execute 'insertAdjacentElement' on 'Element': parameter 2 is not of type 'Element'");
175171
}
176172

177-
auto* element = static_cast<ElementInstance*>(JS_GetOpaque(this_val, Element::classId()));
173+
auto* thisElement = static_cast<ElementInstance*>(JS_GetOpaque(this_val, Element::classId()));
174+
auto* newChild = static_cast<NodeInstance*>(JS_GetOpaque(target, Node::classId(target)));
175+
176+
std::string position = jsValueToStdString(ctx, positionValue);
178177

179-
std::string eventTargetId = std::to_string(targetElementInstance->m_eventTargetId);
180-
std::unique_ptr<NativeString> args_01 = stringToNativeString(eventTargetId);
181-
std::unique_ptr<NativeString> args_02 = jsValueToNativeString(ctx, position);
182-
element->m_context->uiCommandBuffer()->addCommand(element->m_eventTargetId, UICommand::insertAdjacentNode, *args_01, *args_02, nullptr);
178+
if (position == "beforebegin") {
179+
if (auto *parent = static_cast<NodeInstance*>(JS_GetOpaque(thisElement->parentNode, Node::classId(thisElement->parentNode)))) {
180+
parent->internalInsertBefore(newChild, thisElement);
181+
}
182+
} else if (position == "afterbegin") {
183+
thisElement->internalInsertBefore(newChild, thisElement->firstChild());
184+
} else if (position == "beforeend") {
185+
thisElement->internalAppendChild(newChild);
186+
} else if (position == "afterend") {
187+
if (auto *parent = static_cast<NodeInstance*>(JS_GetOpaque(thisElement->parentNode, Node::classId(thisElement->parentNode)))) {
188+
JSValue nextSiblingValue = JS_GetPropertyUint32(ctx, parent->childNodes, arrayFindIdx(ctx, parent->childNodes, thisElement->jsObject) + 1);
189+
auto* nextSibling = static_cast<NodeInstance*>(JS_GetOpaque(nextSiblingValue, Node::classId(nextSiblingValue)));
190+
parent->internalInsertBefore(newChild, nextSibling);
191+
JS_FreeValue(ctx, nextSiblingValue);
192+
}
193+
}
194+
std::unique_ptr<NativeString> args_01 = stringToNativeString(std::to_string(thisElement->eventTargetId()));
195+
std::unique_ptr<NativeString> args_02 = jsValueToNativeString(ctx, positionValue);
196+
thisElement->m_context->uiCommandBuffer()->addCommand(thisElement->m_eventTargetId, UICommand::insertAdjacentNode, *args_01, *args_02, nullptr);
183197
}
184198

185199
JSValue Element::getBoundingClientRect(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) {

0 commit comments

Comments
 (0)