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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
<!DOCTYPE html>
<meta charset=utf-8>
<title>Document.createElementNS</title>
<link rel=help href="https://dom.spec.whatwg.org/#dom-document-createelementns">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="Document-createElementNS.js"></script>
<div id="log"></div>
<iframe src="/common/dummy.xml"></iframe>
<iframe src="/common/dummy.xhtml"></iframe>
<script>
var tests = createElementNS_tests.concat([
/* Arrays with three elements:
* the namespace argument
* the qualifiedName argument
* the expected exception, or null if none
*/
["", "", "INVALID_CHARACTER_ERR"],
[null, "", "INVALID_CHARACTER_ERR"],
[undefined, "", "INVALID_CHARACTER_ERR"],
["http://example.com/", null, null],
["http://example.com/", "", "INVALID_CHARACTER_ERR"],
["/", null, null],
["/", "", "INVALID_CHARACTER_ERR"],
["http://www.w3.org/XML/1998/namespace", null, null],
["http://www.w3.org/XML/1998/namespace", "", "INVALID_CHARACTER_ERR"],
["http://www.w3.org/2000/xmlns/", null, "NAMESPACE_ERR"],
["http://www.w3.org/2000/xmlns/", "", "INVALID_CHARACTER_ERR"],
["foo:", null, null],
["foo:", "", "INVALID_CHARACTER_ERR"],
])
var xmlIframe = document.querySelector('[src="/common/dummy.xml"]');
var xhtmlIframe = document.querySelector('[src="/common/dummy.xhtml"]');
function runTest(t, i, desc) {
async_test(function(testObj) {
window.addEventListener("load", function() {
testObj.step(function() {
var doc;
if (desc == "HTML document") {
doc = document;
} else if (desc == "XML document") {
doc = xmlIframe.contentDocument;
// Make sure we're testing the right document
assert_equals(doc.documentElement.textContent, "Dummy XML document");
} else if (desc == "XHTML document") {
doc = xhtmlIframe.contentDocument;
assert_equals(doc.documentElement.textContent, "Dummy XHTML document");
}
var namespace = t[0], qualifiedName = t[1], expected = t[2]
if (expected != null) {
assert_throws_dom(expected, doc.defaultView.DOMException, function() {doc.createElementNS(namespace, qualifiedName) });
} else {
var element = doc.createElementNS(namespace, qualifiedName)
assert_not_equals(element, null)
assert_equals(element.nodeType, Node.ELEMENT_NODE)
assert_equals(element.nodeType, element.ELEMENT_NODE)
assert_equals(element.nodeValue, null)
assert_equals(element.ownerDocument, doc)
var qualified = String(qualifiedName), names = []
if (qualified.indexOf(":") >= 0) {
names = qualified.split(":", 2)
} else {
names = [null, qualified]
}
assert_equals(element.prefix, names[0])
assert_equals(element.localName, names[1])
assert_equals(element.tagName, qualified)
assert_equals(element.nodeName, qualified)
assert_equals(element.namespaceURI,
namespace === undefined || namespace === "" ? null
: namespace)
}
});
testObj.done();
});
}, "createElementNS test in " + desc + ": " + t.map(format_value))
}
tests.forEach(function(t, i) {
runTest(t, i, "HTML document")
runTest(t, i, "XML document")
runTest(t, i, "XHTML document")
})
test(function() {
var HTMLNS = "http://www.w3.org/1999/xhtml";
var element = document.createElementNS(HTMLNS, "span");
assert_equals(element.namespaceURI, HTMLNS);
assert_equals(element.prefix, null);
assert_equals(element.localName, "span");
assert_equals(element.tagName, "SPAN");
assert_true(element instanceof Node, "Should be a Node");
assert_true(element instanceof Element, "Should be an Element");
assert_true(element instanceof HTMLElement, "Should be an HTMLElement");
assert_true(element instanceof HTMLSpanElement, "Should be an HTMLSpanElement");
}, "Lower-case HTML element without a prefix");
test(function() {
var HTMLNS = "http://www.w3.org/1999/xhtml";
var element = document.createElementNS(HTMLNS, "html:span");
assert_equals(element.namespaceURI, HTMLNS);
assert_equals(element.prefix, "html");
assert_equals(element.localName, "span");
assert_equals(element.tagName, "HTML:SPAN");
assert_true(element instanceof Node, "Should be a Node");
assert_true(element instanceof Element, "Should be an Element");
assert_true(element instanceof HTMLElement, "Should be an HTMLElement");
assert_true(element instanceof HTMLSpanElement, "Should be an HTMLSpanElement");
}, "Lower-case HTML element with a prefix");
test(function() {
var element = document.createElementNS("test", "span");
assert_equals(element.namespaceURI, "test");
assert_equals(element.prefix, null);
assert_equals(element.localName, "span");
assert_equals(element.tagName, "span");
assert_true(element instanceof Node, "Should be a Node");
assert_true(element instanceof Element, "Should be an Element");
assert_false(element instanceof HTMLElement, "Should not be an HTMLElement");
assert_false(element instanceof HTMLSpanElement, "Should not be an HTMLSpanElement");
}, "Lower-case non-HTML element without a prefix");
test(function() {
var element = document.createElementNS("test", "html:span");
assert_equals(element.namespaceURI, "test");
assert_equals(element.prefix, "html");
assert_equals(element.localName, "span");
assert_equals(element.tagName, "html:span");
assert_true(element instanceof Node, "Should be a Node");
assert_true(element instanceof Element, "Should be an Element");
assert_false(element instanceof HTMLElement, "Should not be an HTMLElement");
assert_false(element instanceof HTMLSpanElement, "Should not be an HTMLSpanElement");
}, "Lower-case non-HTML element with a prefix");
test(function() {
var HTMLNS = "http://www.w3.org/1999/xhtml";
var element = document.createElementNS(HTMLNS, "SPAN");
assert_equals(element.namespaceURI, HTMLNS);
assert_equals(element.prefix, null);
assert_equals(element.localName, "SPAN");
assert_equals(element.tagName, "SPAN");
assert_true(element instanceof Node, "Should be a Node");
assert_true(element instanceof Element, "Should be an Element");
assert_true(element instanceof HTMLElement, "Should be an HTMLElement");
assert_true(element instanceof HTMLUnknownElement, "Should be an HTMLUnknownElement");
assert_false(element instanceof HTMLSpanElement, "Should not be an HTMLSpanElement");
}, "Upper-case HTML element without a prefix");
test(function() {
var HTMLNS = "http://www.w3.org/1999/xhtml";
var element = document.createElementNS(HTMLNS, "html:SPAN");
assert_equals(element.namespaceURI, HTMLNS);
assert_equals(element.prefix, "html");
assert_equals(element.localName, "SPAN");
assert_equals(element.tagName, "HTML:SPAN");
assert_true(element instanceof Node, "Should be a Node");
assert_true(element instanceof Element, "Should be an Element");
assert_true(element instanceof HTMLElement, "Should be an HTMLElement");
assert_false(element instanceof HTMLSpanElement, "Should not be an HTMLSpanElement");
}, "Upper-case HTML element with a prefix");
test(function() {
var element = document.createElementNS("test", "SPAN");
assert_equals(element.namespaceURI, "test");
assert_equals(element.prefix, null);
assert_equals(element.localName, "SPAN");
assert_equals(element.tagName, "SPAN");
assert_true(element instanceof Node, "Should be a Node");
assert_true(element instanceof Element, "Should be an Element");
assert_false(element instanceof HTMLElement, "Should not be an HTMLElement");
assert_false(element instanceof HTMLSpanElement, "Should not be an HTMLSpanElement");
}, "Upper-case non-HTML element without a prefix");
test(function() {
var element = document.createElementNS("test", "html:SPAN");
assert_equals(element.namespaceURI, "test");
assert_equals(element.prefix, "html");
assert_equals(element.localName, "SPAN");
assert_equals(element.tagName, "html:SPAN");
assert_true(element instanceof Node, "Should be a Node");
assert_true(element instanceof Element, "Should be an Element");
assert_false(element instanceof HTMLElement, "Should not be an HTMLElement");
assert_false(element instanceof HTMLSpanElement, "Should not be an HTMLSpanElement");
}, "Upper-case non-HTML element with a prefix");
test(function() {
var element = document.createElementNS(null, "span");
assert_equals(element.namespaceURI, null);
assert_equals(element.prefix, null);
assert_equals(element.localName, "span");
assert_equals(element.tagName, "span");
assert_true(element instanceof Node, "Should be a Node");
assert_true(element instanceof Element, "Should be an Element");
assert_false(element instanceof HTMLElement, "Should not be an HTMLElement");
assert_false(element instanceof HTMLSpanElement, "Should not be an HTMLSpanElement");
}, "null namespace");
test(function() {
var element = document.createElementNS(undefined, "span");
assert_equals(element.namespaceURI, null);
assert_equals(element.prefix, null);
assert_equals(element.localName, "span");
assert_equals(element.tagName, "span");
assert_true(element instanceof Node, "Should be a Node");
assert_true(element instanceof Element, "Should be an Element");
assert_false(element instanceof HTMLElement, "Should not be an HTMLElement");
assert_false(element instanceof HTMLSpanElement, "Should not be an HTMLSpanElement");
}, "undefined namespace");
test(function() {
var element = document.createElementNS("", "span");
assert_equals(element.namespaceURI, null);
assert_equals(element.prefix, null);
assert_equals(element.localName, "span");
assert_equals(element.tagName, "span");
assert_true(element instanceof Node, "Should be a Node");
assert_true(element instanceof Element, "Should be an Element");
assert_false(element instanceof HTMLElement, "Should not be an HTMLElement");
assert_false(element instanceof HTMLSpanElement, "Should not be an HTMLSpanElement");
}, "empty string namespace");
</script>
|