DOMImplementation-createDocument.html (7869B)
1 <!doctype html> 2 <meta charset=utf-8> 3 <title>DOMImplementation.createDocument(namespace, qualifiedName, doctype)</title> 4 <link rel=help href="https://dom.spec.whatwg.org/#dom-domimplementation-createdocument"> 5 <link rel=help href="https://dom.spec.whatwg.org/#dom-document-createelementns"> 6 <link rel=help href="https://dom.spec.whatwg.org/#dom-node-nodetype"> 7 <link rel=help href="https://dom.spec.whatwg.org/#dom-document-documentelement"> 8 <link rel=help href="https://dom.spec.whatwg.org/#dom-document-doctype"> 9 <script src="/resources/testharness.js"></script> 10 <script src="/resources/testharnessreport.js"></script> 11 <script src="Document-createElementNS.js"></script> 12 <div id="log"></div> 13 <script> 14 var htmlNamespace = "http://www.w3.org/1999/xhtml" 15 var svgNamespace = "http://www.w3.org/2000/svg" 16 var mathMLNamespace = "http://www.w3.org/1998/Math/MathML" 17 18 // Make DocumentTypes distinct 19 function my_format_value(val) { 20 if (val instanceof DocumentType) { 21 return "DocumentType node <!DOCTYPE " + val.name 22 + (val.publicId ? " " + val.publicId : "") 23 + (val.systemId ? " " + val.systemId : "") 24 + ">"; 25 } 26 return format_value(val); 27 } 28 29 test(function() { 30 var tests = createElementNS_tests.map(function(t) { 31 return [t[0], t[1], null, t[2]] 32 }).concat([ 33 /* Arrays with four elements: 34 * the namespace argument 35 * the qualifiedName argument 36 * the doctype argument 37 * the expected exception, or null if none 38 */ 39 [null, null, false, TypeError], 40 [null, "", null, null], 41 [undefined, null, undefined, null], 42 [undefined, undefined, undefined, null], 43 [undefined, "", undefined, null], 44 ["http://example.com/", null, null, null], 45 ["http://example.com/", "", null, null], 46 ["/", null, null, null], 47 ["/", "", null, null], 48 ["http://www.w3.org/XML/1998/namespace", null, null, null], 49 ["http://www.w3.org/XML/1998/namespace", "", null, null], 50 ["http://www.w3.org/2000/xmlns/", null, null, null], 51 ["http://www.w3.org/2000/xmlns/", "", null, null], 52 ["foo:", null, null, null], 53 ["foo:", "", null, null], 54 [null, null, document.implementation.createDocumentType("foo", "", ""), null], 55 [null, null, document.doctype, null], // This causes a horrible WebKit bug (now fixed in trunk). 56 [null, null, function() { 57 var foo = document.implementation.createDocumentType("bar", "", ""); 58 document.implementation.createDocument(null, null, foo); 59 return foo; 60 }(), null], // DOCTYPE already associated with a document. 61 [null, null, function() { 62 var bar = document.implementation.createDocument(null, null, null); 63 return bar.implementation.createDocumentType("baz", "", ""); 64 }(), null], // DOCTYPE created by a different implementation. 65 [null, null, function() { 66 var bar = document.implementation.createDocument(null, null, null); 67 var magic = bar.implementation.createDocumentType("quz", "", ""); 68 bar.implementation.createDocument(null, null, magic); 69 return magic; 70 }(), null], // DOCTYPE created by a different implementation and already associated with a document. 71 [null, "foo", document.implementation.createDocumentType("foo", "", ""), null], 72 ["foo", null, document.implementation.createDocumentType("foo", "", ""), null], 73 ["foo", "bar", document.implementation.createDocumentType("foo", "", ""), null], 74 [htmlNamespace, "", null, null], 75 [svgNamespace, "", null, null], 76 [mathMLNamespace, "", null, null], 77 [null, "html", null, null], 78 [null, "svg", null, null], 79 [null, "math", null, null], 80 [null, "", document.implementation.createDocumentType("html", 81 "-//W3C//DTD XHTML 1.0 Transitional//EN", 82 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd")], 83 [null, "", document.implementation.createDocumentType("svg", 84 "-//W3C//DTD SVG 1.1//EN", 85 "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd")], 86 [null, "", document.implementation.createDocumentType("math", 87 "-//W3C//DTD MathML 2.0//EN", 88 "http://www.w3.org/Math/DTD/mathml2/mathml2.dtd")], 89 ]) 90 91 tests.forEach(function(t, i) { 92 var namespace = t[0], qualifiedName = t[1], doctype = t[2], expected = t[3] 93 test(function() { 94 if (expected != null) { 95 if (typeof expected == "string") { 96 assert_throws_dom(expected, function() { document.implementation.createDocument(namespace, qualifiedName, doctype) }); 97 } else { 98 assert_throws_js(expected, function() { document.implementation.createDocument(namespace, qualifiedName, doctype) }); 99 } 100 } else { 101 var doc = document.implementation.createDocument(namespace, qualifiedName, doctype) 102 assert_equals(doc.nodeType, Node.DOCUMENT_NODE) 103 assert_equals(doc.nodeType, doc.DOCUMENT_NODE) 104 assert_equals(doc.nodeName, "#document") 105 assert_equals(doc.nodeValue, null) 106 assert_equals(Object.getPrototypeOf(doc), XMLDocument.prototype) 107 var omitRootElement = qualifiedName === null || String(qualifiedName) === "" 108 if (omitRootElement) { 109 assert_equals(doc.documentElement, null) 110 } else { 111 var element = doc.documentElement 112 assert_not_equals(element, null) 113 assert_equals(element.nodeType, Node.ELEMENT_NODE) 114 assert_equals(element.ownerDocument, doc) 115 var qualified = String(qualifiedName) 116 var names = [] 117 var firstColonIndex = qualified.indexOf(":") 118 if (firstColonIndex >= 0) { 119 names = qualifiedName.split(":", 2); 120 } else { 121 names = [null, qualified] 122 } 123 assert_equals(element.prefix, names[0], 'element.prefix') 124 assert_equals(element.localName, names[1], 'element.localName') 125 assert_equals(element.namespaceURI, namespace === undefined || namespace === "" ? null : namespace, 'element.namespaceURI') 126 } 127 if (!doctype) { 128 assert_equals(doc.doctype, null) 129 } else { 130 assert_equals(doc.doctype, doctype) 131 assert_equals(doc.doctype.ownerDocument, doc) 132 } 133 assert_equals(doc.childNodes.length, !omitRootElement + !!doctype) 134 } 135 }, "createDocument test: " + t.map(my_format_value)) 136 137 if (expected === null) { 138 test(function() { 139 var doc = document.implementation.createDocument(namespace, qualifiedName, doctype) 140 assert_equals(doc.location, null) 141 assert_equals(doc.compatMode, "CSS1Compat") 142 assert_equals(doc.characterSet, "UTF-8") 143 assert_equals(doc.contentType, namespace == htmlNamespace ? "application/xhtml+xml" 144 : namespace == svgNamespace ? "image/svg+xml" 145 : "application/xml") 146 assert_equals(doc.URL, "about:blank") 147 assert_equals(doc.documentURI, "about:blank") 148 assert_equals(doc.createElement("DIV").localName, "DIV"); 149 }, "createDocument test: metadata for " + 150 [namespace, qualifiedName, doctype].map(my_format_value)) 151 152 test(function() { 153 var doc = document.implementation.createDocument(namespace, qualifiedName, doctype) 154 assert_equals(doc.characterSet, "UTF-8", "characterSet"); 155 assert_equals(doc.charset, "UTF-8", "charset"); 156 assert_equals(doc.inputEncoding, "UTF-8", "inputEncoding"); 157 }, "createDocument test: characterSet aliases for " + 158 [namespace, qualifiedName, doctype].map(my_format_value)) 159 } 160 }) 161 }) 162 163 test(function() { 164 assert_throws_js(TypeError, function() { 165 document.implementation.createDocument() 166 }, "createDocument() should throw") 167 168 assert_throws_js(TypeError, function() { 169 document.implementation.createDocument('') 170 }, "createDocument('') should throw") 171 }, "createDocument with missing arguments"); 172 </script>