blob: 9f551039ba0b898fe29f0594f6ea3300c8e60350 [file] [log] [blame]
eric@webkit.org4e5ae832008-03-22 09:49:58 +00001<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
2<html>
3<head>
mark.lam@apple.comb130a342013-09-07 22:48:36 +00004<script src="../../../resources/js-test-pre.js"></script>
eric@webkit.org4e5ae832008-03-22 09:49:58 +00005</head>
6<body>
ap@apple.com92352d02017-05-24 22:53:00 +00007<script>
8description("createElementNS tests from mozilla, attached to webkit bug 16833");
9
10function assert(c, m)
11{
12 if (!c)
13 testFailed(m);
14 else
15 testPassed(m);
16}
17
18function stringForExceptionCode(c)
19{
20 var exceptionName;
21 switch(c) {
22 case DOMException.INVALID_CHARACTER_ERR:
23 exceptionName = "INVALID_CHARACTER_ERR";
24 break;
25 case DOMException.NAMESPACE_ERR:
26 exceptionName = "NAMESPACE_ERR";
27 }
28 if (exceptionName)
29 return exceptionName; // + "(" + c + ")";
30 return c;
31}
32
33function assertEquals(actual, expect, m)
34{
35 if (actual !== expect) {
36 m += "; expected " + stringForExceptionCode(expect) + ", threw " + stringForExceptionCode(actual);
37 testFailed(m);
38 } else {
39 m += "; threw " + stringForExceptionCode(actual);;
40 testPassed(m);
41 }
42}
43
44var allNSTests = [
45 { args: [undefined, undefined] },
46 { args: [null, undefined] },
47 { args: [undefined, null] },
48 { args: [null, null] },
49 { args: [null, ""], code: 5 },
50 { args: ["", null] },
51 { args: ["", ""], code: 5 },
52 { args: [null, "<div>"], code: 5 },
53 { args: [null, "0div"], code: 5 },
54 { args: [null, "di v"], code: 5 },
55 { args: [null, "di<v"], code: 5 },
56 { args: [null, "-div"], code: 5 },
57 { args: [null, ".div"], code: 5 },
58 { args: ["http://example.com/", "<div>"], code: 5 },
59 { args: ["http://example.com/", "0div"], code: 5 },
60 { args: ["http://example.com/", "di<v"], code: 5 },
61 { args: ["http://example.com/", "-div"], code: 5 },
62 { args: ["http://example.com/", ".div"], code: 5 },
63 { args: [null, ":div"], code: 5 },
64 { args: [null, "div:"], code: 5 },
65 { args: ["http://example.com/", ":div"], code: 5 },
66 { args: ["http://example.com/", "div:"], code: 5 },
67 { args: [null, "d:iv"], code: 14 },
68 { args: [null, "a:b:c"], code: 5, message: "valid XML name, invalid QName" },
69 { args: ["http://example.com/", "a:b:c"], code: 5, message: "valid XML name, invalid QName" },
70 { args: [null, "a::c"], code: 5, message: "valid XML name, invalid QName" },
71 { args: ["http://example.com/", "a::c"], code: 5, message: "valid XML name, invalid QName" },
72 { args: ["http://example.com/", "a:0"], code: 5, message: "valid XML name, not a valid QName" },
73 { args: ["http://example.com/", "0:a"], code: 5, message: "0 at start makes it not a valid XML name" },
74 { args: ["http://example.com/", "a:_"] },
75 { args: ["http://example.com/", "a:\u0BC6"], code: 14,
76 message: "non-ASCII character after colon is CombiningChar, which is " +
77 "NCNameChar but not (Letter | \"_\") so invalid at start of " +
78 "NCName (but still a valid XML name, hence not INVALID_CHARACTER_ERR)" },
79 { args: ["http://example.com/", "\u0BC6:a"], code: 5,
80 message: "non-ASCII character after colon is CombiningChar, which is " +
81 "NCNameChar but not (Letter | \"_\") so invalid at start of " +
82 "NCName (Gecko chooses to throw NAMESPACE_ERR here, but either is valid " +
83 "as this is both an invalid XML name and an invalid QName)" },
84 { args: ["http://example.com/", "a:a\u0BC6"] },
85 { args: ["http://example.com/", "a\u0BC6:a"] },
86 { args: ["http://example.com/", "xml:test"], code: 14, message: "binding xml prefix wrong" },
87 { args: ["http://example.com/", "xmlns:test"], code: 14, message: "binding xmlns prefix wrong" },
88 { args: ["http://www.w3.org/2000/xmlns/", "x:test"], code: 14, message: "binding namespace namespace to wrong prefix" },
89 { args: ["http://www.w3.org/2000/xmlns/", "xmlns:test"] },
90 { args: ["http://www.w3.org/XML/1998/namespace", "xml:test"] },
91 { args: ["http://www.w3.org/XML/1998/namespace", "x:test"] },
92];
93
94var allNoNSTests = [
95 { args: [undefined] },
96 { args: [null] },
97 { args: [""], code: 5 },
98 { args: ["<div>"], code: 5 },
99 { args: ["0div"], code: 5 },
100 { args: ["di v"], code: 5 },
101 { args: ["di<v"], code: 5 },
102 { args: ["-div"], code: 5 },
103 { args: [".div"], code: 5 },
104 { args: [":"], message: "valid XML name, invalid QName" },
105 { args: [":div"], message: "valid XML name, invalid QName" },
106 { args: ["div:"], message: "valid XML name, invalid QName" },
107 { args: ["d:iv"] },
108 { args: ["a:b:c"], message: "valid XML name, invalid QName" },
109 { args: ["a::c"], message: "valid XML name, invalid QName" },
110 { args: ["a::c:"], message: "valid XML name, invalid QName" },
111 { args: ["a:0"], message: "valid XML name, not a valid QName" },
112 { args: ["0:a"], code: 5, message: "0 at start makes it not a valid XML name" },
113 { args: ["a:_"] },
114 { args: ["a:\u0BC6"],
115 message: "non-ASCII character after colon is CombiningChar, which is " +
116 "valid in pre-namespace XML" },
117 { args: ["\u0BC6:a"], code: 5, message: "not a valid start character" },
118 { args: ["a:a\u0BC6"] },
119 { args: ["a\u0BC6:a"] },
120 { args: ["xml:test"] },
121 { args: ["xmlns:test"] },
122 { args: ["x:test"] },
123 { args: ["xmlns:test"] },
124 { args: ["SOAP-ENV:Body"] }, // From Yahoo Mail Beta
125];
126
127function sourceify(v)
128{
129 switch (typeof v) {
130 case "undefined":
131 return v;
132 case "string":
133 return '"' + v.replace('"', '\\"') + '"';
134 default:
135 return String(v);
136 }
137}
138
139function sourceifyArgs(args)
140{
141 var copy = new Array(args.length);
142 for (var i = 0, sz = args.length; i < sz; i++)
143 copy[i] = sourceify(args[i]);
144
145 return copy.join(", ");
146}
147
148function runNSTests(tests, doc, createFunctionName)
149{
150 for (var i = 0, sz = tests.length; i < sz; i++) {
151 var test = tests[i];
152
153 var code = -1;
154 var argStr = sourceifyArgs(test.args);
155 var msg = createFunctionName + "(" + argStr + ")";
156 if ("message" in test)
157 msg += "; " + test.message;
158 try {
159 doc[createFunctionName].apply(doc, test.args);
160 assert(!("code" in test), msg);
161 } catch (e) {
162 assertEquals(e.code, test.code || "expected no exception", msg);
163 }
164 }
165}
166
167shouldThrow("document.createElementNS()", "'TypeError: Not enough arguments'");
168shouldThrow("document.createElementNS(\"http://www.example.com\")", "'TypeError: Not enough arguments'");
169
170debug("HTML tests:")
171runNSTests(allNSTests, document, "createElementNS");
172runNSTests(allNoNSTests, document, "createElement");
173
174debug("XHTML createElement tests:")
175var xhtmlDoc = document.implementation.createDocument("http://www.w3.org/1999/xhtml", "html", null);
176runNSTests(allNoNSTests, xhtmlDoc, "createElement");
177
178debug("XML createElement tests:")
179var xmlDoc = document.implementation.createDocument("http://www.example.com/foo", "example", null);
180runNSTests(allNoNSTests, xmlDoc, "createElement");
181</script>
mark.lam@apple.comb130a342013-09-07 22:48:36 +0000182<script src="../../../resources/js-test-post.js"></script>
eric@webkit.org4e5ae832008-03-22 09:49:58 +0000183</body>
184</html>