Skip to content

armgit / moli

Public Imported from GitHub
0

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

9.7 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 assert_throws_name(expectedName, callback, description) {
  try {
    callback();
  } catch (error) {
    assert_equals(error.name, expectedName, description);
    return;
  }
  throw new Error(description || "expected exception");
}

test(function () {
  const div = document.createElement("div");

  assert_true(div.classList instanceof DOMTokenList);
  assert_equals(div.classList, div.classList);
  assert_equals(div.classList.length, 0);

  div.classList.add("foo");
  assert_equals(div.className, "foo");
  assert_equals(div.classList.length, 1);
  div.classList.add("bar", "baz");
  assert_equals(div.className, "foo bar baz");
  assert_equals(div.classList.length, 3);
  div.classList.add("bar");
  assert_equals(div.className, "foo bar baz");
  assert_equals(div.classList.length, 3);
}, "Element.classList exposes a stable DOMTokenList and add() deduplicates tokens");

test(function () {
  const div = document.createElement("div");
  div.className = "foo bar";

  assert_false(div.classList.contains(""));
  assert_true(div.classList.contains("foo"));
  assert_true(div.classList.contains("bar"));
  assert_false(div.classList.contains("baz"));

  div.classList.remove("foo");
  assert_equals(div.className, "bar");
  div.classList.remove("missing");
  assert_equals(div.className, "bar");
  div.classList.remove("bar");
  assert_equals(div.className, "");
  assert_equals(div.classList.length, 0);
}, "DOMTokenList contains() and remove() reflect the class attribute");

test(function () {
  const div = document.createElement("div");

  assert_true(div.classList.toggle("foo"));
  assert_equals(div.className, "foo");
  assert_false(div.classList.toggle("foo"));
  assert_equals(div.className, "");
  assert_true(div.classList.toggle("bar", true));
  assert_equals(div.className, "bar");
  assert_true(div.classList.toggle("bar", true));
  assert_equals(div.className, "bar");
  assert_false(div.classList.toggle("baz", false));
  assert_equals(div.className, "bar");
}, "DOMTokenList.toggle() supports optional force");

test(function () {
  const div = document.createElement("div");
  div.className = "foo bar baz";

  assert_true(div.classList.replace("bar", "qux"));
  assert_equals(div.className, "foo qux baz");
  assert_false(div.classList.replace("missing", "other"));
  assert_equals(div.className, "foo qux baz");

  div.className = "alpha beta gamma";
  assert_true(div.classList.replace("beta", "gamma"));
  assert_equals(div.className, "alpha gamma");
}, "DOMTokenList.replace() updates matching tokens and preserves set semantics");

test(function () {
  const div = document.createElement("div");

  assert_throws_name("SyntaxError", () => div.classList.add(""));
  assert_throws_name("InvalidCharacterError", () => div.classList.add("foo bar"));
  assert_throws_name("InvalidCharacterError", () => div.classList.add("foo\tbar"));
  assert_throws_name("InvalidCharacterError", () => div.classList.add("foo\nbar"));
  assert_throws_name("SyntaxError", () => div.classList.toggle(""));
  assert_throws_name("InvalidCharacterError", () => div.classList.remove("has space"));
  assert_throws_name("SyntaxError", () => div.classList.replace("", "baz"));
  assert_throws_name("SyntaxError", () => div.classList.replace("foo", ""));
  assert_throws_name("InvalidCharacterError", () => div.classList.replace("foo bar", "baz"));
  assert_throws_name("InvalidCharacterError", () => div.classList.replace("foo", "bar baz"));
}, "DOMTokenList validates empty and whitespace-containing tokens");

test(function () {
  const div = document.createElement("div");

  div.classList.add({ toString() { return "object-token"; } }, undefined);
  assert_equals(div.className, "object-token undefined");

  assert_throws_name("TypeError", () => div.classList.add("before-error", Symbol("bad")));
  assert_false(div.classList.contains("before-error"), "add() should not mutate after variadic conversion failure");

  div.className = "alpha beta";
  assert_throws_name("TypeError", () => div.classList.remove(Symbol("bad")));
  assert_equals(div.className, "alpha beta", "remove() should not mutate after variadic conversion failure");

  assert_throws_name("Error", () => {
    div.classList.add({ toString() { throw new Error("add conversion failed"); } });
  });
  assert_throws_name("Error", () => {
    div.classList.remove({ toString() { throw new Error("remove conversion failed"); } });
  });
}, "DOMTokenList.add() and remove() use WebIDL variadic DOMString conversion");

test(function () {
  const doc = new DOMParser().parseFromString("<html><body class='alpha beta'></body></html>", "text/html");
  const list = doc.body.classList;

  list.add({ toString() { return "detached-token"; } }, undefined);
  assert_equals(doc.body.className, "alpha beta detached-token undefined");

  assert_throws_name("TypeError", () => list.add("before-error", Symbol("bad")));
  assert_false(list.contains("before-error"), "detached add() should not mutate after variadic conversion failure");

  assert_throws_name("TypeError", () => list.remove(Symbol("bad")));
  assert_equals(doc.body.className, "alpha beta detached-token undefined");
}, "Detached DOMTokenList.add() and remove() use WebIDL variadic DOMString conversion");

