| | 293 | |
|---|
| | 294 | function test_getNamespacePrefix(t) { |
|---|
| | 295 | t.plan(6); |
|---|
| | 296 | |
|---|
| | 297 | // test that getNamespacePrefix returns null with no ns defined |
|---|
| | 298 | var format = new OpenLayers.Format.XML(); |
|---|
| | 299 | var got = format.getNamespacePrefix("http://example.com/foo"); |
|---|
| | 300 | t.eq(got, null, "returns null when no namespaces are defined"); |
|---|
| | 301 | |
|---|
| | 302 | format.defaultPrefix = "def"; |
|---|
| | 303 | format.namespaces = { |
|---|
| | 304 | "def": "http://example.com/default", |
|---|
| | 305 | "foo": "http://example.com/foo", |
|---|
| | 306 | "bar": "http://example.com/bar" |
|---|
| | 307 | }; |
|---|
| | 308 | |
|---|
| | 309 | var cases = [ |
|---|
| | 310 | {uri: null, expect: "def"}, |
|---|
| | 311 | {uri: "http://example.com/default", expect: "def"}, |
|---|
| | 312 | {uri: "http://example.com/foo", expect: "foo"}, |
|---|
| | 313 | {uri: "http://example.com/bar", expect: "bar"}, |
|---|
| | 314 | {uri: "http://example.com/nothing", expect: null} |
|---|
| | 315 | ]; |
|---|
| | 316 | |
|---|
| | 317 | var test; |
|---|
| | 318 | for(var i=0; i<cases.length; ++i) { |
|---|
| | 319 | test = cases[i]; |
|---|
| | 320 | t.eq(format.getNamespacePrefix(test.uri), test.expect, |
|---|
| | 321 | "uri: " + test.uri + " works"); |
|---|
| | 322 | } |
|---|
| | 323 | |
|---|
| | 324 | } |
|---|
| | 325 | |
|---|
| | 326 | function test_readChildNodes(t) { |
|---|
| | 327 | |
|---|
| | 328 | var text = "<?xml version='1.0' encoding='UTF-8'?>" + |
|---|
| | 329 | "<container xmlns='http://example.com/foo'>" + |
|---|
| | 330 | "<marker name='my marker 1'>" + |
|---|
| | 331 | "<position>" + |
|---|
| | 332 | "<lon>-180</lon>" + |
|---|
| | 333 | "<lat>90</lat>" + |
|---|
| | 334 | "</position>" + |
|---|
| | 335 | "<detail>some text for first marker</detail>" + |
|---|
| | 336 | "<atom:link xmlns:atom='http://www.w3.org/2005/Atom' href='http://host/path/1'/>" + |
|---|
| | 337 | "</marker>" + |
|---|
| | 338 | "<marker name='my marker 2'>" + |
|---|
| | 339 | "<position>" + |
|---|
| | 340 | "<lon>180</lon>" + |
|---|
| | 341 | "<lat>-90</lat>" + |
|---|
| | 342 | "</position>" + |
|---|
| | 343 | "<detail>some text for second marker</detail>" + |
|---|
| | 344 | "<atom:link xmlns:atom='http://www.w3.org/2005/Atom' href='http://host/path/2'/>" + |
|---|
| | 345 | "</marker>" + |
|---|
| | 346 | "</container>"; |
|---|
| | 347 | |
|---|
| | 348 | var expect = [ |
|---|
| | 349 | new OpenLayers.Feature.Vector( |
|---|
| | 350 | new OpenLayers.Geometry.Point(-180, 90), |
|---|
| | 351 | { |
|---|
| | 352 | name: 'my marker 1', |
|---|
| | 353 | link: 'http://host/path/1', |
|---|
| | 354 | detail: 'some text for first marker' |
|---|
| | 355 | } |
|---|
| | 356 | ), |
|---|
| | 357 | new OpenLayers.Feature.Vector( |
|---|
| | 358 | new OpenLayers.Geometry.Point(180, -90), |
|---|
| | 359 | { |
|---|
| | 360 | name: 'my marker 2', |
|---|
| | 361 | link: 'http://host/path/2', |
|---|
| | 362 | detail: 'some text for second marker' |
|---|
| | 363 | } |
|---|
| | 364 | ) |
|---|
| | 365 | ]; |
|---|
| | 366 | |
|---|
| | 367 | var format = new OpenLayers.Format.XML({ |
|---|
| | 368 | defaultPrefix: "foo", |
|---|
| | 369 | namespaces: { |
|---|
| | 370 | "foo": "http://example.com/foo", |
|---|
| | 371 | "atom": "http://www.w3.org/2005/Atom" |
|---|
| | 372 | }, |
|---|
| | 373 | readers: { |
|---|
| | 374 | "foo": { |
|---|
| | 375 | "container": function(node, obj) { |
|---|
| | 376 | var list = []; |
|---|
| | 377 | this.readChildNodes(node, list); |
|---|
| | 378 | obj.list = list; |
|---|
| | 379 | }, |
|---|
| | 380 | "marker": function(node, list) { |
|---|
| | 381 | var feature = new OpenLayers.Feature.Vector(); |
|---|
| | 382 | feature.attributes.name = node.getAttribute("name"); |
|---|
| | 383 | this.readChildNodes(node, feature); |
|---|
| | 384 | list.push(feature); |
|---|
| | 385 | }, |
|---|
| | 386 | "position": function(node, feature) { |
|---|
| | 387 | var obj = {}; |
|---|
| | 388 | this.readChildNodes(node, obj); |
|---|
| | 389 | feature.geometry = new OpenLayers.Geometry.Point(obj.x, obj.y); |
|---|
| | 390 | }, |
|---|
| | 391 | "lon": function(node, obj) { |
|---|
| | 392 | obj.x = this.getChildValue(node); |
|---|
| | 393 | }, |
|---|
| | 394 | "lat": function(node, obj) { |
|---|
| | 395 | obj.y = this.getChildValue(node); |
|---|
| | 396 | }, |
|---|
| | 397 | "detail": function(node, feature) { |
|---|
| | 398 | feature.attributes.detail = this.getChildValue(node); |
|---|
| | 399 | } |
|---|
| | 400 | }, |
|---|
| | 401 | "atom": { |
|---|
| | 402 | "link": function(node, feature) { |
|---|
| | 403 | feature.attributes.link = node.getAttribute("href"); |
|---|
| | 404 | } |
|---|
| | 405 | } |
|---|
| | 406 | } |
|---|
| | 407 | }); |
|---|
| | 408 | |
|---|
| | 409 | // convert text to document node |
|---|
| | 410 | var doc = format.read(text); |
|---|
| | 411 | // read child nodes to get back some object |
|---|
| | 412 | var obj = format.readChildNodes(doc); |
|---|
| | 413 | // start comparing what we got to what we expect |
|---|
| | 414 | var got = obj.list; |
|---|
| | 415 | |
|---|
| | 416 | t.plan(11); |
|---|
| | 417 | t.eq(got.length, expect.length, "correct number of items parsed"); |
|---|
| | 418 | t.eq(got[0].geometry.x, expect[0].geometry.x, "correct x coord parsed for marker 1"); |
|---|
| | 419 | t.eq(got[0].geometry.y, expect[0].geometry.y, "correct y coord parsed for marker 1"); |
|---|
| | 420 | t.eq(got[0].attributes.name, expect[0].attributes.name, "correct name parsed for marker 1"); |
|---|
| | 421 | t.eq(got[0].attributes.detail, expect[0].attributes.detail, "correct detail parsed for marker 1"); |
|---|
| | 422 | t.eq(got[0].attributes.link, expect[0].attributes.link, "correct link parsed for marker 1"); |
|---|
| | 423 | t.eq(got[1].geometry.x, expect[1].geometry.x, "correct x coord parsed for marker 2"); |
|---|
| | 424 | t.eq(got[1].geometry.y, expect[1].geometry.y, "correct y coord parsed for marker 2"); |
|---|
| | 425 | t.eq(got[1].attributes.name, expect[1].attributes.name, "correct name parsed for marker 2"); |
|---|
| | 426 | t.eq(got[1].attributes.detail, expect[1].attributes.detail, "correct detail parsed for marker 2"); |
|---|
| | 427 | t.eq(got[1].attributes.link, expect[1].attributes.link, "correct link parsed for marker 2"); |
|---|
| | 428 | |
|---|
| | 429 | } |
|---|
| | 430 | |
|---|
| | 431 | function test_writeNode(t) { |
|---|
| | 432 | |
|---|
| | 433 | var features = [ |
|---|
| | 434 | new OpenLayers.Feature.Vector( |
|---|
| | 435 | new OpenLayers.Geometry.Point(-180, 90), |
|---|
| | 436 | { |
|---|
| | 437 | name: 'my marker 1', |
|---|
| | 438 | link: 'http://host/path/1', |
|---|
| | 439 | detail: 'some text for first marker' |
|---|
| | 440 | } |
|---|
| | 441 | ), |
|---|
| | 442 | new OpenLayers.Feature.Vector( |
|---|
| | 443 | new OpenLayers.Geometry.Point(180, -90), |
|---|
| | 444 | { |
|---|
| | 445 | name: 'my marker 2', |
|---|
| | 446 | link: 'http://host/path/2', |
|---|
| | 447 | detail: 'some text for second marker' |
|---|
| | 448 | } |
|---|
| | 449 | ) |
|---|
| | 450 | ]; |
|---|
| | 451 | |
|---|
| | 452 | var expect = "<?xml version='1.0' encoding='UTF-8'?>" + |
|---|
| | 453 | "<container xmlns='http://example.com/foo'>" + |
|---|
| | 454 | "<marker name='my marker 1'>" + |
|---|
| | 455 | "<position>" + |
|---|
| | 456 | "<lon>-180</lon>" + |
|---|
| | 457 | "<lat>90</lat>" + |
|---|
| | 458 | "</position>" + |
|---|
| | 459 | "<detail>some text for first marker</detail>" + |
|---|
| | 460 | "<atom:link xmlns:atom='http://www.w3.org/2005/Atom' href='http://host/path/1'/>" + |
|---|
| | 461 | "</marker>" + |
|---|
| | 462 | "<marker name='my marker 2'>" + |
|---|
| | 463 | "<position>" + |
|---|
| | 464 | "<lon>180</lon>" + |
|---|
| | 465 | "<lat>-90</lat>" + |
|---|
| | 466 | "</position>" + |
|---|
| | 467 | "<detail>some text for second marker</detail>" + |
|---|
| | 468 | "<atom:link xmlns:atom='http://www.w3.org/2005/Atom' href='http://host/path/2'/>" + |
|---|
| | 469 | "</marker>" + |
|---|
| | 470 | "</container>"; |
|---|
| | 471 | |
|---|
| | 472 | var format = new OpenLayers.Format.XML({ |
|---|
| | 473 | defaultPrefix: "foo", |
|---|
| | 474 | namespaces: { |
|---|
| | 475 | "foo": "http://example.com/foo", |
|---|
| | 476 | "atom": "http://www.w3.org/2005/Atom" |
|---|
| | 477 | }, |
|---|
| | 478 | writers: { |
|---|
| | 479 | "foo": { |
|---|
| | 480 | "container": function(features) { |
|---|
| | 481 | var node = this.createElementNSPlus("container"); |
|---|
| | 482 | var feature; |
|---|
| | 483 | for(var i=0; i<features.length; ++i) { |
|---|
| | 484 | feature = features[i]; |
|---|
| | 485 | this.writeNode("marker", features[i], node); |
|---|
| | 486 | } |
|---|
| | 487 | return node; |
|---|
| | 488 | }, |
|---|
| | 489 | "marker": function(feature) { |
|---|
| | 490 | var node = this.createElementNSPlus("marker", { |
|---|
| | 491 | attributes: {name: feature.attributes.name} |
|---|
| | 492 | }); |
|---|
| | 493 | this.writeNode("position", feature.geometry, node); |
|---|
| | 494 | this.writeNode("detail", feature.attributes.detail, node); |
|---|
| | 495 | this.writeNode("atom:link", feature.attributes.link, node); |
|---|
| | 496 | return node; |
|---|
| | 497 | }, |
|---|
| | 498 | "position": function(geometry) { |
|---|
| | 499 | var node = this.createElementNSPlus("position"); |
|---|
| | 500 | this.writeNode("lon", geometry.x, node); |
|---|
| | 501 | this.writeNode("lat", geometry.y, node); |
|---|
| | 502 | return node; |
|---|
| | 503 | }, |
|---|
| | 504 | "lon": function(x) { |
|---|
| | 505 | return this.createElementNSPlus("lon", { |
|---|
| | 506 | value: x |
|---|
| | 507 | }); |
|---|
| | 508 | }, |
|---|
| | 509 | "lat": function(y) { |
|---|
| | 510 | return this.createElementNSPlus("lat", { |
|---|
| | 511 | value: y |
|---|
| | 512 | }); |
|---|
| | 513 | }, |
|---|
| | 514 | "detail": function(text) { |
|---|
| | 515 | return this.createElementNSPlus("detail", { |
|---|
| | 516 | value: text |
|---|
| | 517 | }); |
|---|
| | 518 | } |
|---|
| | 519 | }, |
|---|
| | 520 | "atom": { |
|---|
| | 521 | "link": function(href) { |
|---|
| | 522 | return this.createElementNSPlus("atom:link", { |
|---|
| | 523 | attributes: {href: href} |
|---|
| | 524 | }); |
|---|
| | 525 | } |
|---|
| | 526 | } |
|---|
| | 527 | } |
|---|
| | 528 | |
|---|
| | 529 | }); |
|---|
| | 530 | |
|---|
| | 531 | t.plan(1); |
|---|
| | 532 | // test that we get what we expect from writeNode |
|---|
| | 533 | var got = format.writeNode("container", features); |
|---|
| | 534 | t.xml_eq(got, expect, "features correctly written"); |
|---|
| | 535 | } |
|---|
| | 536 | |
|---|
| | 537 | function test_createElementNSPlus(t) { |
|---|
| | 538 | |
|---|
| | 539 | var format = new OpenLayers.Format.XML({ |
|---|
| | 540 | defaultPrefix: "def", |
|---|
| | 541 | namespaces: { |
|---|
| | 542 | "def": "http://example.com/default", |
|---|
| | 543 | "foo": "http://example.com/foo", |
|---|
| | 544 | "bar": "http://example.com/bar" |
|---|
| | 545 | } |
|---|
| | 546 | }); |
|---|
| | 547 | |
|---|
| | 548 | var cases = [ |
|---|
| | 549 | { |
|---|
| | 550 | description: "unprefixed name with default options", |
|---|
| | 551 | node: format.createElementNSPlus("FooNode"), |
|---|
| | 552 | expect: "<def:FooNode xmlns:def='http://example.com/default'/>" |
|---|
| | 553 | }, { |
|---|
| | 554 | description: "def prefixed name with default options", |
|---|
| | 555 | node: format.createElementNSPlus("def:FooNode"), |
|---|
| | 556 | expect: "<def:FooNode xmlns:def='http://example.com/default'/>" |
|---|
| | 557 | }, { |
|---|
| | 558 | description: "foo prefixed name with default options", |
|---|
| | 559 | node: format.createElementNSPlus("foo:FooNode"), |
|---|
| | 560 | expect: "<foo:FooNode xmlns:foo='http://example.com/foo'/>" |
|---|
| | 561 | }, { |
|---|
| | 562 | description: "unprefixed name with uri option", |
|---|
| | 563 | node: format.createElementNSPlus("FooNode", { |
|---|
| | 564 | uri: "http://example.com/elsewhere" |
|---|
| | 565 | }), |
|---|
| | 566 | expect: "<FooNode xmlns='http://example.com/elsewhere'/>" |
|---|
| | 567 | }, { |
|---|
| | 568 | description: "foo prefixed name with uri option (overriding format.namespaces)", |
|---|
| | 569 | node: format.createElementNSPlus("foo:FooNode", { |
|---|
| | 570 | uri: "http://example.com/elsewhere" |
|---|
| | 571 | }), |
|---|
| | 572 | expect: "<foo:FooNode xmlns:foo='http://example.com/elsewhere'/>" |
|---|
| | 573 | }, { |
|---|
| | 574 | description: "foo prefixed name with attributes option", |
|---|
| | 575 | node: format.createElementNSPlus("foo:FooNode", { |
|---|
| | 576 | attributes: { |
|---|
| | 577 | "id": "123", |
|---|
| | 578 | "foo:attr1": "namespaced attribute 1", |
|---|
| | 579 | "bar:attr2": "namespaced attribute 2" |
|---|
| | 580 | } |
|---|
| | 581 | }), |
|---|
| | 582 | 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'/>" |
|---|
| | 583 | }, { |
|---|
| | 584 | description: "foo prefixed name with attributes and value options", |
|---|
| | 585 | node: format.createElementNSPlus("foo:FooNode", { |
|---|
| | 586 | attributes: {"id": "123"}, |
|---|
| | 587 | value: "text value" |
|---|
| | 588 | }), |
|---|
| | 589 | expect: "<foo:FooNode xmlns:foo='http://example.com/foo' id='123'>text value<" + "/foo:FooNode>" |
|---|
| | 590 | } |
|---|
| | 591 | ]; |
|---|
| | 592 | |
|---|
| | 593 | t.plan(cases.length); |
|---|
| | 594 | var test; |
|---|
| | 595 | for(var i=0; i<cases.length; ++i) { |
|---|
| | 596 | test = cases[i]; |
|---|
| | 597 | t.xml_eq(test.node, test.expect, test.description); |
|---|
| | 598 | } |
|---|
| | 599 | |
|---|
| | 600 | } |
|---|
| | 601 | |
|---|
| | 602 | function test_setAttributes(t) { |
|---|
| | 603 | |
|---|
| | 604 | var format = new OpenLayers.Format.XML({ |
|---|
| | 605 | defaultPrefix: "def", |
|---|
| | 606 | namespaces: { |
|---|
| | 607 | "def": "http://example.com/default", |
|---|
| | 608 | "foo": "http://example.com/foo", |
|---|
| | 609 | "bar": "http://example.com/bar" |
|---|
| | 610 | } |
|---|
| | 611 | }); |
|---|
| | 612 | |
|---|
| | 613 | var cases = [ |
|---|
| | 614 | { |
|---|
| | 615 | description: "unprefixed attribute", |
|---|
| | 616 | node: format.createElementNSPlus("foo:Node"), |
|---|
| | 617 | attributes: {"id": "123"}, |
|---|
| | 618 | expect: "<foo:Node xmlns:foo='http://example.com/foo' id='123'/>" |
|---|
| | 619 | }, { |
|---|
| | 620 | description: "foo prefixed attribute", |
|---|
| | 621 | node: format.createElementNSPlus("foo:Node"), |
|---|
| | 622 | attributes: {"foo:id": "123"}, |
|---|
| | 623 | expect: "<foo:Node xmlns:foo='http://example.com/foo' foo:id='123'/>" |
|---|
| | 624 | }, { |
|---|
| | 625 | description: "foo prefixed attribute with def prefixed node", |
|---|
| | 626 | node: format.createElementNSPlus("def:Node"), |
|---|
| | 627 | attributes: {"foo:id": "123"}, |
|---|
| | 628 | expect: "<def:Node xmlns:def='http://example.com/default' xmlns:foo='http://example.com/foo' foo:id='123'/>" |
|---|
| | 629 | }, { |
|---|
| | 630 | description: "multiple attributes", |
|---|
| | 631 | node: format.createElementNSPlus("def:Node"), |
|---|
| | 632 | attributes: {"id": "123", "foo": "bar"}, |
|---|
| | 633 | expect: "<def:Node xmlns:def='http://example.com/default' id='123' foo='bar'/>" |
|---|
| | 634 | } |
|---|
| | 635 | ]; |
|---|
| | 636 | |
|---|
| | 637 | t.plan(cases.length); |
|---|
| | 638 | var test; |
|---|
| | 639 | for(var i=0; i<cases.length; ++i) { |
|---|
| | 640 | test = cases[i]; |
|---|
| | 641 | format.setAttributes(test.node, test.attributes); |
|---|
| | 642 | t.xml_eq(test.node, test.expect, test.description); |
|---|
| | 643 | } |
|---|
| | 644 | |
|---|
| | 645 | } |
|---|