@@ -163,23 +163,37 @@ JSValue Element::insertAdjacentElement(JSContext* ctx, JSValue this_val, int arg
163
163
if (argc < 2 ) {
164
164
return JS_ThrowTypeError (ctx, " Failed to execute 'insertAdjacentElement' on 'Element': 2 argument required." );
165
165
}
166
- JSValue position = argv[0 ];
166
+ JSValue positionValue = argv[0 ];
167
167
JSValue target = argv[1 ];
168
168
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)) {
174
170
return JS_ThrowTypeError (ctx, " TypeError: Failed to execute 'insertAdjacentElement' on 'Element': parameter 2 is not of type 'Element'" );
175
171
}
176
172
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);
178
177
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 );
183
197
}
184
198
185
199
JSValue Element::getBoundingClientRect (JSContext* ctx, JSValue this_val, int argc, JSValue* argv) {
0 commit comments