Skip to content

armgit / moli

Public Imported from GitHub
0

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

6.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>
<body></body>
<script>
function assert_throws_name(expectedName, callback, description) {
  let error = null;
  try {
    callback();
  } catch (thrown) {
    error = thrown;
  }
  assert_true(error !== null, (description || "assert_throws_name") + ": expected an exception");
  assert_equals(error.name, expectedName, (description || "assert_throws_name") + ": unexpected exception name");
}

function withHost(markup, callback) {
  const host = document.createElement("div");
  host.innerHTML = markup;
  document.body.appendChild(host);
  try {
    callback(host);
  } finally {
    host.remove();
  }
}

test(function () {
  const input = document.createElement("input");
  input.type = "date";

  input.value = "2024-02-29";
  assert_equals(input.value, "2024-02-29", "valid leap-day values should be preserved");

  input.value = "2023-02-29";
  assert_equals(input.value, "", "invalid date values should sanitize to empty");
  assert_true(input.validity.valid, "empty non-required date input should remain valid");

  input.value = "2024-1-02";
  assert_equals(input.value, "", "non-normalized date strings should sanitize to empty");

  input.value = "0000-01-01";
  assert_equals(input.value, "", "year zero is not a valid date input value");
}, "date input value setter sanitizes invalid date strings");

test(function () {
  withHost(
    `<input id="date" type="date" value="2023-02-29">`,
    function (host) {
      const input = host.querySelector("#date");
      assert_equals(input.getAttribute("value"), "2023-02-29", "content attribute should keep the original date text");
      assert_equals(input.value, "", "invalid initial date content value should sanitize to empty");

      input.setAttribute("value", "2024-02-29");
      assert_equals(input.value, "2024-02-29", "non-dirty valid date attribute changes should update the current value");

      input.setAttribute("value", "2024-02-30");
      assert_equals(input.value, "", "non-dirty invalid date attribute changes should sanitize the current value");
    },
  );
}, "date input initial value and non-dirty value attribute sync use date sanitization");

test(function () {
  const input = document.createElement("input");
  input.type = "text";
  input.value = "not-a-date";
  assert_equals(input.value, "not-a-date", "text input should keep the script-set value");

  input.type = "date";
  assert_equals(input.value, "", "switching to date should sanitize the existing value");

  input.type = "text";
  assert_equals(input.value, "", "sanitized value should not reappear after switching back to text");
}, "changing an input to type=date sanitizes the current value");

test(function () {
  const input = document.createElement("input");
  input.type = "date";
  input.min = "2024-01-10";
  input.max = "2024-01-20";

  input.value = "2024-01-09";
  assert_true(input.validity.rangeUnderflow, "date below min should set rangeUnderflow");
  assert_false(input.validity.rangeOverflow, "date below min should not set rangeOverflow");
  assert_false(input.checkValidity(), "rangeUnderflow should fail checkValidity");

  input.value = "2024-01-21";
  assert_false(input.validity.rangeUnderflow, "date above max should not set rangeUnderflow");
  assert_true(input.validity.rangeOverflow, "date above max should set rangeOverflow");
  assert_false(input.checkValidity(), "rangeOverflow should fail checkValidity");

  input.value = "2024-01-10";
  assert_false(input.validity.rangeUnderflow, "date equal to min should pass");
  assert_false(input.validity.rangeOverflow, "date equal to min should not overflow");
  assert_true(input.validity.valid, "date at min should be valid");

  input.min = "bad";
  input.max = "also-bad";
  input.value = "2024-01-25";
  assert_false(input.validity.rangeUnderflow, "invalid date min should be ignored");
  assert_false(input.validity.rangeOverflow, "invalid date max should be ignored");
  assert_true(input.validity.valid, "invalid date range attributes should not make the control invalid");
}, "date input validity reflects min/max range constraints");

test(function () {
  const input = document.createElement("input");
  input.type = "date";
  input.min = "2024-01-01";
  input.step = "2";

  input.value = "2024-01-03";
  assert_false(input.validity.stepMismatch, "date aligned to min + n * step days should pass");
  assert_true(input.checkValidity(), "aligned date should pass checkValidity");

  input.value = "2024-01-04";
  assert_true(input.validity.stepMismatch, "date not aligned to min + n * step days should set stepMismatch");
  assert_false(input.validity.valid, "stepMismatch should make the date invalid");

  input.step = "any";
  assert_false(input.validity.stepMismatch, "date step=any should disable stepMismatch");
  assert_true(input.validity.valid, "step=any should make the date valid when no other constraints fail");
}, "date input validity reflects stepMismatch in day units");

test(function () {
  const input = document.createElement("input");
  input.type = "date";

  assert_true(Number.isNaN(input.valueAsNumber), "empty date input should expose NaN");

  input.value = "1970-01-02";
  assert_equals(input.valueAsNumber, 86400000, "date valueAsNumber should expose UTC milliseconds since epoch");

  input.valueAsNumber = 0;
  assert_equals(input.value, "1970-01-01", "date valueAsNumber setter should write the epoch date");

  input.valueAsNumber = 86400000;
  assert_equals(input.value, "1970-01-02", "date valueAsNumber setter should write whole-day offsets");

  input.valueAsNumber = NaN;
  assert_equals(input.value, "", "NaN date valueAsNumber setter should clear the value");
  assert_true(Number.isNaN(input.valueAsNumber), "cleared date input should expose NaN");
}, "date input valueAsNumber getter and setter round-trip UTC day values");

test(function () {
  const input = document.createElement("input");
  input.type = "date";
  input.value = "1970-01-02";

  assert_throws_name("TypeError", function () {
    input.valueAsNumber = Infinity;
  }, "setting date valueAsNumber to Infinity should throw TypeError");
  assert_equals(input.value, "1970-01-02", "failed valueAsNumber setter should not change date value");
}, "date input valueAsNumber setter rejects infinite values");
</script>