From 030f8fdbb9b1b3f943f2f231e5c1df5da8dbde2a Mon Sep 17 00:00:00 2001 From: Pierre GIRAUD Date: Thu, 2 Feb 2012 13:13:05 +0100 Subject: [PATCH 1/3] Adding missing schemalocation, version --- lib/OpenLayers/Format/GPX.js | 47 +++++++++++++++++++++++------------- tests/Format/GPX.html | 12 ++++----- 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/lib/OpenLayers/Format/GPX.js b/lib/OpenLayers/Format/GPX.js index c6a67f9953..401321519a 100644 --- a/lib/OpenLayers/Format/GPX.js +++ b/lib/OpenLayers/Format/GPX.js @@ -57,11 +57,20 @@ OpenLayers.Format.GPX = OpenLayers.Class(OpenLayers.Format.XML, { extractAttributes: true, /** - * APIProperty: gpxns - * {String} GPX namespace to use. Defaults to - * "http://www.topografix.com/GPX/1/1" + * Property: namespaces + * {Object} Mapping of namespace aliases to namespace URIs. */ - gpxns: "http://www.topografix.com/GPX/1/1", + namespaces: { + gpx: "http://www.topografix.com/GPX/1/1", + xsi: "http://www.w3.org/2001/XMLSchema-instance" + }, + + /** + * Property: schemaLocation + * {String} Schema location. Defaults to + * "http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" + */ + schemaLocation: "http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd", /** * Constructor: OpenLayers.Format.GPX @@ -207,17 +216,21 @@ OpenLayers.Format.GPX = OpenLayers.Class(OpenLayers.Format.XML, { * add to the gpx. Supported keys are 'name', 'desc', 'author'. */ write: function(features, metadata) { - var gpx; features = OpenLayers.Util.isArray(features) ? features : [features]; - gpx = this.createElementNS(this.gpxns, "gpx"); + var gpx = this.createElementNS(this.namespaces["gpx"], "gpx"); + gpx.setAttribute("version", "1.1"); + this.setAttributeNS( + gpx, this.namespaces["xsi"], + "xsi:schemaLocation", this.schemaLocation + ); - for(var i=0, len=features.length; i} */ appendAttributesNode: function(node, feature) { - var name = this.createElementNS(this.gpxns, 'name'); + var name = this.createElementNS(this.namespaces["gpx"], 'name'); name.appendChild(this.createTextNode( feature.attributes.name || feature.id)); node.appendChild(name); - var desc = this.createElementNS(this.gpxns, 'desc'); + var desc = this.createElementNS(this.namespaces["gpx"], 'desc'); desc.appendChild(this.createTextNode( feature.attributes.description || this.defaultDesc)); node.appendChild(desc); diff --git a/tests/Format/GPX.html b/tests/Format/GPX.html index 2b88e7a9de..6a86ca6774 100644 --- a/tests/Format/GPX.html +++ b/tests/Format/GPX.html @@ -46,7 +46,7 @@ new OpenLayers.Feature.Vector(point2, {name: 'foo', description: 'bar'}) ]; var data = parser.write(features); - t.xml_eq(data, 'foobarfoobar', 'GPX serializes points correctly'); + t.xml_eq(data, 'foobarfoobar', 'GPX serializes points correctly'); } function test_Format_GPX_serialize_line(t) { t.plan(1); @@ -58,7 +58,7 @@ var line = new OpenLayers.Geometry.LineString([point, point2]); var f = new OpenLayers.Feature.Vector(line, {name: 'foo', description: 'bar'}); var data = parser.write(f); - t.xml_eq(data, 'foobar', 'GPX serializes line correctly'); + t.xml_eq(data, 'foobar', 'GPX serializes line correctly'); } function test_Format_GPX_serialize_lines(t) { t.plan(1); @@ -74,7 +74,7 @@ var f = new OpenLayers.Feature.Vector(line, {name: 'foo', description: 'bar'}); var f2 = new OpenLayers.Feature.Vector(line2, {name: 'dude', description: 'truite'}); var data = parser.write([f, f2]); - t.xml_eq(data, 'foobardudetruite', 'GPX serializes lines correctly'); + t.xml_eq(data, 'foobardudetruite', 'GPX serializes lines correctly'); } function test_Format_GPX_serialize_multiline(t) { t.plan(1); @@ -90,7 +90,7 @@ var multiline = new OpenLayers.Geometry.MultiLineString([line, line2]); var f = new OpenLayers.Feature.Vector(multiline, {name: 'foo', description: 'bar'}); var data = parser.write([f]); - t.xml_eq(data, 'foobar', 'GPX serializes multiline correctly'); + t.xml_eq(data, 'foobar', 'GPX serializes multiline correctly'); } function test_Format_GPX_serialize_polygon(t) { t.plan(1); @@ -103,7 +103,7 @@ var polygon = new OpenLayers.Geometry.Polygon([linearRing]); var f = new OpenLayers.Feature.Vector(polygon, {name: 'foo', description: 'bar'}); var data = parser.write([f]); - t.xml_eq(data, 'foobar', 'GPX serializes polygon correctly'); + t.xml_eq(data, 'foobar', 'GPX serializes polygon correctly'); } function test_Format_GPX_serialize_metadata(t) { t.plan(1); @@ -111,7 +111,7 @@ var parser = new OpenLayers.Format.GPX(); var data = parser.write([], {name: 'foo', desc: 'bar'}); - t.xml_eq(data, 'foobar', 'GPX serializes metadata correctly'); + t.xml_eq(data, 'foobar', 'GPX serializes metadata correctly'); } From 92f8040938b39e4b8ae9e332622ed559fd9e23c0 Mon Sep 17 00:00:00 2001 From: Pierre GIRAUD Date: Wed, 8 Feb 2012 10:39:31 +0100 Subject: [PATCH 2/3] More up-to-date way to create or set namespacedj elements or attributes. --- lib/OpenLayers/Format/GPX.js | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/lib/OpenLayers/Format/GPX.js b/lib/OpenLayers/Format/GPX.js index 401321519a..f3f0fce313 100644 --- a/lib/OpenLayers/Format/GPX.js +++ b/lib/OpenLayers/Format/GPX.js @@ -218,12 +218,11 @@ OpenLayers.Format.GPX = OpenLayers.Class(OpenLayers.Format.XML, { write: function(features, metadata) { features = OpenLayers.Util.isArray(features) ? features : [features]; - var gpx = this.createElementNS(this.namespaces["gpx"], "gpx"); + var gpx = this.createElementNSPlus("gpx:gpx"); gpx.setAttribute("version", "1.1"); - this.setAttributeNS( - gpx, this.namespaces["xsi"], - "xsi:schemaLocation", this.schemaLocation - ); + this.setAttributes(gpx, { + "xsi:schemaLocation": this.schemaLocation + }); if (metadata && typeof metadata == 'object') { gpx.appendChild(this.buildMetadataNode(metadata)); @@ -243,11 +242,11 @@ OpenLayers.Format.GPX = OpenLayers.Class(OpenLayers.Format.XML, { */ buildMetadataNode: function(metadata) { var types = ['name', 'desc', 'author'], - node = this.createElementNS(this.namespaces["gpx"], 'metadata'); + node = this.createElementNSPlus('gpx:metadata'); for (var i=0; i < types.length; i++) { var type = types[i]; if (metadata[type]) { - var n = this.createElementNS(this.namespaces["gpx"], type); + var n = this.createElementNSPlus("gpx:" + type); n.appendChild(this.createTextNode(metadata[type])); node.appendChild(n); } @@ -276,7 +275,7 @@ OpenLayers.Format.GPX = OpenLayers.Class(OpenLayers.Format.XML, { var wpt = this.buildWptNode(feature); return wpt; } else { - var trkNode = this.createElementNS(this.namespaces["gpx"], "trk"); + var trkNode = this.createElementNSPlus("gpx:trk"); this.appendAttributesNode(trkNode, feature); var trkSegNodes = this.buildTrkSegNode(geometry); trkSegNodes = OpenLayers.Util.isArray(trkSegNodes) ? @@ -304,7 +303,7 @@ OpenLayers.Format.GPX = OpenLayers.Class(OpenLayers.Format.XML, { nodes; if (geometry.CLASS_NAME == "OpenLayers.Geometry.LineString" || geometry.CLASS_NAME == "OpenLayers.Geometry.LinearRing") { - node = this.createElementNS(this.namespaces["gpx"], "trkseg"); + node = this.createElementNSPlus("gpx:trkseg"); for (i = 0, len=geometry.components.length; i < len; i++) { point = geometry.components[i]; node.appendChild(this.buildTrkPtNode(point)); @@ -330,7 +329,7 @@ OpenLayers.Format.GPX = OpenLayers.Class(OpenLayers.Format.XML, { * {DOMElement} A trkpt node */ buildTrkPtNode: function(point) { - var node = this.createElementNS(this.namespaces["gpx"], "trkpt"); + var node = this.createElementNSPlus("gpx:trkpt"); node.setAttribute("lon", point.x); node.setAttribute("lat", point.y); return node; @@ -347,7 +346,7 @@ OpenLayers.Format.GPX = OpenLayers.Class(OpenLayers.Format.XML, { * {DOMElement} A wpt node */ buildWptNode: function(feature) { - var node = this.createElementNS(this.namespaces["gpx"], "wpt"); + var node = this.createElementNSPlus("gpx:wpt"); node.setAttribute("lon", feature.geometry.x); node.setAttribute("lat", feature.geometry.y); this.appendAttributesNode(node, feature); @@ -363,11 +362,11 @@ OpenLayers.Format.GPX = OpenLayers.Class(OpenLayers.Format.XML, { * feature - {} */ appendAttributesNode: function(node, feature) { - var name = this.createElementNS(this.namespaces["gpx"], 'name'); + var name = this.createElementNSPlus('gpx:name'); name.appendChild(this.createTextNode( feature.attributes.name || feature.id)); node.appendChild(name); - var desc = this.createElementNS(this.namespaces["gpx"], 'desc'); + var desc = this.createElementNSPlus('gpx:desc'); desc.appendChild(this.createTextNode( feature.attributes.description || this.defaultDesc)); node.appendChild(desc); From 734f150f4f3cd3c67d665ce4411218b3855eaafe Mon Sep 17 00:00:00 2001 From: Pierre GIRAUD Date: Wed, 8 Feb 2012 10:50:38 +0100 Subject: [PATCH 3/3] Adding behavior change node --- notes/2.12.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/notes/2.12.md b/notes/2.12.md index ee5ae8843c..eaf6985f3d 100644 --- a/notes/2.12.md +++ b/notes/2.12.md @@ -22,6 +22,10 @@ People can override this rule to use other transition settings. To remove tile a # Behavior Changes from Past Releases +## GPX API change + +The `gpxns` API property has been removed. The GPX namespace is now defined in the `namespaces` property but is not intended to be overriden. + ## Function return values [Previously][prev] a few functions in the library displayed error messages and returned `undefined`, `null` or `false` if the parameters passed in were bad. In 2.12 these functions now just throw an error/exception. People relying on return values to know if a function call is successful may need to change their code. Here are the modified functions: