Skip to content

armgit / moli

Public Imported from GitHub
0

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

3.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_attr(attr, expectedName) {
  assert_true(attr instanceof Attr, "instanceof Attr");
  assert_equals(attr.name, expectedName, "name");
  assert_equals(attr.localName, expectedName, "localName");
  assert_equals(attr.nodeName, expectedName, "nodeName");
  assert_equals(attr.prefix, null, "prefix");
  assert_equals(attr.namespaceURI, null, "namespaceURI");
  assert_equals(attr.ownerElement, null, "ownerElement");
  assert_equals(attr.value, "", "initial value");
  assert_equals(attr.nodeValue, "", "initial nodeValue");
  assert_equals(attr.textContent, "", "initial textContent");
  assert_equals(attr.nodeType, 2, "nodeType");

  attr.value = "value";
  assert_equals(attr.nodeValue, "value", "nodeValue reflects value");
  assert_equals(attr.textContent, "value", "textContent reflects value");

  attr.nodeValue = "node";
  assert_equals(attr.value, "node", "value reflects nodeValue");
  assert_equals(attr.textContent, "node", "textContent reflects nodeValue");

  attr.textContent = "text";
  assert_equals(attr.value, "text", "value reflects textContent");
  assert_equals(attr.nodeValue, "text", "nodeValue reflects textContent");
}

[
  ["title", "title"],
  ["TITLE", "title"],
  [null, "null"],
  [undefined, "undefined"],
].forEach(function ([name, expectedName]) {
  test(function () {
    assert_attr(document.createAttribute(name), expectedName);
  }, "HTML document.createAttribute(" + JSON.stringify(name) + ")");
});

test(function () {
  const htmlDocument = document.implementation.createHTMLDocument("");
  const xmlDocument = document.implementation.createDocument(null, "root");
  const parsedHtml = new DOMParser().parseFromString("<html><body></body></html>", "text/html");
  const parsedXml = new DOMParser().parseFromString("<root/>", "application/xml");

  assert_attr(htmlDocument.createAttribute("DATA-ID"), "data-id");
  assert_attr(xmlDocument.createAttribute("DATA-ID"), "DATA-ID");
  assert_attr(parsedHtml.createAttribute("DATA-ID"), "data-id");
  assert_attr(parsedXml.createAttribute("DATA-ID"), "DATA-ID");
}, "detached document.createAttribute() applies document-type name casing");

test(function () {
  assert_throws_dom("InvalidCharacterError", function () {
    document.createAttribute("bad name");
  });
  assert_throws_dom("InvalidCharacterError", function () {
    document.implementation.createHTMLDocument("").createAttribute("bad name");
  });
  assert_throws_dom("InvalidCharacterError", function () {
    document.implementation.createDocument(null, "root").createAttribute("bad name");
  });
  assert_throws_dom("InvalidCharacterError", function () {
    new DOMParser().parseFromString("<html></html>", "text/html").createAttribute("bad name");
  });
  assert_throws_dom("InvalidCharacterError", function () {
    new DOMParser().parseFromString("<root/>", "application/xml").createAttribute("bad name");
  });
}, "document.createAttribute() rejects invalid XML attribute names");

test(function () {
  const attr = document.createAttribute("data-value");

  attr.value = null;
  assert_equals(attr.value, "null");

  attr.nodeValue = undefined;
  assert_equals(attr.value, "undefined");

  attr.textContent = { toString: function () { return "text"; } };
  assert_equals(attr.value, "text");

  assert_throws_js(TypeError, function () {
    attr.value = Symbol("value");
  });
  assert_equals(attr.value, "text");

  assert_throws_js(Error, function () {
    attr.nodeValue = { toString: function () { throw new Error("node"); } };
  });
  assert_equals(attr.value, "text");
}, "Attr value accessors use DOMString conversion");
</script>