createDocument-namespace-err.js revision eff69b907ef2cd3a9af0351287a929c66f58e3f6
1description("createDocument tests modeled after createElementNS tests from mozilla which were attached to webkit bug 16833");
2
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
8
9function assert(c, m)
10{
11    if (!c)
12        testFailed(m);
13    else
14        testPassed(m);
15}
16
17function stringForExceptionCode(c)
18{
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";
26    }
27    if (exceptionName)
28        return exceptionName; // + "(" + c + ")";
29    return c;
30}
31
32function assertEquals(actual, expect, m)
33{
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);
40    }
41}
42
43var 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"] },
91];
92
93function sourceify(v)
94{
95    switch (typeof v) {
96        case "undefined":
97            return v;
98        case "string":
99            return '"' + v.replace('"', '\\"') + '"';
100        default:
101            return String(v);
102    }
103}
104
105function sourceifyArgs(args)
106{
107    var copy = new Array(args.length);
108    for (var i = 0, sz = args.length; i < sz; i++)
109        copy[i] = sourceify(args[i]);
110
111    return copy.join(", ");
112}
113
114function runNSTests(tests, doc, createFunctionName)
115{
116    for (var i = 0, sz = tests.length; i < sz; i++) {
117        var test = tests[i];
118
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);
125
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);
136        }
137    }
138}
139
140// Moz throws a "Not enough arguments" exception in these, we don't:
141shouldBeEqualToString("document.implementation.createDocument().toString()", "[object Document]");
142shouldBeEqualToString("document.implementation.createDocument(\"http://www.example.com\").toString()", "[object Document]");
143
144runNSTests(allNSTests, document.implementation, "createDocument");
145
146var successfullyParsed = true;
147