summaryrefslogtreecommitdiffstats
path: root/LayoutTests/fast/dom/DOMImplementation/script-tests/createDocument-namespace-err.js
blob: 6683f8bcdb8a362c0a2bd2287ac36d209b1f8753 (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
description("createDocument tests modeled after createElementNS tests from mozilla which were attached to webkit bug 16833");

// document.implementation.createDocument() should throw the same set of errors
// as document.createElementNS()
// http://www.w3.org/TR/DOM-Level-3-Core/core.html#Level-2-Core-DOM-createDocument
// Thus we copied these test cases from:
// LayoutTests/fast/dom/Document/resources/createDocument-namespace-err.js

function assert(c, m)
{
    if (!c)
        testFailed(m);
    else
        testPassed(m);
}

function stringForExceptionCode(c)
{
    var exceptionName;
    switch(c) {
        case DOMException.INVALID_CHARACTER_ERR:
            exceptionName = "INVALID_CHARACTER_ERR";
            break;
        case DOMException.NAMESPACE_ERR:
            exceptionName = "NAMESPACE_ERR";
    }
    if (exceptionName)
        return exceptionName; // + "(" + c + ")";
    return c;
}

function assertEquals(actual, expect, m)
{
    if (actual !== expect) {
        m += "; expected " + stringForExceptionCode(expect) + ", threw " + stringForExceptionCode(actual);
        testFailed(m);
    } else {
        m += "; threw " + stringForExceptionCode(actual);;
        testPassed(m);
    }
}

var allNSTests = [
   { args: [undefined, undefined] },
   { args: [null, undefined] },
   { args: [undefined, null], code: 5 },
   { args: [null, null], code: 5 },
   { args: [null, ""], code: 5 },
   { args: ["", null], code: 5 },
   { args: ["", ""], code: 5 },
   { args: [null, "<div>"], code: 5 },
   { args: [null, "0div"], code: 5 },
   { args: [null, "di v"], code: 5 },
   { args: [null, "di<v"], code: 5 },
   { args: [null, "-div"], code: 5 },
   { args: [null, ".div"], code: 5 },
   { args: ["http://example.com/", "<div>"], code: 5 },
   { args: ["http://example.com/", "0div"], code: 5 },
   { args: ["http://example.com/", "di<v"], code: 5 },
   { args: ["http://example.com/", "-div"], code: 5 },
   { args: ["http://example.com/", ".div"], code: 5 },
   { args: [null, ":div"], code: 14 },
   { args: [null, "div:"], code: 14 },
   { args: ["http://example.com/", ":div"], code: 14 },
   { args: ["http://example.com/", "div:"], code: 14 },
   { args: [null, "d:iv"], code: 14 },
   { args: [null, "a:b:c"], code: 14, message: "valid XML name, invalid QName" },
   { args: ["http://example.com/", "a:b:c"], code: 14, message: "valid XML name, invalid QName" },
   { args: [null, "a::c"], code: 14, message: "valid XML name, invalid QName" },
   { args: ["http://example.com/", "a::c"], code: 14, message: "valid XML name, invalid QName" },
   { args: ["http://example.com/", "a:0"], code: 5, message: "valid XML name, not a valid QName" },
   { args: ["http://example.com/", "0:a"], code: 5, message: "0 at start makes it not a valid XML name" },
   { args: ["http://example.com/", "a:_"] },
   { args: ["http://example.com/", "a:\u0BC6"], code: 14,
     message: "non-ASCII character after colon is CombiningChar, which is " +
              "NCNameChar but not (Letter | \"_\") so invalid at start of " +
              "NCName (but still a valid XML name, hence not INVALID_CHARACTER_ERR)" },
   { args: ["http://example.com/", "\u0BC6:a"], code: 5,
     message: "non-ASCII character after colon is CombiningChar, which is " +
              "NCNameChar but not (Letter | \"_\") so invalid at start of " +
              "NCName (Gecko chooses to throw NAMESPACE_ERR here, but either is valid " +
              "as this is both an invalid XML name and an invalid QName)" },
   { args: ["http://example.com/", "a:a\u0BC6"] },
   { args: ["http://example.com/", "a\u0BC6:a"] },
   { args: ["http://example.com/", "xml:test"], code: 14, message: "binding xml prefix wrong" },
   { args: ["http://example.com/", "xmlns:test"], code: 14, message: "binding xmlns prefix wrong" },
   { args: ["http://www.w3.org/2000/xmlns/", "x:test"], code: 14, message: "binding namespace namespace to wrong prefix" },
   { args: ["http://www.w3.org/2000/xmlns/", "xmlns:test"] },
   { args: ["http://www.w3.org/XML/1998/namespace", "xml:test"] },
   { args: ["http://www.w3.org/XML/1998/namespace", "x:test"] },
];

function sourceify(v)
{
    switch (typeof v) {
        case "undefined":
            return v;
        case "string":
            return '"' + v.replace('"', '\\"') + '"';
        default:
            return String(v);
    }
}

function sourceifyArgs(args)
{
    var copy = new Array(args.length);
    for (var i = 0, sz = args.length; i < sz; i++)
        copy[i] = sourceify(args[i]);

    return copy.join(", ");
}

function runNSTests(tests, doc, createFunctionName)
{
    for (var i = 0, sz = tests.length; i < sz; i++) {
        var test = tests[i];

        // Gecko throws "undefined" if createDocument isn't
        // called with 3 arguments.  Instead of modifying all
        // of the values in the arrays above (which were taken from createElementNS tests)
        // we will instead just hack the args list here.
        var argsWithExtraLastNull = test.args.slice(); // copy the args arary
        argsWithExtraLastNull.push(null);

        var code = -1;
        var argStr = sourceifyArgs(argsWithExtraLastNull);
        var msg = createFunctionName + "(" + argStr + ")";
        if ("message" in test)
            msg += "; " + test.message;
        try {
            doc[createFunctionName].apply(doc, argsWithExtraLastNull);
            assert(!("code" in test), msg);
        } catch (e) {
            assertEquals(e.code, test.code || "expected no exception", msg);
        }
    }
}

// Moz throws a "Not enough arguments" exception in these, we don't:
shouldBeEqualToString("document.implementation.createDocument().toString()", "[object Document]");
shouldBeEqualToString("document.implementation.createDocument(\"http://www.example.com\").toString()", "[object Document]");

runNSTests(allNSTests, document.implementation, "createDocument");

var successfullyParsed = true;