dom: add more WPT tests for document

This commit is contained in:
Pierre Tachoire
2023-12-07 17:44:43 +01:00
parent b0c6948848
commit 807b3275a4
11 changed files with 339 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
<!doctype html>
<meta charset=utf-8>
<title>Document.adoptNode</title>
<link rel=help href="https://dom.spec.whatwg.org/#dom-document-adoptnode">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<!--creates an element with local name "x<": --><x<>x</x<>
<script>
test(function() {
var y = document.getElementsByTagName("x<")[0]
var child = y.firstChild
assert_equals(y.parentNode, document.body)
assert_equals(y.ownerDocument, document)
assert_equals(document.adoptNode(y), y)
assert_equals(y.parentNode, null)
assert_equals(y.firstChild, child)
assert_equals(y.ownerDocument, document)
assert_equals(child.ownerDocument, document)
var doc = document.implementation.createDocument(null, null, null)
assert_equals(doc.adoptNode(y), y)
assert_equals(y.parentNode, null)
assert_equals(y.firstChild, child)
assert_equals(y.ownerDocument, doc)
assert_equals(child.ownerDocument, doc)
}, "Adopting an Element called 'x<' should work.")
test(function() {
var x = document.createElement(":good:times:")
assert_equals(document.adoptNode(x), x);
var doc = document.implementation.createDocument(null, null, null)
assert_equals(doc.adoptNode(x), x)
assert_equals(x.parentNode, null)
assert_equals(x.ownerDocument, doc)
}, "Adopting an Element called ':good:times:' should work.")
test(function() {
var doctype = document.doctype;
assert_equals(doctype.parentNode, document)
assert_equals(doctype.ownerDocument, document)
assert_equals(document.adoptNode(doctype), doctype)
assert_equals(doctype.parentNode, null)
assert_equals(doctype.ownerDocument, document)
}, "Explicitly adopting a DocumentType should work.")
test(function() {
var doc = document.implementation.createDocument(null, null, null)
assert_throws_dom("NOT_SUPPORTED_ERR", function() { document.adoptNode(doc) })
}, "Adopting a Document should throw.")
</script>

View File

@@ -0,0 +1,55 @@
<!doctype html>
<meta charset=utf-8>
<title>Document.createAttribute</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=attributes.js></script>
<script src=productions.js></script>
<div id=log>
<script>
var xml_document;
setup(function() {
xml_document = document.implementation.createDocument(null, null, null);
});
invalid_names.forEach(function(name) {
test(function() {
assert_throws_dom("INVALID_CHARACTER_ERR", function() {
document.createAttribute(name, "test");
});
}, "HTML document.createAttribute(" + format_value(name) + ") should throw");
test(function() {
assert_throws_dom("INVALID_CHARACTER_ERR", function() {
xml_document.createAttribute(name, "test");
});
}, "XML document.createAttribute(" + format_value(name) + ") should throw");
});
valid_names.forEach(name => {
test(() => {
let attr = document.createAttribute(name);
attr_is(attr, "", name.toLowerCase(), null, null, name.toLowerCase());
}, `HTML document.createAttribute(${format_value(name)})`);
test(() => {
let attr = xml_document.createAttribute(name);
attr_is(attr, "", name, null, null, name);
}, `XML document.createAttribute(${format_value(name)})`);
});
var tests = ["title", "TITLE", null, undefined];
tests.forEach(function(name) {
test(function() {
var attribute = document.createAttribute(name);
attr_is(attribute, "", String(name).toLowerCase(), null, null, String(name).toLowerCase());
assert_equals(attribute.ownerElement, null);
}, "HTML document.createAttribute(" + format_value(name) + ")");
test(function() {
var attribute = xml_document.createAttribute(name);
attr_is(attribute, "", String(name), null, null, String(name));
assert_equals(attribute.ownerElement, null);
}, "XML document.createAttribute(" + format_value(name) + ")");
});
</script>

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8"/>
<title>document.createCDATASection</title>
<link rel="help" href="https://dom.spec.whatwg.org/#dom-document-createcdatasection"/>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="Document-createComment-createTextNode.js"></script>
</head>
<body>
<script>
"use strict";
test_create("createCDATASection", CDATASection, 4, "#cdata-section");
test(() => {
assert_throws_dom("InvalidCharacterError", () => document.createCDATASection(" ]" + "]> "));
}, "Creating a CDATA section containing the string \"]" + "]>\" must throw");
</script>
</body>
</html>

View File

@@ -0,0 +1,16 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>document.createCDATASection must throw in HTML documents</title>
<link rel="help" href="https://dom.spec.whatwg.org/#dom-document-createcdatasection">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
"use strict";
setup({ single_test: true });
assert_throws_dom("NotSupportedError", () => document.createCDATASection("foo"));
done();
</script>

