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