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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
|
<html>
<head>
<script src="../OLLoader.js"></script>
<script type="text/javascript">
var text =
'<?xml version="1.0"?>' +
'<ol:root xmlns="http://namespace.default.net" ' +
'xmlns:ol="http://namespace.openlayers.org" ' +
'xmlns:ta="http://namespace.testattribute.net">' +
'<ol:child ta:attribute="value1" ' +
'attribute="value2">' +
'junk1' +
'<' + '/ol:child>' +
'<ol:child>junk2<' + '/ol:child>' +
'<ol:child>junk3<' + '/ol:child>' +
'<element>junk4<' + '/element>' +
'<ol:element>junk5<' + '/ol:element>' +
'<ol:p>' +
'<ol:a>junk' +
'<' + '/ol:a>' +
'<ol:b>junk' +
'<' + '/ol:b>' +
'<ol:a>junk' +
'<' + '/ol:a>' +
'<' + '/ol:p>' +
'<' + '/ol:root>';
function test_Format_XML_constructor(t) {
t.plan(13);
var options = {'foo': 'bar'};
var format = new OpenLayers.Format.XML(options);
t.ok(format instanceof OpenLayers.Format.XML,
"new OpenLayers.Format.XML returns object" );
t.eq(format.foo, "bar", "constructor sets options correctly");
t.eq(typeof format.read, "function", "format has a read function");
t.eq(typeof format.write, "function", "format has a write function");
t.ok(!window.ActiveXObject || format.xmldom, "browsers with activeX must have xmldom");
// test namespaces
t.ok(format.namespaces instanceof Object, "format has namespace object");
var namespaces = {"foo": "bar"};
format = new OpenLayers.Format.XML({namespaces: namespaces});
t.eq(format.namespaces, namespaces, "format.namespaces correctly set in constructor");
// test default prefix
t.eq(format.defaultPrefix, null, "defaultPrefix is null by default");
format = new OpenLayers.Format.XML({defaultPrefix: "foo"});
t.eq(format.defaultPrefix, "foo", "defaultPrefix correctly set in constructor");
// test readers
t.ok(format.readers instanceof Object, "format has readers object");
var readers = {"foo": "bar"};
format = new OpenLayers.Format.XML({readers: readers});
t.eq(format.readers, readers, "format.readers correctly set in constructor");
// test readers
t.ok(format.writers instanceof Object, "format has writers object");
var writers = {"foo": "bar"};
format = new OpenLayers.Format.XML({writers: writers});
t.eq(format.writers, writers, "format.writers correctly set in constructor");
}
function test_destroy(t) {
t.plan(1);
var format = new OpenLayers.Format.XML();
format.destroy();
t.eq(format.xmldom, null, "xmldom set to null for all browsers");
}
function test_Format_XML_read(t) {
var format = new OpenLayers.Format.XML();
t.plan(format.xmldom ? 10 : 9);
var doc = format.read(text);
t.eq(doc.nodeType, 9,
"doc has the correct node type");
t.eq(doc.nodeName, "#document",
"doc has the correct node name");
t.ok(doc.documentElement,
"ok to access doc.documentElement");
t.xml_eq(doc.documentElement, text,
"doc.documentElement correctly read");
// read can also be called on the prototype directly
doc = OpenLayers.Format.XML.prototype.read(text);
t.eq(doc.nodeType, 9,
"doc has the correct node type");
t.eq(doc.nodeName, "#document",
"doc has the correct node name");
t.ok(doc.documentElement,
"ok to access doc.documentElement");
t.xml_eq(doc.documentElement, text,
"doc.documentElement correctly read");
// where appropriate, make sure doc is loaded into xmldom property
if(format.xmldom) {
t.xml_eq(format.xmldom.documentElement, text,
"xmldom.documentElement contains equivalent xml");
}
// test equivalence with different namespace alias
var pre1 =
"<pre1:parent xmlns:pre1='http://namespace'>" +
"<pre1:child1>value2</pre1:child1>" +
"<pre1:child2 pre1:attr1='foo'>value2</pre1:child2>" +
"<pre1:child3 chicken:attr='hot' xmlns:chicken='http://soup'/>" +
"</pre1:parent>";
var pre2 =
"<pre2:parent xmlns:pre2='http://namespace'>" +
"<pre2:child1>value2</pre2:child1>" +
"<pre2:child2 pre2:attr1='foo'>value2</pre2:child2>" +
"<pre2:child3 pea:attr='hot' xmlns:pea='http://soup'/>" +
"</pre2:parent>";
var doc1 = format.read(pre1);
t.xml_eq(doc1.documentElement, pre2, "read correctly sets namespaces");
}
function test_Format_XML_write(t) {
t.plan(2);
var format = new OpenLayers.Format.XML();
var doc = format.read(text);
var out = format.write(doc);
out = out.replace(/[\r\n]/g, '');
out = out.replace( /<\?.*\?>/, '')
var expected = text.replace(/<\?.*\?>/, '')
t.eq(expected, out,
"correctly writes an XML DOM doc");
var out = format.write(
format.getElementsByTagNameNS(doc,
"http://namespace.openlayers.org","root")[0]);
out = out.replace(/[\r\n]/g, '');
out = out.replace( /<\?.*\?>/, '')
t.eq(out, expected,
"correctly writes an XML DOM node");
}
function test_Format_XML_createElementNS(t) {
t.plan(5);
var format = new OpenLayers.Format.XML();
var uri = "http://foo.com";
var prefix = "foo";
var localName = "bar";
var qualifiedName = prefix + ":" + localName;
var node = format.createElementNS(uri, qualifiedName);
t.eq(node.nodeType, 1,
"node has correct type");
t.eq(node.nodeName, qualifiedName,
"node has correct qualified name");
t.eq(node.prefix, prefix,
"node has correct prefix");
t.eq(node.namespaceURI, uri,
"node has correct namespace uri");
var doc = format.read(text);
if (doc.importNode) {
node = doc.importNode(node, true);
}
t.ok(doc.documentElement.appendChild(node),
"node can be appended to a doc root");
}
function test_createDocumentFragment(t) {
t.plan(3);
var format = new OpenLayers.Format.XML();
var uri = "http://foo.com";
var prefix = "foo";
var localName = "bar";
var qualifiedName = prefix + ":" + localName;
var parent = format.createElementNS(uri, qualifiedName);
var fragment = format.createDocumentFragment();
t.eq(fragment.nodeType, 11, "fragment type");
try {
fragment.appendChild(format.createTextNode("one"));
fragment.appendChild(format.createTextNode("two"));
t.eq(fragment.childNodes.length, 2, "fragment has two child nodes");
} catch (err) {
t.fail("trouble appending text nodes to fragment: " + err.message);
}
try {
parent.appendChild(fragment);
t.eq(parent.childNodes.length, 2, "parent has two child nodes");
} catch (err) {
t.fail("trouble appending fragment to parent: " + err.message);
}
}
function test_Format_XML_createTextNode(t) {
t.plan(10);
var format = new OpenLayers.Format.XML();
var value, node;
value = "string";
node = format.createTextNode(value);
t.eq(node.nodeType, 3,
"[string] node has correct type");
t.eq(node.nodeName, "#text",
"[string] node has correct name");
t.eq(node.nodeValue, "string",
"[string] node has correct value");
value = 0.42;
node = format.createTextNode(value);
t.eq(node.nodeType, 3,
"[number] node has correct type");
t.eq(node.nodeName, "#text",
"[number] node has correct name");
t.eq(node.nodeValue, "0.42",
"[number] node has correct value");
value = false;
node = format.createTextNode(value);
t.eq(node.nodeType, 3,
"[boolean] node has correct type");
t.eq(node.nodeName, "#text",
"[boolean] node has correct name");
t.eq(node.nodeValue, "false",
"[boolean] node has correct value");
var doc = format.read(text);
if (doc.importNode) {
node = doc.importNode(node, true);
}
t.ok(doc.documentElement.appendChild(node),
"node can be appended to a doc root");
}
function test_Format_XML_getElementsByTagNameNS(t) {
t.plan(5);
var format = new OpenLayers.Format.XML();
var olUri = "http://namespace.openlayers.org";
var name = "child";
var doc = format.read(text);
var nodes = format.getElementsByTagNameNS(doc.documentElement,
olUri, name);
t.eq(nodes.length, 3,
"gets correct number of nodes");
var qualifiedName = nodes[0].prefix + ":" + name;
t.eq(nodes[0].nodeName, qualifiedName,
"first node has correct qualified name");
var defaultUri = "http://namespace.default.net";
name = "element";
nodes = format.getElementsByTagNameNS(doc.documentElement,
defaultUri, name);
t.eq(nodes.length, 1,
"gets correct number of nodes in default namespace");
var pList = format.getElementsByTagNameNS(doc.documentElement,
olUri, "p");
t.eq(pList.length, 1, "got one ol:p element");
var p = pList[0];
var aList = format.getElementsByTagNameNS(p, olUri, "a");
t.eq(aList.length, 2, "got two child ol:a elements");
}
function test_Format_XML_getAttributeNodeNS(t) {
t.plan(5);
var format = new OpenLayers.Format.XML();
var doc = format.read(text);
var olUri = "http://namespace.openlayers.org";
var taUri = "http://namespace.testattribute.net";
var localNodeName = "child";
var localAttrName = "attribute";
var nodes = format.getElementsByTagNameNS(doc.documentElement,
olUri, localNodeName);
var attributeNode = format.getAttributeNodeNS(nodes[0],
taUri, localAttrName);
var qualifiedName = attributeNode.prefix + ":" + localAttrName;
t.ok(attributeNode,
"returns non-null value");
t.eq(attributeNode.nodeType, 2,
"attribute node has correct type");
t.eq(attributeNode.nodeName, qualifiedName,
"attribute node has correct qualified name");
t.eq(attributeNode.nodeValue, "value1",
"attribute node has correct value");
var nullAttribute = format.getAttributeNodeNS(nodes[0],
taUri, "nothing");
t.ok(nullAttribute === null,
"returns null for nonexistent attribute");
}
function test_Format_XML_getAttributeNS(t) {
t.plan(2);
var format = new OpenLayers.Format.XML();
var doc = format.read(text);
var olUri = "http://namespace.openlayers.org";
var taUri = "http://namespace.testattribute.net";
var localNodeName = "child";
var localAttrName = "attribute";
var nodes = format.getElementsByTagNameNS(doc.documentElement,
olUri, localNodeName);
var attributeValue = format.getAttributeNS(nodes[0],
taUri, localAttrName);
t.eq(attributeValue, "value1",
"got correct attribute value");
var emptyValue = format.getAttributeNS(nodes[0],
taUri, "nothing");
t.ok(emptyValue === "",
"returns empty string for nonexistent attributes");
}
function test_Format_XML_hasAttributeNS(t) {
t.plan(2);
var format = new OpenLayers.Format.XML();
var doc = format.read(text);
var olUri = "http://namespace.openlayers.org";
var taUri = "http://namespace.testattribute.net";
var localNodeName = "child";
var localAttrName = "attribute";
var nodes = format.getElementsByTagNameNS(doc.documentElement,
olUri, localNodeName);
var found = format.hasAttributeNS(nodes[0], taUri, localAttrName);
t.ok(found === true, "returns true for good attribute");
found = format.hasAttributeNS(nodes[0], taUri, "nothing");
t.ok(found === false, "returns false for bad attribute");
}
function test_namespaces(t) {
t.plan(2);
var format = new OpenLayers.Format.XML({
namespaces: {
"def": "http://example.com/default",
"foo": "http://example.com/foo",
"bar": "http://example.com/bar"
},
defaultPrefix: "def"
});
// test that prototype has not been altered
t.eq(OpenLayers.Format.XML.prototype.namespaces, null,
"setting namespaces at construction does not modify prototype");
// test that namespaceAlias has been set
t.eq(format.namespaceAlias["http://example.com/foo"], "foo",
"namespaceAlias mapping has been set");
}
function test_setNamespace(t) {
t.plan(3);
var format = new OpenLayers.Format.XML();
// test that namespaces is an object
t.ok(format.namespaces instanceof Object, "empty namespace object set");
format.setNamespace("foo", "http://example.com/foo");
t.eq(format.namespaces["foo"], "http://example.com/foo", "alias -> uri mapping set");
t.eq(format.namespaceAlias["http://example.com/foo"], "foo", "uri -> alias mapping set");
}
function test_readChildNodes(t) {
var text = "<?xml version='1.0' encoding='UTF-8'?>" +
"<container xmlns='http://example.com/foo'>" +
"<marker name='my marker 1'>" +
"<position>" +
"<lon>-180</lon>" +
"<lat>90</lat>" +
"</position>" +
"<detail>some text for first marker</detail>" +
"<atom:link xmlns:atom='http://www.w3.org/2005/Atom' href='http://host/path/1'/>" +
"</marker>" +
"<marker name='my marker 2'>" +
"<position>" +
"<lon>180</lon>" +
"<lat>-90</lat>" +
"</position>" +
"<detail>some text for second marker</detail>" +
"<atom:link xmlns:atom='http://www.w3.org/2005/Atom' href='http://host/path/2'/>" +
"</marker>" +
"</container>";
var expect = [
new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(-180, 90),
{
name: 'my marker 1',
link: 'http://host/path/1',
detail: 'some text for first marker'
}
),
new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(180, -90),
{
name: 'my marker 2',
link: 'http://host/path/2',
detail: 'some text for second marker'
}
)
];
var format = new OpenLayers.Format.XML({
defaultPrefix: "foo",
namespaces: {
"foo": "http://example.com/foo",
"atom": "http://www.w3.org/2005/Atom"
},
readers: {
"foo": {
"container": function(node, obj) {
var list = [];
this.readChildNodes(node, list);
obj.list = list;
},
"marker": function(node, list) {
var feature = new OpenLayers.Feature.Vector();
feature.attributes.name = node.getAttribute("name");
this.readChildNodes(node, feature);
list.push(feature);
},
"position": function(node, feature) {
var obj = {};
this.readChildNodes(node, obj);
feature.geometry = new OpenLayers.Geometry.Point(obj.x, obj.y);
},
"lon": function(node, obj) {
obj.x = this.getChildValue(node);
},
"lat": function(node, obj) {
obj.y = this.getChildValue(node);
},
"detail": function(node, feature) {
feature.attributes.detail = this.getChildValue(node);
}
},
"atom": {
"link": function(node, feature) {
feature.attributes.link = node.getAttribute("href");
}
}
}
});
// convert text to document node
var doc = format.read(text);
// read child nodes to get back some object
var obj = format.readChildNodes(doc);
// start comparing what we got to what we expect
var got = obj.list;
t.plan(11);
t.eq(got.length, expect.length, "correct number of items parsed");
t.eq(got[0].geometry.x, expect[0].geometry.x, "correct x coord parsed for marker 1");
t.eq(got[0].geometry.y, expect[0].geometry.y, "correct y coord parsed for marker 1");
t.eq(got[0].attributes.name, expect[0].attributes.name, "correct name parsed for marker 1");
t.eq(got[0].attributes.detail, expect[0].attributes.detail, "correct detail parsed for marker 1");
t.eq(got[0].attributes.link, expect[0].attributes.link, "correct link parsed for marker 1");
t.eq(got[1].geometry.x, expect[1].geometry.x, "correct x coord parsed for marker 2");
t.eq(got[1].geometry.y, expect[1].geometry.y, "correct y coord parsed for marker 2");
t.eq(got[1].attributes.name, expect[1].attributes.name, "correct name parsed for marker 2");
t.eq(got[1].attributes.detail, expect[1].attributes.detail, "correct detail parsed for marker 2");
t.eq(got[1].attributes.link, expect[1].attributes.link, "correct link parsed for marker 2");
}
function test_writeNode(t) {
var features = [
new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(-180, 90),
{
name: 'my marker 1',
link: 'http://host/path/1',
detail: 'some text for first marker'
}
),
new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(180, -90),
{
name: 'my marker 2',
link: 'http://host/path/2',
detail: 'some text for second marker'
}
)
];
var expect = "<?xml version='1.0' encoding='UTF-8'?>" +
"<container xmlns='http://example.com/foo'>" +
"<marker name='my marker 1'>" +
"<position>" +
"<lon>-180</lon>" +
"<lat>90</lat>" +
"</position>" +
"<detail>some text for first marker</detail>" +
"<atom:link xmlns:atom='http://www.w3.org/2005/Atom' href='http://host/path/1'/>" +
"</marker>" +
"<marker name='my marker 2'>" +
"<position>" +
"<lon>180</lon>" +
"<lat>-90</lat>" +
"</position>" +
"<detail>some text for second marker</detail>" +
"<atom:link xmlns:atom='http://www.w3.org/2005/Atom' href='http://host/path/2'/>" +
"</marker>" +
"</container>";
var format = new OpenLayers.Format.XML({
defaultPrefix: "foo",
namespaces: {
"foo": "http://example.com/foo",
"atom": "http://www.w3.org/2005/Atom"
},
writers: {
"foo": {
"container": function(features) {
var node = this.createElementNSPlus("container");
var feature;
for(var i=0; i<features.length; ++i) {
feature = features[i];
this.writeNode("marker", features[i], node);
}
return node;
},
"marker": function(feature) {
var node = this.createElementNSPlus("marker", {
attributes: {name: feature.attributes.name}
});
this.writeNode("position", feature.geometry, node);
this.writeNode("detail", feature.attributes.detail, node);
this.writeNode("atom:link", feature.attributes.link, node);
return node;
},
"position": function(geometry) {
var node = this.createElementNSPlus("position");
this.writeNode("lon", geometry.x, node);
this.writeNode("lat", geometry.y, node);
return node;
},
"lon": function(x) {
return this.createElementNSPlus("lon", {
value: x
});
},
"lat": function(y) {
return this.createElementNSPlus("lat", {
value: y
});
},
"detail": function(text) {
return this.createElementNSPlus("detail", {
value: text
});
}
},
"atom": {
"link": function(href) {
return this.createElementNSPlus("atom:link", {
attributes: {href: href}
});
}
}
}
});
t.plan(1);
// test that we get what we expect from writeNode
var got = format.writeNode("container", features);
t.xml_eq(got, expect, "features correctly written");
}
function test_createElementNSPlus(t) {
var format = new OpenLayers.Format.XML({
defaultPrefix: "def",
namespaces: {
"def": "http://example.com/default",
"foo": "http://example.com/foo",
"bar": "http://example.com/bar"
}
});
var cases = [
{
description: "unprefixed name with default options",
node: format.createElementNSPlus("FooNode"),
expect: "<def:FooNode xmlns:def='http://example.com/default'/>"
}, {
description: "def prefixed name with default options",
node: format.createElementNSPlus("def:FooNode"),
expect: "<def:FooNode xmlns:def='http://example.com/default'/>"
}, {
description: "foo prefixed name with default options",
node: format.createElementNSPlus("foo:FooNode"),
expect: "<foo:FooNode xmlns:foo='http://example.com/foo'/>"
}, {
description: "unprefixed name with uri option",
node: format.createElementNSPlus("FooNode", {
uri: "http://example.com/elsewhere"
}),
expect: "<FooNode xmlns='http://example.com/elsewhere'/>"
}, {
description: "foo prefixed name with uri option (overriding format.namespaces)",
node: format.createElementNSPlus("foo:FooNode", {
uri: "http://example.com/elsewhere"
}),
expect: "<foo:FooNode xmlns:foo='http://example.com/elsewhere'/>"
}, {
description: "foo prefixed name with attributes option",
node: format.createElementNSPlus("foo:FooNode", {
attributes: {
"id": "123",
"foo:attr1": "namespaced attribute 1",
"bar:attr2": "namespaced attribute 2"
}
}),
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'/>"
}, {
description: "foo prefixed name with attributes and value options",
node: format.createElementNSPlus("foo:FooNode", {
attributes: {"id": "123"},
value: "text value"
}),
expect: "<foo:FooNode xmlns:foo='http://example.com/foo' id='123'>text value<" + "/foo:FooNode>"
}, {
description: "value of 0 gets appended as a text node",
node: format.createElementNSPlus("foo:bar", {value: 0}),
expect: "<foo:bar xmlns:foo='http://example.com/foo'>0</foo:bar>"
}, {
description: "value of 0.42 gets appended as a text node",
node: format.createElementNSPlus("foo:bar", {value: 0.42}),
expect: "<foo:bar xmlns:foo='http://example.com/foo'>0.42</foo:bar>"
}, {
description: "value of true gets appended as a text node",
node: format.createElementNSPlus("foo:bar", {value: true}),
expect: "<foo:bar xmlns:foo='http://example.com/foo'>true</foo:bar>"
}, {
description: "value of false gets appended as a text node",
node: format.createElementNSPlus("foo:bar", {value: false}),
expect: "<foo:bar xmlns:foo='http://example.com/foo'>false</foo:bar>"
}, {
description: "null value does not get appended as a text node",
node: format.createElementNSPlus("foo:bar", {value: null}),
expect: "<foo:bar xmlns:foo='http://example.com/foo'/>"
}, {
description: "undefined value does not get appended as a text node",
node: format.createElementNSPlus("foo:bar"),
expect: "<foo:bar xmlns:foo='http://example.com/foo'/>"
}
];
t.plan(cases.length);
var test;
for(var i=0; i<cases.length; ++i) {
test = cases[i];
t.xml_eq(test.node, test.expect, test.description);
}
}
function test_setAttributes(t) {
var format = new OpenLayers.Format.XML({
defaultPrefix: "def",
namespaces: {
"def": "http://example.com/default",
"foo": "http://example.com/foo",
"bar": "http://example.com/bar"
}
});
var cases = [
{
description: "unprefixed attribute",
node: format.createElementNSPlus("foo:Node"),
attributes: {"id": "123"},
expect: "<foo:Node xmlns:foo='http://example.com/foo' id='123'/>"
}, {
description: "foo prefixed attribute",
node: format.createElementNSPlus("foo:Node"),
attributes: {"foo:id": "123"},
expect: "<foo:Node xmlns:foo='http://example.com/foo' foo:id='123'/>"
}, {
description: "foo prefixed attribute with def prefixed node",
node: format.createElementNSPlus("def:Node"),
attributes: {"foo:id": "123"},
expect: "<def:Node xmlns:def='http://example.com/default' xmlns:foo='http://example.com/foo' foo:id='123'/>"
}, {
description: "multiple attributes",
node: format.createElementNSPlus("def:Node"),
attributes: {"id": "123", "foo": "bar"},
expect: "<def:Node xmlns:def='http://example.com/default' id='123' foo='bar'/>"
}
];
t.plan(cases.length);
var test;
for(var i=0; i<cases.length; ++i) {
test = cases[i];
format.setAttributes(test.node, test.attributes);
t.xml_eq(test.node, test.expect, test.description);
}
}
function test_keepData(t) {
t.plan(2);
var options = {'keepData': true};
var format = new OpenLayers.Format.XML(options);
format.read(text);
t.ok(format.data != null, 'data property is not null after read with keepData=true');
t.eq(format.data.documentElement.tagName,'ol:root','keepData keeps the right data');
}
function test_getChildValue(t) {
t.plan(1);
var text =
"<?xml version='1.0' encoding='UTF-8'?>" +
"<root>" +
"x<!-- comment -->y<!-- comment 2 --><![CDATA[z]]>z<foo />y" +
"</root>";
var format = new OpenLayers.Format.XML();
var doc = format.read(text).documentElement;
t.eq(format.getChildValue(doc), "xyzzy", "child value skips comments, concatenates multiple values, reads through entities");
}
function test_getChildEl(t) {
t.plan(3);
var text =
"<?xml version='1.0' encoding='UTF-8'?>" +
"<root>" +
"<!-- comment -->" +
"<a>x</a>" +
"<b>x</b>" +
"</root>";
var format = new OpenLayers.Format.XML();
var doc = format.read(text).documentElement;
var a = format.getChildEl(doc);
t.eq(a.nodeName, "a", "first element found correctly");
a = format.getChildEl(doc, "a");
t.eq(b, null, "first child element matches the given name");
var b = format.getChildEl(doc, "b");
t.eq(b, null, "first child element does not match the given name");
}
function test_getNextEl(t) {
t.plan(5);
var text =
"<?xml version='1.0' encoding='UTF-8'?>" +
"<root>" +
"<!-- comment -->" +
"<a>x</a>" +
"<!-- comment -->" +
"<b xmlns='urn:example'>x</b>" +
"</root>";
var format = new OpenLayers.Format.XML();
var doc = format.read(text).documentElement;
var a = format.getChildEl(doc);
var b = format.getNextEl(a);
t.eq(b && b.nodeName, "b", "next element correctly found");
b = format.getNextEl(a, "b");
t.eq(b && b.nodeName, "b", "next element correctly found when name supplied");
b = format.getNextEl(a, "c");
t.eq(b, null, "null returned when name does not match next element");
b = format.getNextEl(a, null, "urn:example");
t.eq(b && b.nodeName, "b", "next element correctly found when namespace supplied");
b = format.getNextEl(a, null, "foo");
t.eq(b, null, "null returned when namespace does not match next element");
}
function test_isSimpleContent(t) {
t.plan(2);
var text =
"<?xml version='1.0' encoding='UTF-8'?>" +
"<root>" +
"<!-- comment -->" +
"<a>x<!-- comment -->y<!-- comment 2 --><![CDATA[z]]>z<foo />y</a>" +
"<!-- comment -->" +
"<b>x<!-- comment -->y<!-- comment 2 --><![CDATA[z]]>zy</b>" +
"</root>";
var format = new OpenLayers.Format.XML();
var doc = format.read(text).documentElement;
var a = format.getChildEl(doc);
var b = format.getNextEl(a);
t.ok(!format.isSimpleContent(a), "<a> content is not simple");
t.ok(format.isSimpleContent(b), "<b> content is simple");
}
function test_lookupNamespaceURI(t) {
t.plan(8);
var text =
"<?xml version='1.0' encoding='UTF-8'?>" +
"<root xmlns:baz='urn:baznamespace'>" +
"<!-- comment -->" +
"<a><foo /></a>" +
"<!-- comment -->" +
"<b xmlns='urn:example'><!-- comment --><bar foo='value'/></b>" +
"</root>";
var format = new OpenLayers.Format.XML();
var doc = format.read(text).documentElement;
var a = format.getChildEl(doc);
t.eq(format.lookupNamespaceURI(a, "baz"), "urn:baznamespace", "prefix lookup on first child");
var foo = format.getChildEl(a);
t.eq(format.lookupNamespaceURI(foo, "baz"), "urn:baznamespace", "prefix lookup on child of first child");
var b = format.getNextEl(a);
t.eq(format.lookupNamespaceURI(b, null), "urn:example", "default namespace lookup on element");
var bar = format.getChildEl(b);
t.eq(format.lookupNamespaceURI(bar, null), "urn:example", "default namespace lookup on child");
t.eq(format.lookupNamespaceURI(bar, "baz"), "urn:baznamespace", "prefix lookup on child with different default");
// test that the alias behaves properly
var lookup = OpenLayers.Format.XML.lookupNamespaceURI;
t.eq(lookup(bar, "baz"), "urn:baznamespace", "(alias) prefix lookup on child with different default");
var attr = bar.attributes[0];
// Internet Explorer didn't have the ownerElement property until 8.
var supportsOwnerElement = !!attr.ownerElement;
if(supportsOwnerElement) {
t.eq(format.lookupNamespaceURI(attr, null), "urn:example", "default namespace lookup on attribute");
t.eq(format.lookupNamespaceURI(attr, "baz"), "urn:baznamespace", "prefix lookup on attribute with different default");
} else {
t.debug_print("namespace lookup on attributes not supported in this browser");
t.ok(true, "namespace lookup on attributes not supported in this browser");
t.ok(true, "namespace lookup on attributes not supported in this browser");
}
}
function test_getXMLDoc(t) {
t.plan(2);
var format = new OpenLayers.Format.XML();
var doc = format.getXMLDoc();
t.ok(doc !== document, "document returned from getXMLDoc is not the page's html doc");
var root = format.createElementNS("http://test", "root");
// appending CDATA created from a different document
var cdata = doc.createCDATASection("<foo></foo>");
root.appendChild(cdata);
var result = format.write(root);
var expect = '<root xmlns="http://test"><![CDATA[<foo></foo>]]></root>';
t.eq(result, expect, "document with CDATA section serialized correctly");
}
</script>
</head>
<body>
</body>
</html>
|