Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / dom / DOMImplementation / script-tests / createDocument-namespace-err.js
blobb0291b2cce22c69f192fd1d788ae906db0b1b435
1 description("createDocument tests modeled after createElementNS tests from mozilla which were attached to webkit bug 16833");
3 // document.implementation.createDocument() should throw the same set of errors
4 // as document.createElementNS()
5 // http://www.w3.org/TR/DOM-Level-3-Core/core.html#Level-2-Core-DOM-createDocument
6 // Thus we copied these test cases from:
7 // LayoutTests/fast/dom/Document/resources/createDocument-namespace-err.js
9 function assert(c, m)
11 if (!c)
12 testFailed(m);
13 else
14 testPassed(m);
17 function stringForExceptionCode(c)
19 var exceptionName;
20 switch(c) {
21 case DOMException.INVALID_CHARACTER_ERR:
22 exceptionName = "INVALID_CHARACTER_ERR";
23 break;
24 case DOMException.NAMESPACE_ERR:
25 exceptionName = "NAMESPACE_ERR";
27 if (exceptionName)
28 return exceptionName; // + "(" + c + ")";
29 return c;
32 function assertEquals(actual, expect, m)
34 if (actual !== expect) {
35 m += "; expected " + stringForExceptionCode(expect) + ", threw " + stringForExceptionCode(actual);
36 testFailed(m);
37 } else {
38 m += "; threw " + stringForExceptionCode(actual);;
39 testPassed(m);
43 var allNSTests = [
44 { args: [undefined, undefined] },
45 { args: [null, undefined] },
46 { args: [undefined, null], code: 5 },
47 { args: [null, null], code: 5 },
48 { args: [null, ""], code: 5 },
49 { args: ["", null], code: 5 },
50 { args: ["", ""], code: 5 },
51 { args: [null, "<div>"], code: 5 },
52 { args: [null, "0div"], code: 5 },
53 { args: [null, "di v"], code: 5 },
54 { args: [null, "di<v"], code: 5 },
55 { args: [null, "-div"], code: 5 },
56 { args: [null, ".div"], code: 5 },
57 { args: ["http://example.com/", "<div>"], code: 5 },
58 { args: ["http://example.com/", "0div"], code: 5 },
59 { args: ["http://example.com/", "di<v"], code: 5 },
60 { args: ["http://example.com/", "-div"], code: 5 },
61 { args: ["http://example.com/", ".div"], code: 5 },
62 { args: [null, ":div"], code: 14 },
63 { args: [null, "div:"], code: 14 },
64 { args: ["http://example.com/", ":div"], code: 14 },
65 { args: ["http://example.com/", "div:"], code: 14 },
66 { args: [null, "d:iv"], code: 14 },
67 { args: [null, "a:b:c"], code: 14, message: "valid XML name, invalid QName" },
68 { args: ["http://example.com/", "a:b:c"], code: 14, message: "valid XML name, invalid QName" },
69 { args: [null, "a::c"], code: 14, message: "valid XML name, invalid QName" },
70 { args: ["http://example.com/", "a::c"], code: 14, message: "valid XML name, invalid QName" },
71 { args: ["http://example.com/", "a:0"], code: 5, message: "valid XML name, not a valid QName" },
72 { args: ["http://example.com/", "0:a"], code: 5, message: "0 at start makes it not a valid XML name" },
73 { args: ["http://example.com/", "a:_"] },
74 { args: ["http://example.com/", "a:\u0BC6"], code: 14,
75 message: "non-ASCII character after colon is CombiningChar, which is " +
76 "NCNameChar but not (Letter | \"_\") so invalid at start of " +
77 "NCName (but still a valid XML name, hence not INVALID_CHARACTER_ERR)" },
78 { args: ["http://example.com/", "\u0BC6:a"], code: 5,
79 message: "non-ASCII character after colon is CombiningChar, which is " +
80 "NCNameChar but not (Letter | \"_\") so invalid at start of " +
81 "NCName (Gecko chooses to throw NAMESPACE_ERR here, but either is valid " +
82 "as this is both an invalid XML name and an invalid QName)" },
83 { args: ["http://example.com/", "a:a\u0BC6"] },
84 { args: ["http://example.com/", "a\u0BC6:a"] },
85 { args: ["http://example.com/", "xml:test"], code: 14, message: "binding xml prefix wrong" },
86 { args: ["http://example.com/", "xmlns:test"], code: 14, message: "binding xmlns prefix wrong" },
87 { args: ["http://www.w3.org/2000/xmlns/", "x:test"], code: 14, message: "binding namespace namespace to wrong prefix" },
88 { args: ["http://www.w3.org/2000/xmlns/", "xmlns:test"] },
89 { args: ["http://www.w3.org/XML/1998/namespace", "xml:test"] },
90 { args: ["http://www.w3.org/XML/1998/namespace", "x:test"] },
93 function sourceify(v)
95 switch (typeof v) {
96 case "undefined":
97 return v;
98 case "string":
99 return '"' + v.replace('"', '\\"') + '"';
100 default:
101 return String(v);
105 function sourceifyArgs(args)
107 var copy = new Array(args.length);
108 for (var i = 0, sz = args.length; i < sz; i++)
109 copy[i] = sourceify(args[i]);
111 return copy.join(", ");
114 function runNSTests(tests, doc, createFunctionName)
116 for (var i = 0, sz = tests.length; i < sz; i++) {
117 var test = tests[i];
119 // Gecko throws "undefined" if createDocument isn't
120 // called with 3 arguments. Instead of modifying all
121 // of the values in the arrays above (which were taken from createElementNS tests)
122 // we will instead just hack the args list here.
123 var argsWithExtraLastNull = test.args.slice(); // copy the args arary
124 argsWithExtraLastNull.push(null);
126 var code = -1;
127 var argStr = sourceifyArgs(argsWithExtraLastNull);
128 var msg = createFunctionName + "(" + argStr + ")";
129 if ("message" in test)
130 msg += "; " + test.message;
131 try {
132 doc[createFunctionName].apply(doc, argsWithExtraLastNull);
133 assert(!("code" in test), msg);
134 } catch (e) {
135 assertEquals(e.code, test.code || "expected no exception", msg);
140 shouldThrow("document.implementation.createDocument()", '"TypeError: Failed to execute \'createDocument\' on \'DOMImplementation\': 2 arguments required, but only 0 present."');
141 shouldThrow("document.implementation.createDocument(\"http://www.example.com\")", '"TypeError: Failed to execute \'createDocument\' on \'DOMImplementation\': 2 arguments required, but only 1 present."');
143 runNSTests(allNSTests, document.implementation, "createDocument");