View File

@@ -0,0 +1,22 @@
function test_create(method, iface, nodeType, nodeName) {
["\u000b", "a -- b", "a-", "-b", null, undefined].forEach(function(value) {
test(function() {
var c = document[method](value);
var expected = String(value);
assert_true(c instanceof iface);
assert_true(c instanceof CharacterData);
assert_true(c instanceof Node);
assert_equals(c.ownerDocument, document);
assert_equals(c.data, expected, "data");
assert_equals(c.nodeValue, expected, "nodeValue");
assert_equals(c.textContent, expected, "textContent");
assert_equals(c.length, expected.length);
assert_equals(c.nodeType, nodeType);
assert_equals(c.nodeName, nodeName);
assert_equals(c.hasChildNodes(), false);
assert_equals(c.childNodes.length, 0);
assert_equals(c.firstChild, null);
assert_equals(c.lastChild, null);
}, method + "(" + format_value(value) + ")");
});
}

View File

@@ -0,0 +1,21 @@
<!DOCTYPE html>
<meta charset=utf-8>
<title>Document.createComment</title>
<link rel=help href="https://dom.spec.whatwg.org/#dom-document-createcomment">
<link rel=help href="https://dom.spec.whatwg.org/#dom-node-ownerdocument">
<link rel=help href="https://dom.spec.whatwg.org/#dom-characterdata-data">
<link rel=help href="https://dom.spec.whatwg.org/#dom-node-nodevalue">
<link rel=help href="https://dom.spec.whatwg.org/#dom-node-textcontent">
<link rel=help href="https://dom.spec.whatwg.org/#dom-characterdata-length">
<link rel=help href="https://dom.spec.whatwg.org/#dom-node-nodetype">
<link rel=help href="https://dom.spec.whatwg.org/#dom-node-haschildnodes">
<link rel=help href="https://dom.spec.whatwg.org/#dom-node-childnodes">
<link rel=help href="https://dom.spec.whatwg.org/#dom-node-firstchild">
<link rel=help href="https://dom.spec.whatwg.org/#dom-node-lastchild">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="Document-createComment-createTextNode.js"></script>
<div id="log"></div>
<script>
test_create("createComment", Comment, 8, "#comment");
</script>

View File

@@ -0,0 +1,15 @@
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Document.createProcessingInstruction in XML documents</title>
<link rel="help" href="https://dom.spec.whatwg.org/#dom-document-createprocessinginstruction"/>
<link rel="help" href="https://dom.spec.whatwg.org/#dom-processinginstruction-target"/>
<link rel="help" href="https://dom.spec.whatwg.org/#dom-characterdata-data"/>
<link rel="help" href="https://dom.spec.whatwg.org/#dom-node-ownerdocument"/>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<div id="log"/>
<script src="Document-createProcessingInstruction.js"/>
</body>
</html>

View File

@@ -0,0 +1,11 @@
<!DOCTYPE html>
<meta charset=utf-8>
<title>Document.createProcessingInstruction in HTML documents</title>
<link rel=help href="https://dom.spec.whatwg.org/#dom-document-createprocessinginstruction">
<link rel=help href="https://dom.spec.whatwg.org/#dom-processinginstruction-target">
<link rel=help href="https://dom.spec.whatwg.org/#dom-characterdata-data">
<link rel=help href="https://dom.spec.whatwg.org/#dom-node-ownerdocument">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id=log></div>
<script src="Document-createProcessingInstruction.js"></script>

View File

@@ -0,0 +1,39 @@
test(function() {
var invalid = [
["A", "?>"],
["\u00B7A", "x"],
["\u00D7A", "x"],
["A\u00D7", "x"],
["\\A", "x"],
["\f", "x"],
[0, "x"],
["0", "x"]
],
valid = [
["xml:fail", "x"],
["A\u00B7A", "x"],
["a0", "x"]
]
for (var i = 0, il = invalid.length; i < il; i++) {
test(function() {
assert_throws_dom("INVALID_CHARACTER_ERR", function() {
document.createProcessingInstruction(invalid[i][0], invalid[i][1])
})
}, "Should throw an INVALID_CHARACTER_ERR for target " +
format_value(invalid[i][0]) + " and data " +
format_value(invalid[i][1]) + ".")
}
for (var i = 0, il = valid.length; i < il; ++i) {
test(function() {
var pi = document.createProcessingInstruction(valid[i][0], valid[i][1]);
assert_equals(pi.target, valid[i][0]);
assert_equals(pi.data, valid[i][1]);
assert_equals(pi.ownerDocument, document);
assert_true(pi instanceof ProcessingInstruction);
assert_true(pi instanceof Node);
}, "Should get a ProcessingInstruction for target " +
format_value(valid[i][0]) + " and data " +
format_value(valid[i][1]) + ".")
}
})

