3
<script src="../../lib/OpenLayers.js"></script>
4
<script type="text/javascript">
7
'<?xml version="1.0"?>' +
8
'<ol:root xmlns="http://namespace.default.net" ' +
9
'xmlns:ol="http://namespace.openlayers.org" ' +
10
'xmlns:ta="http://namespace.testattribute.net">' +
11
'<ol:child ta:attribute="value1" ' +
12
'attribute="value2">' +
15
'<ol:child>junk2<' + '/ol:child>' +
16
'<ol:child>junk3<' + '/ol:child>' +
17
'<element>junk4<' + '/element>' +
18
'<ol:element>junk5<' + '/ol:element>' +
29
function test_Format_XML_constructor(t) {
32
var options = {'foo': 'bar'};
33
var format = new OpenLayers.Format.XML(options);
34
t.ok(format instanceof OpenLayers.Format.XML,
35
"new OpenLayers.Format.XML returns object" );
36
t.eq(format.foo, "bar", "constructor sets options correctly");
37
t.eq(typeof format.read, "function", "format has a read function");
38
t.eq(typeof format.write, "function", "format has a write function");
40
t.ok(!window.ActiveXObject || format.xmldom, "browsers with activeX must have xmldom");
43
t.ok(format.namespaces instanceof Object, "format has namespace object");
44
var namespaces = {"foo": "bar"};
45
format = new OpenLayers.Format.XML({namespaces: namespaces});
46
t.eq(format.namespaces, namespaces, "format.namespaces correctly set in constructor");
48
// test default prefix
49
t.eq(format.defaultPrefix, null, "defaultPrefix is null by default");
50
format = new OpenLayers.Format.XML({defaultPrefix: "foo"});
51
t.eq(format.defaultPrefix, "foo", "defaultPrefix correctly set in constructor");
54
t.ok(format.readers instanceof Object, "format has readers object");
55
var readers = {"foo": "bar"};
56
format = new OpenLayers.Format.XML({readers: readers});
57
t.eq(format.readers, readers, "format.readers correctly set in constructor");
60
t.ok(format.writers instanceof Object, "format has writers object");
61
var writers = {"foo": "bar"};
62
format = new OpenLayers.Format.XML({writers: writers});
63
t.eq(format.writers, writers, "format.writers correctly set in constructor");
66
function test_destroy(t) {
68
var format = new OpenLayers.Format.XML();
70
t.eq(format.xmldom, null, "xmldom set to null for all browsers");
73
function test_Format_XML_read(t) {
75
var format = new OpenLayers.Format.XML();
76
t.plan(format.xmldom ? 10 : 9);
78
var doc = format.read(text);
80
"doc has the correct node type");
81
t.eq(doc.nodeName, "#document",
82
"doc has the correct node name");
83
t.ok(doc.documentElement,
84
"ok to access doc.documentElement");
85
t.xml_eq(doc.documentElement, text,
86
"doc.documentElement correctly read");
88
// read can also be called on the prototype directly
89
doc = OpenLayers.Format.XML.prototype.read(text);
91
"doc has the correct node type");
92
t.eq(doc.nodeName, "#document",
93
"doc has the correct node name");
94
t.ok(doc.documentElement,
95
"ok to access doc.documentElement");
96
t.xml_eq(doc.documentElement, text,
97
"doc.documentElement correctly read");
99
// where appropriate, make sure doc is loaded into xmldom property
101
t.xml_eq(format.xmldom.documentElement, text,
102
"xmldom.documentElement contains equivalent xml");
105
// test equivalence with different namespace alias
107
"<pre1:parent xmlns:pre1='http://namespace'>" +
108
"<pre1:child1>value2</pre1:child1>" +
109
"<pre1:child2 pre1:attr1='foo'>value2</pre1:child2>" +
110
"<pre1:child3 chicken:attr='hot' xmlns:chicken='http://soup'/>" +
113
"<pre2:parent xmlns:pre2='http://namespace'>" +
114
"<pre2:child1>value2</pre2:child1>" +
115
"<pre2:child2 pre2:attr1='foo'>value2</pre2:child2>" +
116
"<pre2:child3 pea:attr='hot' xmlns:pea='http://soup'/>" +
118
var doc1 = format.read(pre1);
119
t.xml_eq(doc1.documentElement, pre2, "read correctly sets namespaces");
123
function test_Format_XML_write(t) {
126
var format = new OpenLayers.Format.XML();
127
var doc = format.read(text);
128
var out = format.write(doc);
129
out = out.replace(/[\r\n]/g, '');
130
out = out.replace( /<\?.*\?>/, '')
131
var expected = text.replace(/<\?.*\?>/, '')
133
"correctly writes an XML DOM doc");
134
var out = format.write(
135
format.getElementsByTagNameNS(doc,
136
"http://namespace.openlayers.org","root")[0]);
137
out = out.replace(/[\r\n]/g, '');
138
out = out.replace( /<\?.*\?>/, '')
140
"correctly writes an XML DOM node");
143
function test_Format_XML_createElementNS(t) {
146
var format = new OpenLayers.Format.XML();
147
var uri = "http://foo.com";
149
var localName = "bar";
150
var qualifiedName = prefix + ":" + name;
151
var node = format.createElementNS(uri, qualifiedName);
152
t.eq(node.nodeType, 1,
153
"node has correct type");
154
t.eq(node.nodeName, qualifiedName,
155
"node has correct qualified name");
156
t.eq(node.prefix, prefix,
157
"node has correct prefix");
158
t.eq(node.namespaceURI, uri,
159
"node has correct namespace uri");
161
var doc = format.read(text);
162
if (doc.importNode) {
163
node = doc.importNode(node, true);
165
t.ok(doc.documentElement.appendChild(node),
166
"node can be appended to a doc root");
169
function test_Format_XML_createTextNode(t) {
172
var format = new OpenLayers.Format.XML();
173
var value = Math.random().toString();
174
var node = format.createTextNode(value);
175
t.eq(node.nodeType, 3,
176
"node has correct type");
177
t.eq(node.nodeName, "#text",
178
"node has correct name");
179
t.eq(node.nodeValue, value,
180
"node has correct value");
182
var doc = format.read(text);
183
if (doc.importNode) {
184
node = doc.importNode(node, true);
186
t.ok(doc.documentElement.appendChild(node),
187
"node can be appended to a doc root");
190
function test_Format_XML_getElementsByTagNameNS(t) {
193
var format = new OpenLayers.Format.XML();
194
var olUri = "http://namespace.openlayers.org";
196
var doc = format.read(text);
197
var nodes = format.getElementsByTagNameNS(doc.documentElement,
199
t.eq(nodes.length, 3,
200
"gets correct number of nodes");
201
var qualifiedName = nodes[0].prefix + ":" + name;
202
t.eq(nodes[0].nodeName, qualifiedName,
203
"first node has correct qualified name");
205
var defaultUri = "http://namespace.default.net";
207
nodes = format.getElementsByTagNameNS(doc.documentElement,
209
t.eq(nodes.length, 1,
210
"gets correct number of nodes in default namespace");
212
var pList = format.getElementsByTagNameNS(doc.documentElement,
214
t.eq(pList.length, 1, "got one ol:p element");
217
var aList = format.getElementsByTagNameNS(p, olUri, "a");
218
t.eq(aList.length, 2, "got two child ol:a elements");
224
function test_Format_XML_getAttributeNodeNS(t) {
227
var format = new OpenLayers.Format.XML();
228
var doc = format.read(text);
229
var olUri = "http://namespace.openlayers.org";
230
var taUri = "http://namespace.testattribute.net";
231
var localNodeName = "child";
232
var localAttrName = "attribute";
233
var nodes = format.getElementsByTagNameNS(doc.documentElement,
234
olUri, localNodeName);
235
var attributeNode = format.getAttributeNodeNS(nodes[0],
236
taUri, localAttrName);
237
var qualifiedName = attributeNode.prefix + ":" + localAttrName;
240
"returns non-null value");
241
t.eq(attributeNode.nodeType, 2,
242
"attribute node has correct type");
243
t.eq(attributeNode.nodeName, qualifiedName,
244
"attribute node has correct qualified name");
245
t.eq(attributeNode.nodeValue, "value1",
246
"attribute node has correct value");
248
var nullAttribute = format.getAttributeNodeNS(nodes[0],
250
t.ok(nullAttribute === null,
251
"returns null for nonexistent attribute");
254
function test_Format_XML_getAttributeNS(t) {
257
var format = new OpenLayers.Format.XML();
258
var doc = format.read(text);
259
var olUri = "http://namespace.openlayers.org";
260
var taUri = "http://namespace.testattribute.net";
261
var localNodeName = "child";
262
var localAttrName = "attribute";
263
var nodes = format.getElementsByTagNameNS(doc.documentElement,
264
olUri, localNodeName);
265
var attributeValue = format.getAttributeNS(nodes[0],
266
taUri, localAttrName);
267
t.eq(attributeValue, "value1",
268
"got correct attribute value");
270
var emptyValue = format.getAttributeNS(nodes[0],
272
t.ok(emptyValue === "",
273
"returns empty string for nonexistent attributes");
276
function test_Format_XML_hasAttributeNS(t) {
279
var format = new OpenLayers.Format.XML();
280
var doc = format.read(text);
281
var olUri = "http://namespace.openlayers.org";
282
var taUri = "http://namespace.testattribute.net";
283
var localNodeName = "child";
284
var localAttrName = "attribute";
285
var nodes = format.getElementsByTagNameNS(doc.documentElement,
286
olUri, localNodeName);
287
var found = format.hasAttributeNS(nodes[0], taUri, localAttrName);
288
t.ok(found === true, "returns true for good attribute");
290
found = format.hasAttributeNS(nodes[0], taUri, "nothing");
291
t.ok(found === false, "returns false for bad attribute");
294
function test_namespaces(t) {
297
var format = new OpenLayers.Format.XML({
299
"def": "http://example.com/default",
300
"foo": "http://example.com/foo",
301
"bar": "http://example.com/bar"
306
// test that prototype has not been altered
307
t.eq(OpenLayers.Format.XML.prototype.namespaces, null,
308
"setting namespaces at construction does not modify prototype");
310
// test that namespaceAlias has been set
311
t.eq(format.namespaceAlias["http://example.com/foo"], "foo",
312
"namespaceAlias mapping has been set");
316
function test_setNamespace(t) {
319
var format = new OpenLayers.Format.XML();
321
// test that namespaces is an object
322
t.ok(format.namespaces instanceof Object, "empty namespace object set");
324
format.setNamespace("foo", "http://example.com/foo");
325
t.eq(format.namespaces["foo"], "http://example.com/foo", "alias -> uri mapping set");
326
t.eq(format.namespaceAlias["http://example.com/foo"], "foo", "uri -> alias mapping set");
330
function test_readChildNodes(t) {
332
var text = "<?xml version='1.0' encoding='UTF-8'?>" +
333
"<container xmlns='http://example.com/foo'>" +
334
"<marker name='my marker 1'>" +
339
"<detail>some text for first marker</detail>" +
340
"<atom:link xmlns:atom='http://www.w3.org/2005/Atom' href='http://host/path/1'/>" +
342
"<marker name='my marker 2'>" +
347
"<detail>some text for second marker</detail>" +
348
"<atom:link xmlns:atom='http://www.w3.org/2005/Atom' href='http://host/path/2'/>" +
353
new OpenLayers.Feature.Vector(
354
new OpenLayers.Geometry.Point(-180, 90),
357
link: 'http://host/path/1',
358
detail: 'some text for first marker'
361
new OpenLayers.Feature.Vector(
362
new OpenLayers.Geometry.Point(180, -90),
365
link: 'http://host/path/2',
366
detail: 'some text for second marker'
371
var format = new OpenLayers.Format.XML({
372
defaultPrefix: "foo",
374
"foo": "http://example.com/foo",
375
"atom": "http://www.w3.org/2005/Atom"
379
"container": function(node, obj) {
381
this.readChildNodes(node, list);
384
"marker": function(node, list) {
385
var feature = new OpenLayers.Feature.Vector();
386
feature.attributes.name = node.getAttribute("name");
387
this.readChildNodes(node, feature);
390
"position": function(node, feature) {
392
this.readChildNodes(node, obj);
393
feature.geometry = new OpenLayers.Geometry.Point(obj.x, obj.y);
395
"lon": function(node, obj) {
396
obj.x = this.getChildValue(node);
398
"lat": function(node, obj) {
399
obj.y = this.getChildValue(node);
401
"detail": function(node, feature) {
402
feature.attributes.detail = this.getChildValue(node);
406
"link": function(node, feature) {
407
feature.attributes.link = node.getAttribute("href");
413
// convert text to document node
414
var doc = format.read(text);
415
// read child nodes to get back some object
416
var obj = format.readChildNodes(doc);
417
// start comparing what we got to what we expect
421
t.eq(got.length, expect.length, "correct number of items parsed");
422
t.eq(got[0].geometry.x, expect[0].geometry.x, "correct x coord parsed for marker 1");
423
t.eq(got[0].geometry.y, expect[0].geometry.y, "correct y coord parsed for marker 1");
424
t.eq(got[0].attributes.name, expect[0].attributes.name, "correct name parsed for marker 1");
425
t.eq(got[0].attributes.detail, expect[0].attributes.detail, "correct detail parsed for marker 1");
426
t.eq(got[0].attributes.link, expect[0].attributes.link, "correct link parsed for marker 1");
427
t.eq(got[1].geometry.x, expect[1].geometry.x, "correct x coord parsed for marker 2");
428
t.eq(got[1].geometry.y, expect[1].geometry.y, "correct y coord parsed for marker 2");
429
t.eq(got[1].attributes.name, expect[1].attributes.name, "correct name parsed for marker 2");
430
t.eq(got[1].attributes.detail, expect[1].attributes.detail, "correct detail parsed for marker 2");
431
t.eq(got[1].attributes.link, expect[1].attributes.link, "correct link parsed for marker 2");
435
function test_writeNode(t) {
438
new OpenLayers.Feature.Vector(
439
new OpenLayers.Geometry.Point(-180, 90),
442
link: 'http://host/path/1',
443
detail: 'some text for first marker'
446
new OpenLayers.Feature.Vector(
447
new OpenLayers.Geometry.Point(180, -90),
450
link: 'http://host/path/2',
451
detail: 'some text for second marker'
456
var expect = "<?xml version='1.0' encoding='UTF-8'?>" +
457
"<container xmlns='http://example.com/foo'>" +
458
"<marker name='my marker 1'>" +
463
"<detail>some text for first marker</detail>" +
464
"<atom:link xmlns:atom='http://www.w3.org/2005/Atom' href='http://host/path/1'/>" +
466
"<marker name='my marker 2'>" +
471
"<detail>some text for second marker</detail>" +
472
"<atom:link xmlns:atom='http://www.w3.org/2005/Atom' href='http://host/path/2'/>" +
476
var format = new OpenLayers.Format.XML({
477
defaultPrefix: "foo",
479
"foo": "http://example.com/foo",
480
"atom": "http://www.w3.org/2005/Atom"
484
"container": function(features) {
485
var node = this.createElementNSPlus("container");
487
for(var i=0; i<features.length; ++i) {
488
feature = features[i];
489
this.writeNode("marker", features[i], node);
493
"marker": function(feature) {
494
var node = this.createElementNSPlus("marker", {
495
attributes: {name: feature.attributes.name}
497
this.writeNode("position", feature.geometry, node);
498
this.writeNode("detail", feature.attributes.detail, node);
499
this.writeNode("atom:link", feature.attributes.link, node);
502
"position": function(geometry) {
503
var node = this.createElementNSPlus("position");
504
this.writeNode("lon", geometry.x, node);
505
this.writeNode("lat", geometry.y, node);
509
return this.createElementNSPlus("lon", {
514
return this.createElementNSPlus("lat", {
518
"detail": function(text) {
519
return this.createElementNSPlus("detail", {
525
"link": function(href) {
526
return this.createElementNSPlus("atom:link", {
527
attributes: {href: href}
536
// test that we get what we expect from writeNode
537
var got = format.writeNode("container", features);
538
t.xml_eq(got, expect, "features correctly written");
541
function test_createElementNSPlus(t) {
543
var format = new OpenLayers.Format.XML({
544
defaultPrefix: "def",
546
"def": "http://example.com/default",
547
"foo": "http://example.com/foo",
548
"bar": "http://example.com/bar"
554
description: "unprefixed name with default options",
555
node: format.createElementNSPlus("FooNode"),
556
expect: "<def:FooNode xmlns:def='http://example.com/default'/>"
558
description: "def prefixed name with default options",
559
node: format.createElementNSPlus("def:FooNode"),
560
expect: "<def:FooNode xmlns:def='http://example.com/default'/>"
562
description: "foo prefixed name with default options",
563
node: format.createElementNSPlus("foo:FooNode"),
564
expect: "<foo:FooNode xmlns:foo='http://example.com/foo'/>"
566
description: "unprefixed name with uri option",
567
node: format.createElementNSPlus("FooNode", {
568
uri: "http://example.com/elsewhere"
570
expect: "<FooNode xmlns='http://example.com/elsewhere'/>"
572
description: "foo prefixed name with uri option (overriding format.namespaces)",
573
node: format.createElementNSPlus("foo:FooNode", {
574
uri: "http://example.com/elsewhere"
576
expect: "<foo:FooNode xmlns:foo='http://example.com/elsewhere'/>"
578
description: "foo prefixed name with attributes option",
579
node: format.createElementNSPlus("foo:FooNode", {
582
"foo:attr1": "namespaced attribute 1",
583
"bar:attr2": "namespaced attribute 2"
586
expect: "<foo:FooNode xmlns:foo='http://example.com/foo' xmlns:bar='http://example.com/bar' id='123' foo:attr1='namespaced attribute 1' bar:attr2='namespaced attribute 2'/>"
588
description: "foo prefixed name with attributes and value options",
589
node: format.createElementNSPlus("foo:FooNode", {
590
attributes: {"id": "123"},
593
expect: "<foo:FooNode xmlns:foo='http://example.com/foo' id='123'>text value<" + "/foo:FooNode>"
597
t.plan(cases.length);
599
for(var i=0; i<cases.length; ++i) {
601
t.xml_eq(test.node, test.expect, test.description);
606
function test_setAttributes(t) {
608
var format = new OpenLayers.Format.XML({
609
defaultPrefix: "def",
611
"def": "http://example.com/default",
612
"foo": "http://example.com/foo",
613
"bar": "http://example.com/bar"
619
description: "unprefixed attribute",
620
node: format.createElementNSPlus("foo:Node"),
621
attributes: {"id": "123"},
622
expect: "<foo:Node xmlns:foo='http://example.com/foo' id='123'/>"
624
description: "foo prefixed attribute",
625
node: format.createElementNSPlus("foo:Node"),
626
attributes: {"foo:id": "123"},
627
expect: "<foo:Node xmlns:foo='http://example.com/foo' foo:id='123'/>"
629
description: "foo prefixed attribute with def prefixed node",
630
node: format.createElementNSPlus("def:Node"),
631
attributes: {"foo:id": "123"},
632
expect: "<def:Node xmlns:def='http://example.com/default' xmlns:foo='http://example.com/foo' foo:id='123'/>"
634
description: "multiple attributes",
635
node: format.createElementNSPlus("def:Node"),
636
attributes: {"id": "123", "foo": "bar"},
637
expect: "<def:Node xmlns:def='http://example.com/default' id='123' foo='bar'/>"
641
t.plan(cases.length);
643
for(var i=0; i<cases.length; ++i) {
645
format.setAttributes(test.node, test.attributes);
646
t.xml_eq(test.node, test.expect, test.description);