Skip to content

armgit / moli

Public Imported from GitHub
0

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

5.5 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_type_error(callback, description) {
  let error = null;
  try {
    callback();
  } catch (caught) {
    error = caught;
  }
  assert_true(error instanceof TypeError, (description || "assert_throws_type_error") + ": expected TypeError");
}

test(function () {
  const error = new DOMException();
  assert_true(error instanceof DOMException);
  assert_equals(error.name, "Error");
  assert_equals(error.message, "");
  assert_equals(error.code, 0);
  assert_equals(error.toString(), "Error");
}, "DOMException default constructor creates an Error-named exception");

test(function () {
  const error = new DOMException("missing node", "NotFoundError");
  assert_equals(error.name, "NotFoundError");
  assert_equals(error.message, "missing node");
  assert_equals(error.code, 8);
  assert_equals(error.toString(), "NotFoundError: missing node");
}, "DOMException constructor preserves message, name, code, and stringification");

test(function () {
  const cases = [
    ["IndexSizeError", 1],
    ["InvalidCharacterError", 5],
    ["InvalidStateError", 11],
    ["SecurityError", 18],
    ["AbortError", 20],
    ["QuotaExceededError", 22],
    ["DataCloneError", 25],
    ["OperationError", 0],
    ["CustomError", 0],
  ];

  for (const entry of cases) {
    const error = new DOMException("message", entry[0]);
    assert_equals(error.name, entry[0]);
    assert_equals(error.code, entry[1]);
  }
}, "DOMException exposes legacy numeric codes for names that have them");

test(function () {
  const constants = [
    ["INDEX_SIZE_ERR", 1],
    ["DOMSTRING_SIZE_ERR", 2],
    ["HIERARCHY_REQUEST_ERR", 3],
    ["WRONG_DOCUMENT_ERR", 4],
    ["INVALID_CHARACTER_ERR", 5],
    ["NO_DATA_ALLOWED_ERR", 6],
    ["NO_MODIFICATION_ALLOWED_ERR", 7],
    ["NOT_FOUND_ERR", 8],
    ["NOT_SUPPORTED_ERR", 9],
    ["INUSE_ATTRIBUTE_ERR", 10],
    ["INVALID_STATE_ERR", 11],
    ["SYNTAX_ERR", 12],
    ["INVALID_MODIFICATION_ERR", 13],
    ["NAMESPACE_ERR", 14],
    ["INVALID_ACCESS_ERR", 15],
    ["VALIDATION_ERR", 16],
    ["TYPE_MISMATCH_ERR", 17],
    ["SECURITY_ERR", 18],
    ["NETWORK_ERR", 19],
    ["ABORT_ERR", 20],
    ["URL_MISMATCH_ERR", 21],
    ["QUOTA_EXCEEDED_ERR", 22],
    ["TIMEOUT_ERR", 23],
    ["INVALID_NODE_TYPE_ERR", 24],
    ["DATA_CLONE_ERR", 25],
  ];
  const instance = new DOMException("x", "InvalidCharacterError");
  for (const [name, value] of constants) {
    assert_equals(DOMException[name], value, "constructor constant " + name);
    assert_equals(DOMException.prototype[name], value, "prototype constant " + name);
    assert_equals(instance[name], value, "instance inherited constant " + name);

    const descriptor = Object.getOwnPropertyDescriptor(DOMException, name);
    assert_false(descriptor.writable, "constructor constant writable " + name);
    assert_true(descriptor.enumerable, "constructor constant enumerable " + name);
    assert_false(descriptor.configurable, "constructor constant configurable " + name);
  }
}, "DOMException exposes legacy constants on constructor and prototype");

test(function () {
  try {
    throw new DOMException("operation failed", "InvalidStateError");
  } catch (error) {
    assert_true(error instanceof DOMException);
    assert_equals(error.name, "InvalidStateError");
    assert_equals(error.message, "operation failed");
    assert_equals(error.code, 11);
  }
}, "DOMException instances preserve their data when thrown and caught");

test(function () {
  const error = new DOMException("missing node", "NotFoundError");
  assert_false(Object.prototype.hasOwnProperty.call(error, "message"));
  assert_false(Object.prototype.hasOwnProperty.call(error, "name"));
  assert_false(Object.prototype.hasOwnProperty.call(error, "code"));
  assert_equals(Object.getOwnPropertyDescriptor(error, "message"), undefined);
  assert_equals(Object.getOwnPropertyDescriptor(error, "name"), undefined);
  assert_equals(Object.getOwnPropertyDescriptor(error, "code"), undefined);
}, "DOMException instances expose message/name/code via inherited accessors");

test(function () {
  const message = Object.getOwnPropertyDescriptor(DOMException.prototype, "message");
  const name = Object.getOwnPropertyDescriptor(DOMException.prototype, "name");
  const code = Object.getOwnPropertyDescriptor(DOMException.prototype, "code");
  assert_equals(typeof message.get, "function");
  assert_equals(message.set, undefined);
  assert_true(message.enumerable);
  assert_true(message.configurable);
  assert_equals(typeof name.get, "function");
  assert_equals(name.set, undefined);
  assert_true(name.enumerable);
  assert_true(name.configurable);
  assert_equals(typeof code.get, "function");
  assert_equals(code.set, undefined);
  assert_true(code.enumerable);
  assert_true(code.configurable);
}, "DOMException prototype exposes accessor descriptors for message/name/code");

test(function () {
  const messageGetter = Object.getOwnPropertyDescriptor(DOMException.prototype, "message").get;
  assert_throws_type_error(function () {
    messageGetter.call({});
  }, "message getter");
  assert_false(Object.prototype.hasOwnProperty.call(DOMException.prototype, "toString"));
  assert_equals(new DOMException("boom", "SyntaxError").toString(), "SyntaxError: boom");
  assert_throws_type_error(function () {
    DOMException.prototype.toString();
  }, "DOMException.prototype.toString");
}, "DOMException uses Error.prototype.toString() and getter brand checks");
</script>