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

var xmlIframe = document.querySelector('[src="/common/dummy.xml"]');
var xhtmlIframe = document.querySelector('[src="/common/dummy.xhtml"]');

function getWin(desc) {
  if (desc == "HTML document") {
    return window;
  }
  if (desc == "XML document") {
    assert_equals(xmlIframe.contentDocument.documentElement.textContent,
                  "Dummy XML document", "XML document didn't load");
    return xmlIframe.contentWindow;
  }
  if (desc == "XHTML document") {
    assert_equals(xhtmlIframe.contentDocument.documentElement.textContent,
                  "Dummy XHTML document", "XHTML document didn't load");
    return xhtmlIframe.contentWindow;
  }
}


valid.forEach(function(t) {
  ["HTML document", "XML document", "XHTML document"].forEach(function(desc) {
    async_test(function(testObj) {
      window.addEventListener("load", function() {
        testObj.step(function() {
          var win = getWin(desc);
          var doc = win.document;
          var elt = doc.createElement(t)
          assert_true(elt instanceof win.Element, "instanceof Element")
          assert_true(elt instanceof win.Node, "instanceof Node")
          assert_equals(elt.localName,
                        desc == "HTML document" ? toASCIILowercase(String(t))
                                                : String(t),
                        "localName")
          assert_equals(elt.tagName,
                        desc == "HTML document" ? toASCIIUppercase(String(t))
                                                : String(t),
                        "tagName")
          assert_equals(elt.prefix, null, "prefix")
          assert_equals(elt.namespaceURI,
                        desc == "XML document" ? null : HTMLNS, "namespaceURI")
        });
        testObj.done();
      });
    }, "createElement(" + format_value(t) + ") in " + desc);
  });
});
invalid.forEach(function(arg) {
  ["HTML document", "XML document", "XHTML document"].forEach(function(desc) {
    async_test(function(testObj) {
      window.addEventListener("load", function() {
        testObj.step(function() {
          let win = getWin(desc);
          let doc = win.document;
          assert_throws_dom("InvalidCharacterError", win.DOMException,
                            function() { doc.createElement(arg) })
        });
        testObj.done();
      });
    }, "createElement(" + format_value(arg) + ") in " + desc);
  });
});
</script>