Skip to content

armgit / moli

Public Imported from GitHub
0

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

3.3 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 reference = otherParent.appendChild(document.createElement("span"));

  assert_throws_name("NotFoundError", function () {
    parent.insertBefore(document.createElement("p"), reference);
  });
}, "insertBefore() throws NotFoundError when reference child has a different parent");

test(function () {
  const parent = document.createElement("div");
  const c1 = document.createElement("p");
  const c2 = document.createElement("span");
  const c3 = document.createElement("em");
  c1.id = "c1";
  c2.id = "c2";
  c3.id = "c3";

  assert_equals(parent.insertBefore(c1, null), c1);
  assert_array_equals(ids(parent), ["c1"]);
  assert_equals(c1.parentNode, parent);

  assert_equals(parent.insertBefore(c2, c1), c2);
  assert_array_equals(ids(parent), ["c2", "c1"]);
  assert_equals(c2.nextSibling, c1);

  assert_equals(parent.insertBefore(c3, c1), c3);
  assert_array_equals(ids(parent), ["c2", "c3", "c1"]);
  assert_equals(c3.previousSibling, c2);
  assert_equals(c3.nextSibling, c1);
}, "insertBefore() inserts before reference child and appends for null reference");

test(function () {
  const firstParent = document.createElement("div");
  const secondParent = document.createElement("div");
  const c1 = document.createElement("p");
  const c2 = document.createElement("span");
  c1.id = "c1";
  c2.id = "c2";

  firstParent.appendChild(c1);
  firstParent.appendChild(c2);
  assert_array_equals(ids(firstParent), ["c1", "c2"]);

  assert_equals(secondParent.insertBefore(c1, null), c1);
  assert_array_equals(ids(firstParent), ["c2"]);
  assert_array_equals(ids(secondParent), ["c1"]);
  assert_equals(c1.parentNode, secondParent);

  assert_equals(secondParent.insertBefore(c2, c1), c2);
  assert_array_equals(ids(firstParent), []);
  assert_array_equals(ids(secondParent), ["c2", "c1"]);
  assert_equals(c2.parentNode, secondParent);
}, "insertBefore() moves existing children across parents");

test(function () {
  const parent = document.createElement("div");
  const before = document.createElement("before");
  const after = document.createElement("after");
  before.id = "before";
  after.id = "after";
  parent.append(before, after);

  const fragment = document.createDocumentFragment();
  const first = document.createElement("p");
  const second = document.createElement("span");
  first.id = "first";
  second.id = "second";
  fragment.append(first, second);

  assert_equals(parent.insertBefore(fragment, after), fragment);
  assert_array_equals(ids(parent), ["before", "first", "second", "after"]);
  assert_array_equals(Array.from(fragment.childNodes), []);
  assert_equals(first.parentNode, parent);
  assert_equals(second.parentNode, parent);
}, "insertBefore() inserts DocumentFragment children and empties the fragment");
</script>