/
jquery.xmlns.js
258 lines (224 loc) · 8.29 KB
/
jquery.xmlns.js
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* jquery.xmlns.js unit tests
*/
(function($){
var ns = {
html: "http://www.w3.org/1999/xhtml",
dc: "http://purl.org/dc/elements/1.1/",
foaf: "http://xmlns.com/foaf/0.1/",
cc: "http://creativecommons.org/ns#",
ex: "http://example.org/",
sioc: "http://rdfs.org/sioc/ns#",
xhv: "http://www.w3.org/1999/xhtml/vocab#",
prism: "http://prism.talis.com/schema#",
xsd: "http://www.w3.org/2001/XMLSchema#",
rdf: "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
xml: 'http://www.w3.org/XML/1998/namespace',
xmlns: 'http://www.w3.org/2000/xmlns/'
}
module("When getting all namespaces");
test("on an element with three namespaces declared on it, including an empty one", function() {
var namespaces = $('html').xmlns();
equals(namespaces.dc, ns.dc);
equals(namespaces.foaf, 'http://www.example.org/foaf');
equals(namespaces[''], ns.html);
equals(namespaces.rdf, ns.rdf);
equals(namespaces.cc, ns.cc);
equals(namespaces.ex, ns.ex);
equals(namespaces.sioc, ns.sioc);
for (n in namespaces) {
if (n !== 'dc' && n !== 'foaf' && n !== '' && n !== 'rdf' && n !== 'cc' && n !== 'ex' && n !== 'sioc' && n !== 'xsd' && n !== 'xml' && n !== 'xmlns') {
ok(false, 'unexpected namespace: ' + n + '=' + namespaces[n]);
}
}
});
test("on an element that inherits namespaces", function() {
var namespaces = $('body').xmlns();
equals(namespaces.dc, ns.dc);
equals(namespaces.foaf, ns.foaf);
equals(namespaces[''], ns.html);
for (n in namespaces) {
if (n !== 'dc' && n !== 'foaf' && n !== '' && n !== 'rdf' && n !== 'cc' && n !== 'ex' && n !== 'sioc' && n !== 'xsd' && n !== 'xml' && n !== 'xmlns') {
ok(false, 'unexpected namespace: ' + n + '=' + namespaces[n]);
}
}
});
test("on a sequence of two elements", function() {
var elems = $('body, html');
var namespaces = elems.xmlns();
equals(namespaces.dc, ns.dc);
// This changed with jQuery 1.3.2
equals(namespaces.foaf, 'http://www.example.org/foaf');
equals(namespaces[''], ns.html);
for (n in namespaces) {
if (n !== 'dc' && n !== 'foaf' && n !== '' && n !== 'rdf' && n !== 'cc' && n !== 'ex' && n !== 'sioc' && n !== 'xsd' && n !== 'xml' && n !== 'xmlns') {
ok(false, 'unexpected namespace: ' + n + '=' + namespaces[n]);
}
}
});
module("When getting a known namespace");
test("on an element that has the namespace declared", function() {
equals($('html').xmlns('dc'), ns.dc);
});
test("on an element that inherits the namespace", function() {
equals($('body').xmlns('dc'), ns.dc);
});
test("on an element that redeclares the namespace", function() {
equals($('body').xmlns('foaf'), ns.foaf);
});
test("when the namespace is a relative URI", function() {
equals('' + $('#relativeUri').xmlns('non'), '' + $.uri('foo'));
});
test("when the context is more than one element", function() {
// This changed in jQuery 1.3.2 to return the elements in the order they appear on the page
equals($('body, html').xmlns('foaf'), 'http://www.example.org/foaf', "it should return the namespace from the first of the elements");
});
test("when the namespace hasn't been declared", function() {
equals($('body').xmlns('lic'), undefined);
});
test("when the namespace is the xml namespace", function () {
equals($('body').xmlns('xml'), 'http://www.w3.org/XML/1998/namespace');
});
test("when the namespace is the xmlns namespace", function () {
equals($('body').xmlns('xmlns'), 'http://www.w3.org/2000/xmlns/');
});
module("When setting a namespace");
test("on an element that doesn't have the default namespace declared", function() {
var namespace = 'http://www.example.org/foo';
equals($('h1').attr('xmlns'), undefined, "when it doesn't have a declaration itself");
$('h1').xmlns('', namespace);
equals($('h1').attr('xmlns'), namespace);
$('h1').removeAttr('xmlns');
});
test("on an element that already has a namespace with that prefix", function() {
var namespace = 'http://www.example.org/foo';
equals($('body').attr('xmlns:foaf'), ns.foaf);
$('body').xmlns('foaf', namespace);
equals($('body').attr('xmlns:foaf'), namespace);
$('body').attr('xmlns:foaf', ns.foaf);
});
test("on multiple elements at the same time", function() {
var namespace = 'http://www.example.org/foo';
$('h2').xmlns('foo', namespace);
ok($('h2').length > 1, "there's more than one element");
for (var i = 0; i < $('h2').length; i += 1) {
equals($('h2').eq(i).attr('xmlns:foo'), namespace);
}
$('h2').removeAttr('xmlns:foo');
});
test("with an object containing the namespace mappings", function() {
var namespaces = { foo: 'http://www.example.org/foo', bar: 'http://www.example.org/bar' };
try {
$('h1').xmlns(namespaces);
equals($('h1').attr('xmlns:foo'), namespaces.foo);
equals($('h1').attr('xmlns:bar'), namespaces.bar);
} catch (e) {
ok(false, "it should not cause an error");
}
$('h1').removeAttr('xmlns:foo');
$('h1').removeAttr('xmlns:bar');
});
module("When removing a namespace declaration");
test("from an element that has it", function() {
$('body').removeXmlns('foaf');
equals($('body').attr('xmlns:foaf'), undefined);
equals($('body').xmlns('foaf'), 'http://www.example.org/foaf', "should inherit namespace as usual");
$('body').xmlns('foaf', ns.foaf);
});
test("from a set of elements", function() {
var namespace = 'http://www.example.org/foo';
$('h2').attr('xmlns:foo', namespace);
$('h2').removeXmlns('foo');
for (var i = 0; i < $('h2').length; i += 1) {
equals($('h2').eq(i).attr('xmlns:foo'), undefined);
}
$('h2').removeAttr('xmlns:foo'); // just in case the removeNamespaces didn't work
});
test("when the namespace doesn't exist", function() {
equals($('h2').xmlns('foo'), undefined);
try {
$('h2').removeXmlns('foo');
ok(true, "it should not cause an error");
} catch (e) {
ok(false, "it should not cause an error");
}
});
test("en-masse using an object", function() {
var namespaces = { foo: 'http://www.example.org/foo', bar: 'http://www.example.org/bar' };
$('#main').attr('xmlns:foo', namespaces.foo);
$('#main').attr('xmlns:bar', namespaces.bar);
$('#main').removeXmlns(namespaces);
equals($('#main').attr('xmlns:foo'), undefined);
equals($('#main').attr('xmlns:bar'), undefined);
});
test("en-masse using an array", function() {
var namespaces = { foo: 'http://www.example.org/foo', bar: 'http://www.example.org/bar' };
$('#main').attr('xmlns:foo', namespaces.foo);
$('#main').attr('xmlns:bar', namespaces.bar);
$('#main').removeXmlns(['foo', 'bar']);
equals($('#main').attr('xmlns:foo'), undefined);
equals($('#main').attr('xmlns:bar'), undefined);
});
module("When resolving QNames");
test("of an element in the default namespace with the default namespace declaration on it", function() {
var qname = $('html').qname();
equals(qname.namespace, ns.html);
equals(qname.localPart, 'html');
equals(qname.prefix, '');
equals(qname.name, 'html');
});
test("of an element in the default namespace that inherits its namespace", function() {
var qname = $('body').qname();
equals(qname.namespace, ns.html);
equals(qname.localPart, 'body');
equals(qname.prefix, '');
equals(qname.name, 'body');
});
test("of an element with a prefix", function() {
var qname;
$('#main').append('<s:svg xmlns:s="http://www.w3.org/2000/svg">...</s:svg>');
try {
qname = $('#main *').qname();
equals(qname.namespace, 'http://www.w3.org/2000/svg');
equals(qname.localPart, 'svg');
equals(qname.prefix, 's');
equals(qname.name, 's:svg');
} catch (e) {
if ($.browser.msie) {
ok(true, "should raise an error");
} else {
ok(false, "should not raise the error " + e);
}
}
$('#main *').remove();
});
test("of a specified QName with a prefix", function() {
var qname = $('body').qname('foaf:img');
equals(qname.namespace, ns.foaf);
equals(qname.localPart, 'img');
equals(qname.prefix, 'foaf');
equals(qname.name, 'foaf:img');
});
test("of a QName whose prefix hasn't been declared", function() {
try {
var qname = $('body').qname('foo:bar');
ok(false, "it should raise an error");
} catch (e) {
ok(true, "it should raise an error");
}
});
test("of a QName that has no prefix and there's no in-scope default namespace declaration", function() {
var qname;
$('html').removeXmlns('');
try {
qname = $('body').qname('bar');
equals(qname.namespace, undefined);
equals(qname.prefix, '');
equals(qname.localPart, 'bar');
equals(qname.name, 'bar');
} catch (e) {
ok(false, "it should not raise an error");
}
$('html').xmlns('', ns.html);
});
})(jQuery);