Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add element insert adjacent element API #1257

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions bridge/bindings/qjs/dom/element.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,43 @@ JSValue Element::instanceConstructor(JSContext* ctx, JSValue func_obj, JSValue t
return element->jsObject;
}

JSValue Element::insertAdjacentElement(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) {
if (argc < 2) {
return JS_ThrowTypeError(ctx, "Failed to execute 'insertAdjacentElement' on 'Element': 2 argument required.");
}
JSValue positionValue = argv[0];
JSValue target = argv[1];

if (!JS_IsObject(target)) {
return JS_ThrowTypeError(ctx, "TypeError: Failed to execute 'insertAdjacentElement' on 'Element': parameter 2 is not of type 'Element'");
}

auto* thisElement = static_cast<ElementInstance*>(JS_GetOpaque(this_val, Element::classId()));
auto* newChild = static_cast<NodeInstance*>(JS_GetOpaque(target, Node::classId(target)));

std::string position = jsValueToStdString(ctx, positionValue);

if (position == "beforebegin") {
if (auto* parent = static_cast<NodeInstance*>(JS_GetOpaque(thisElement->parentNode, Node::classId(thisElement->parentNode)))) {
parent->internalInsertBefore(newChild, thisElement);
}
} else if (position == "afterbegin") {
thisElement->internalInsertBefore(newChild, thisElement->firstChild());
} else if (position == "beforeend") {
thisElement->internalAppendChild(newChild);
} else if (position == "afterend") {
if (auto* parent = static_cast<NodeInstance*>(JS_GetOpaque(thisElement->parentNode, Node::classId(thisElement->parentNode)))) {
JSValue nextSiblingValue = JS_GetPropertyUint32(ctx, parent->childNodes, arrayFindIdx(ctx, parent->childNodes, thisElement->jsObject) + 1);
auto* nextSibling = static_cast<NodeInstance*>(JS_GetOpaque(nextSiblingValue, Node::classId(nextSiblingValue)));
parent->internalInsertBefore(newChild, nextSibling);
JS_FreeValue(ctx, nextSiblingValue);
}
}
std::unique_ptr<NativeString> args_01 = stringToNativeString(std::to_string(thisElement->eventTargetId()));
std::unique_ptr<NativeString> args_02 = jsValueToNativeString(ctx, positionValue);
thisElement->m_context->uiCommandBuffer()->addCommand(thisElement->m_eventTargetId, UICommand::insertAdjacentNode, *args_01, *args_02, nullptr);
}

JSValue Element::getBoundingClientRect(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) {
auto element = static_cast<ElementInstance*>(JS_GetOpaque(this_val, Element::classId()));
getDartMethod()->flushUICommand();
Expand Down
2 changes: 2 additions & 0 deletions bridge/bindings/qjs/dom/element.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class Element : public Node {

JSValue instanceConstructor(JSContext* ctx, JSValue func_obj, JSValue this_val, int argc, JSValue* argv) override;

static JSValue insertAdjacentElement(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
static JSValue getBoundingClientRect(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
static JSValue hasAttribute(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
static JSValue setAttribute(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
Expand Down Expand Up @@ -118,6 +119,7 @@ class Element : public Node {
DEFINE_PROTOTYPE_PROPERTY(scrollTop);
DEFINE_PROTOTYPE_PROPERTY(scrollLeft);

DEFINE_PROTOTYPE_FUNCTION(insertAdjacentElement, 2);
DEFINE_PROTOTYPE_FUNCTION(getBoundingClientRect, 0);
DEFINE_PROTOTYPE_FUNCTION(hasAttribute, 1);
DEFINE_PROTOTYPE_FUNCTION(setAttribute, 2);
Expand Down
48 changes: 48 additions & 0 deletions integration_tests/specs/dom/nodes/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* - Element.prototype.toBlob
* - Element.prototype.firstElementChild
* - Element.prototype.lastElementChild
* - Element.prototype.insertAdjacentElement
*/
describe('DOM Element API', () => {
it('should work', () => {
Expand Down Expand Up @@ -101,4 +102,51 @@ describe('DOM Element API', () => {
var target = el.lastElementChild;
expect(target.tagName).toEqual('SPAN');
});

// <!-- beforebegin -->
// <p>
// <!-- afterbegin -->
// foo
// <!-- beforeend -->
// </p>
// <!-- afterend -->
it('insertAdjacentElement should work with beforebegin', () => {
const root = document.createElement('div');
document.body.appendChild(root);

const newElement = document.createElement('p');
root.insertAdjacentElement('beforebegin', newElement);

expect(newElement.parentNode).toEqual(root.parentNode);
});

it('insertAdjacentElement should work with afterbegin', () => {
const root = document.createElement('div');
document.body.appendChild(root);

const newElement = document.createElement('p');
root.insertAdjacentElement('afterbegin', newElement);

expect(newElement.parentNode).toEqual(root);
});

it('insertAdjacentElement should work with afterend', () => {
const root = document.createElement('div');
document.body.appendChild(root);

const newElement = document.createElement('p');
root.insertAdjacentElement('afterend', newElement);

expect(newElement.parentNode).toEqual(root.parentNode);
});

it('insertAdjacentElement should work with beforeend', () => {
const root = document.createElement('div');
document.body.appendChild(root);

const newElement = document.createElement('p');
root.insertAdjacentElement('beforeend', newElement);

expect(newElement.parentNode).toEqual(root);
});
});