test(function () {
  const div = document.createElement("div");
  div.className = "alpha beta gamma";

  assert_equals(div.classList.item(0), "alpha");
  assert_equals(div.classList.item(1), "beta");
  assert_equals(div.classList.item(2), "gamma");
  assert_equals(div.classList.item(3), null);
  assert_equals(div.classList.item(-1), null);
  assert_throws_name("TypeError", () => div.classList.item(), "item() should require its index argument");
  assert_equals(div.classList.item(undefined), "alpha", "explicit undefined should convert to zero");
  assert_equals(div.classList.item(null), "alpha", "null should convert to zero");
  assert_equals(div.classList.item("1.9"), "beta", "string decimals should truncate through unsigned long conversion");
  assert_equals(div.classList.item(NaN), "alpha", "NaN should convert to zero");
  assert_equals(div.classList.item(Infinity), "alpha", "Infinity should convert to zero");
  assert_equals(div.classList.item(-4294967295), "beta", "negative values should wrap through unsigned long conversion");
  assert_equals(div.classList.item(4294967297), "beta", "large values should wrap through unsigned long conversion");
  assert_equals(
    div.classList.item({
      valueOf() { return {}; },
      toString() { return "2"; },
    }),
    "gamma",
    "object values should fall back to toString during unsigned long conversion",
  );
  assert_throws_name("Error", () => {
    div.classList.item({ valueOf() { throw new Error("item conversion failed"); } });
  }, "item() should propagate object conversion exceptions");
  assert_throws_name("TypeError", () => div.classList.item(Symbol("index")), "item() should reject Symbol conversion");
  assert_throws_name("TypeError", () => div.classList.item(1n), "item() should reject BigInt conversion");
  assert_equals(div.classList[0], "alpha");
  assert_equals(div.classList[1], "beta");
  assert_equals(div.classList[2], "gamma");
  assert_equals(div.classList[3], undefined);
}, "DOMTokenList exposes item() and indexed access");

test(function () {
  const div = document.createElement("div");
  div.className = "one two three";
  const forEachValues = [];
  const forEachIndexes = [];
  const forEachLists = [];

  div.classList.forEach(function (value, index, list) {
    forEachValues.push(value);
    forEachIndexes.push(index);
    forEachLists.push(list);
  });

  assert_array_equals(Array.from(div.classList), ["one", "two", "three"]);
  assert_array_equals(Array.from(div.classList.keys()), [0, 1, 2]);
  assert_array_equals(Array.from(div.classList.values()), ["one", "two", "three"]);
  assert_array_equals(
    Array.from(div.classList.entries()).map(([index, value]) => index + ":" + value),
    ["0:one", "1:two", "2:three"],
  );
  assert_array_equals(forEachValues, ["one", "two", "three"]);
  assert_array_equals(forEachIndexes, [0, 1, 2]);
  assert_true(forEachLists.every((list) => list === div.classList));
}, "DOMTokenList supports iteration helpers in token order");

test(function () {
  const div = document.createElement("div");

  div.setAttribute("class", "alpha beta");
  assert_equals(div.classList.length, 2);
  assert_true(div.classList.contains("alpha"));
  assert_true(div.classList.contains("beta"));

  div.className = "gamma delta";
  assert_equals(div.classList.length, 2);
  assert_true(div.classList.contains("gamma"));
  assert_false(div.classList.contains("alpha"));

  div.classList.add("epsilon");
  assert_equals(div.className, "gamma delta epsilon");

  div.classList.value = "test1 test2";
  assert_equals(div.classList.value, "test1 test2");
  assert_equals(div.className, "test1 test2");
  assert_equals(div.classList.toString(), "test1 test2");
}, "DOMTokenList stays synchronized with className, class attribute, and value");

test(function () {
  const div = document.createElement("div");

  div.className = "  foo   bar  \t\n  baz  ";
  assert_equals(div.classList.length, 3);
  assert_array_equals(Array.from(div.classList), ["foo", "bar", "baz"]);

  div.className = "foo foo bar";
  assert_equals(div.classList.length, 2);
  assert_equals(div.classList[0], "foo");
  assert_equals(div.classList[1], "bar");
  div.classList.add("foo");
  assert_equals(div.className, "foo bar");

  div.className = "";
  div.classList.add("Foo", "foo", "FOO");
  assert_equals(div.className, "Foo foo FOO");
  assert_equals(div.classList.length, 3);
  assert_false(div.classList.contains("fOO"));
}, "DOMTokenList tokenization deduplicates and remains case-sensitive");
</script>