Skip to content

Commit 3c64ed1

Browse files
authored
Merge pull request #899 from lightpanda-io/element_remove
Add element.remove() (needed by reddit)
2 parents aebc877 + 9c0d26b commit 3c64ed1

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/browser/dom/element.zig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,16 @@ pub const Element = struct {
508508
_ = opts;
509509
return Animation.constructor(effect, null);
510510
}
511+
512+
pub fn _remove(self: *parser.Element) !void {
513+
// TODO: This hasn't been tested to make sure all references to this
514+
// node are properly updated. A lot of libdom is lazy and will look
515+
// for related elements JIT by walking the tree, but there could be
516+
// cases in libdom or the Zig WebAPI where this reference is kept
517+
const as_node: *parser.Node = @ptrCast(self);
518+
const parent = try parser.nodeParentNode(as_node) orelse return;
519+
_ = try Node._removeChild(parent, as_node);
520+
}
511521
};
512522

513523
// Tests
@@ -767,4 +777,13 @@ test "Browser.DOM.Element" {
767777
.{ "fc; (fc = document.createElement('div')).innerHTML = '<script><\\/script><p>hello</p>'", null },
768778
.{ "fc.outerHTML", "<div><script></script><p>hello</p></div>" },
769779
}, .{});
780+
781+
try runner.testCases(&.{
782+
.{ "const rm = document.createElement('div')", null },
783+
.{ "rm.id = 'to-remove'", null },
784+
.{ "document.getElementsByTagName('body')[0].appendChild(rm)", null },
785+
.{ "document.getElementById('to-remove') != null", "true" },
786+
.{ "rm.remove()", "undefined" },
787+
.{ "document.getElementById('to-remove') != null", "false" },
788+
}, .{});
770789
}

0 commit comments

Comments
 (0)