Skip to content
Merged
Changes from 1 commit
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
15 changes: 15 additions & 0 deletions src/browser/dom/element.zig
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,12 @@ pub const Element = struct {
_ = opts;
return Animation.constructor(effect, null);
}

pub fn _remove(self: *parser.Element) !void {
const as_node: *parser.Node = @ptrCast(self);
const parent = try parser.nodeParentNode(as_node) orelse return;
_ = try Node._removeChild(parent, as_node);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, I doubt this will update all location where a pointer to the deleted element was stored.
For example:

Image

Code:

document.body.innerHTML = `
  <div class="preference">
    <label for="cheese">Do you like cheese?</label>
    <input type="checkbox" name="cheese" id="cheese" />
  </div>
`;

const label = document.querySelector('label[for="cheese"]');
label.control

const input = document.getElementById('cheese');
if (input) {
  input.remove();
}

label.control

Maybe just leave a TODO

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively elements could have a tombstone flag

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this a widespread issue for any part of the code that removes/moves nodes?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is, I personally prefer leaving breadcrumbs around to help our future debugging selves.

}
};

// Tests
Expand Down Expand Up @@ -767,4 +773,13 @@ test "Browser.DOM.Element" {
.{ "fc; (fc = document.createElement('div')).innerHTML = '<script><\\/script><p>hello</p>'", null },
.{ "fc.outerHTML", "<div><script></script><p>hello</p></div>" },
}, .{});

try runner.testCases(&.{
.{ "const rm = document.createElement('div')", null },
.{ "rm.id = 'to-remove'", null },
.{ "document.getElementsByTagName('body')[0].appendChild(rm)", null },
.{ "document.getElementById('to-remove') != null", "true" },
.{ "rm.remove()", "undefined" },
.{ "document.getElementById('to-remove') != null", "false" },
}, .{});
}