Skip to content

armgit / moli

Public Imported from GitHub
0

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

4.0 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 oldChild = otherParent.appendChild(document.createElement("p"));
  const newChild = document.createElement("span");

  assert_throws_name("NotFoundError", function () {
    parent.replaceChild(newChild, oldChild);
  });
  assert_equals(oldChild.parentNode, otherParent);
  assert_equals(newChild.parentNode, null);
}, "replaceChild() throws NotFoundError when oldChild has a different parent");

test(function () {
  const parent = document.createElement("div");
  const first = document.createElement("p");
  const oldChild = document.createElement("span");
  const last = document.createElement("em");
  const replacement = document.createElement("strong");
  first.id = "first";
  oldChild.id = "old";
  last.id = "last";
  replacement.id = "replacement";
  parent.append(first, oldChild, last);

  assert_equals(parent.replaceChild(replacement, oldChild), oldChild);
  assert_array_equals(ids(parent), ["first", "replacement", "last"]);
  assert_equals(oldChild.parentNode, null);
  assert_equals(replacement.parentNode, parent);
  assert_equals(replacement.previousSibling, first);
  assert_equals(replacement.nextSibling, last);
  assert_equals(first.nextSibling, replacement);
  assert_equals(last.previousSibling, replacement);
}, "replaceChild() replaces and returns the old child");

test(function () {
  const firstParent = document.createElement("div");
  const secondParent = document.createElement("div");
  const moving = document.createElement("p");
  const oldChild = document.createElement("span");
  const sibling = document.createElement("em");
  moving.id = "moving";
  oldChild.id = "old";
  sibling.id = "sibling";
  firstParent.append(moving, sibling);
  secondParent.appendChild(oldChild);

  assert_equals(secondParent.replaceChild(moving, oldChild), oldChild);
  assert_array_equals(ids(firstParent), ["sibling"]);
  assert_array_equals(ids(secondParent), ["moving"]);
  assert_equals(moving.parentNode, secondParent);
  assert_equals(oldChild.parentNode, null);
}, "replaceChild() moves the replacement from its old parent");

test(function () {
  const parent = document.createElement("div");
  const before = document.createElement("before");
  const oldChild = document.createElement("old");
  const after = document.createElement("after");
  const fragment = document.createDocumentFragment();
  const first = document.createElement("p");
  const second = document.createTextNode("text");
  before.id = "before";
  oldChild.id = "old";
  after.id = "after";
  first.id = "first";
  parent.append(before, oldChild, after);
  fragment.append(first, second);

  assert_equals(parent.replaceChild(fragment, oldChild), oldChild);
  assert_array_equals(ids(parent), ["before", "first", "#text", "after"]);
  assert_array_equals(Array.from(fragment.childNodes), []);
  assert_equals(oldChild.parentNode, null);
  assert_equals(first.parentNode, parent);
  assert_equals(second.parentNode, parent);
}, "replaceChild() replaces a child with DocumentFragment children and empties the fragment");

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

  assert_equals(parent.replaceChild(child, child), child);
  assert_array_equals(ids(parent), ["child"]);
  assert_equals(child.parentNode, parent);
}, "replaceChild() with the same new and old child is a no-op that returns the child");
</script>