Document-createElement.html (4690B)
1 <!DOCTYPE html> 2 <meta charset=utf-8> 3 <title>Document.createElement</title> 4 <link rel=help href="https://dom.spec.whatwg.org/#dom-document-createelement"> 5 <link rel=help href="https://dom.spec.whatwg.org/#dom-element-localname"> 6 <link rel=help href="https://dom.spec.whatwg.org/#dom-element-tagname"> 7 <link rel=help href="https://dom.spec.whatwg.org/#dom-element-prefix"> 8 <link rel=help href="https://dom.spec.whatwg.org/#dom-element-namespaceuri"> 9 <script src="/resources/testharness.js"></script> 10 <script src="/resources/testharnessreport.js"></script> 11 <div id="log"></div> 12 <iframe src="/common/dummy.xml"></iframe> 13 <iframe src="/common/dummy.xhtml"></iframe> 14 <script> 15 function toASCIIUppercase(str) { 16 var diff = "a".charCodeAt(0) - "A".charCodeAt(0); 17 var res = ""; 18 for (var i = 0; i < str.length; ++i) { 19 if ("a" <= str[i] && str[i] <= "z") { 20 res += String.fromCharCode(str.charCodeAt(i) - diff); 21 } else { 22 res += str[i]; 23 } 24 } 25 return res; 26 } 27 function toASCIILowercase(str) { 28 var diff = "a".charCodeAt(0) - "A".charCodeAt(0); 29 var res = ""; 30 for (var i = 0; i < str.length; ++i) { 31 if ("A" <= str[i] && str[i] <= "Z") { 32 res += String.fromCharCode(str.charCodeAt(i) + diff); 33 } else { 34 res += str[i]; 35 } 36 } 37 return res; 38 } 39 var HTMLNS = "http://www.w3.org/1999/xhtml", 40 valid = [ 41 undefined, 42 null, 43 "foo", 44 "f1oo", 45 "foo1", 46 "f\u0BC6", 47 "foo\u0BC6", 48 ":", 49 ":foo", 50 "f:oo", 51 "foo:", 52 "f:o:o", 53 "f::oo", 54 "f::oo:", 55 "foo:0", 56 "foo:_", 57 // combining char after :, invalid QName but valid Name 58 "foo:\u0BC6", 59 "foo:foo\u0BC6", 60 "foo\u0BC6:foo", 61 "xml", 62 "xmlns", 63 "xmlfoo", 64 "xml:foo", 65 "xmlns:foo", 66 "xmlfoo:bar", 67 "svg", 68 "math", 69 "FOO", 70 // Test that non-ASCII chars don't get uppercased/lowercased 71 "mar\u212a", 72 "\u0130nput", 73 "\u0131nput", 74 "\u0300foo", 75 "f}oo", 76 "foo}", 77 "\ufffffoo", 78 "f\uffffoo", 79 "foo\uffff", 80 "f<oo", 81 "\u0300", 82 ], 83 invalid = [ 84 "", 85 "1foo", 86 "1:foo", 87 "fo o", 88 "}foo", 89 "<foo", 90 "foo>", 91 "<foo>", 92 "-foo", 93 ".foo", 94 ] 95 96 var xmlIframe = document.querySelector('[src="/common/dummy.xml"]'); 97 var xhtmlIframe = document.querySelector('[src="/common/dummy.xhtml"]'); 98 99 function getWin(desc) { 100 if (desc == "HTML document") { 101 return window; 102 } 103 if (desc == "XML document") { 104 assert_equals(xmlIframe.contentDocument.documentElement.textContent, 105 "Dummy XML document", "XML document didn't load"); 106 return xmlIframe.contentWindow; 107 } 108 if (desc == "XHTML document") { 109 assert_equals(xhtmlIframe.contentDocument.documentElement.textContent, 110 "Dummy XHTML document", "XHTML document didn't load"); 111 return xhtmlIframe.contentWindow; 112 } 113 } 114 115 116 valid.forEach(function(t) { 117 ["HTML document", "XML document", "XHTML document"].forEach(function(desc) { 118 async_test(function(testObj) { 119 window.addEventListener("load", function() { 120 testObj.step(function() { 121 var win = getWin(desc); 122 var doc = win.document; 123 var elt = doc.createElement(t) 124 assert_true(elt instanceof win.Element, "instanceof Element") 125 assert_true(elt instanceof win.Node, "instanceof Node") 126 assert_equals(elt.localName, 127 desc == "HTML document" ? toASCIILowercase(String(t)) 128 : String(t), 129 "localName") 130 assert_equals(elt.tagName, 131 desc == "HTML document" ? toASCIIUppercase(String(t)) 132 : String(t), 133 "tagName") 134 assert_equals(elt.prefix, null, "prefix") 135 assert_equals(elt.namespaceURI, 136 desc == "XML document" ? null : HTMLNS, "namespaceURI") 137 }); 138 testObj.done(); 139 }); 140 }, "createElement(" + format_value(t) + ") in " + desc); 141 }); 142 }); 143 invalid.forEach(function(arg) { 144 ["HTML document", "XML document", "XHTML document"].forEach(function(desc) { 145 async_test(function(testObj) { 146 window.addEventListener("load", function() { 147 testObj.step(function() { 148 let win = getWin(desc); 149 let doc = win.document; 150 assert_throws_dom("InvalidCharacterError", win.DOMException, 151 function() { doc.createElement(arg) }) 152 }); 153 testObj.done(); 154 }); 155 }, "createElement(" + format_value(arg) + ") in " + desc); 156 }); 157 }); 158 </script>