Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

2.2 KB
Raw Blame
<!doctype html>
<meta charset="utf-8">
<script src="/wpt/resources/testharness.js"></script>
<script src="/wpt/resources/testharnessreport.js"></script>
<script src="/wpt/resources/moli-wpt-adapter.js"></script>
<script>
function ids(parent) {
  return Array.from(parent.childNodes).map((node) => node.id || "#" + node.nodeValue);
}

function assert_throws_name(name, callback) {
  try {
    callback();
  } catch (error) {
    assert_equals(error.name, name);
    return;
  }
  assert_true(false, "Expected " + name + " to be thrown");
}

test(function () {
  const parent = document.createElement("div");
  const otherParent = document.createElement("div");
  const child = otherParent.appendChild(document.createElement("p"));

  assert_throws_name("NotFoundError", function () {
    parent.removeChild(child);
  });
  assert_equals(child.parentNode, otherParent);
}, "removeChild() throws NotFoundError for a node with a different parent");

test(function () {
  const parent = document.createElement("div");
  const first = document.createElement("p");
  const second = document.createElement("span");
  const third = document.createElement("em");
  first.id = "first";
  second.id = "second";
  third.id = "third";
  parent.append(first, second, third);

  assert_equals(parent.removeChild(second), second);
  assert_array_equals(ids(parent), ["first", "third"]);
  assert_equals(second.parentNode, null);
  assert_equals(second.previousSibling, null);
  assert_equals(second.nextSibling, null);
  assert_equals(first.nextSibling, third);
  assert_equals(third.previousSibling, first);

  assert_equals(parent.removeChild(first), first);
  assert_array_equals(ids(parent), ["third"]);
  assert_equals(first.parentNode, null);
  assert_equals(third.previousSibling, null);
}, "removeChild() removes and returns the selected child");

test(function () {
  const parent = document.createElement("div");
  const children = parent.childNodes;
  const child = parent.appendChild(document.createElement("p"));

  assert_equals(children.length, 1);
  assert_equals(children[0], child);
  assert_equals(parent.removeChild(child), child);
  assert_equals(children.length, 0);
  assert_equals(children[0], undefined);
}, "removeChild() updates an existing live childNodes collection");
</script>