View File

@@ -0,0 +1,21 @@
<!DOCTYPE html>
<meta charset=utf-8>
<title>Document.createTextNode</title>
<link rel=help href="https://dom.spec.whatwg.org/#dom-document-createtextnode">
<link rel=help href="https://dom.spec.whatwg.org/#dom-node-ownerdocument">
<link rel=help href="https://dom.spec.whatwg.org/#dom-characterdata-data">
<link rel=help href="https://dom.spec.whatwg.org/#dom-node-nodevalue">
<link rel=help href="https://dom.spec.whatwg.org/#dom-node-textcontent">
<link rel=help href="https://dom.spec.whatwg.org/#dom-characterdata-length">
<link rel=help href="https://dom.spec.whatwg.org/#dom-node-nodetype">
<link rel=help href="https://dom.spec.whatwg.org/#dom-node-haschildnodes">
<link rel=help href="https://dom.spec.whatwg.org/#dom-node-childnodes">
<link rel=help href="https://dom.spec.whatwg.org/#dom-node-firstchild">
<link rel=help href="https://dom.spec.whatwg.org/#dom-node-lastchild">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="Document-createComment-createTextNode.js"></script>
<div id="log"></div>
<script>
test_create("createTextNode", Text, 3, "#text");
</script>

View File

@@ -0,0 +1,67 @@
<!DOCTYPE html>
<meta charset=utf-8>
<title>Document.importNode</title>
<link rel=help href="https://dom.spec.whatwg.org/#dom-document-importnode">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<script>
test(function() {
var doc = document.implementation.createHTMLDocument("Title");
var div = doc.body.appendChild(doc.createElement("div"));
div.appendChild(doc.createElement("span"));
assert_equals(div.ownerDocument, doc);
assert_equals(div.firstChild.ownerDocument, doc);
var newDiv = document.importNode(div);
assert_equals(div.ownerDocument, doc);
assert_equals(div.firstChild.ownerDocument, doc);
assert_equals(newDiv.ownerDocument, document);
assert_equals(newDiv.firstChild, null);
}, "No 'deep' argument.")
test(function() {
var doc = document.implementation.createHTMLDocument("Title");
var div = doc.body.appendChild(doc.createElement("div"));
div.appendChild(doc.createElement("span"));
assert_equals(div.ownerDocument, doc);
assert_equals(div.firstChild.ownerDocument, doc);
var newDiv = document.importNode(div, undefined);
assert_equals(div.ownerDocument, doc);
assert_equals(div.firstChild.ownerDocument, doc);
assert_equals(newDiv.ownerDocument, document);
assert_equals(newDiv.firstChild, null);
}, "Undefined 'deep' argument.")
test(function() {
var doc = document.implementation.createHTMLDocument("Title");
var div = doc.body.appendChild(doc.createElement("div"));
div.appendChild(doc.createElement("span"));
assert_equals(div.ownerDocument, doc);
assert_equals(div.firstChild.ownerDocument, doc);
var newDiv = document.importNode(div, true);
assert_equals(div.ownerDocument, doc);
assert_equals(div.firstChild.ownerDocument, doc);
assert_equals(newDiv.ownerDocument, document);
assert_equals(newDiv.firstChild.ownerDocument, document);
}, "True 'deep' argument.")
test(function() {
var doc = document.implementation.createHTMLDocument("Title");
var div = doc.body.appendChild(doc.createElement("div"));
div.appendChild(doc.createElement("span"));
assert_equals(div.ownerDocument, doc);
assert_equals(div.firstChild.ownerDocument, doc);
var newDiv = document.importNode(div, false);
assert_equals(div.ownerDocument, doc);
assert_equals(div.firstChild.ownerDocument, doc);
assert_equals(newDiv.ownerDocument, document);
assert_equals(newDiv.firstChild, null);
}, "False 'deep' argument.")
test(function() {
let doc = document.implementation.createHTMLDocument("Title");
doc.body.setAttributeNS("http://example.com/", "p:name", "value");
let originalAttr = doc.body.getAttributeNodeNS("http://example.com/", "name");
let imported = document.importNode(originalAttr, true);
assert_equals(imported.prefix, originalAttr.prefix);
assert_equals(imported.namespaceURI, originalAttr.namespaceURI);
assert_equals(imported.localName, originalAttr.localName);
}, "Import an Attr node with namespace/prefix correctly.");
</script>