From 5904d1568080b66dbfc1a5d358c21426a32c0244 Mon Sep 17 00:00:00 2001 From: crschmidt Date: Wed, 3 Oct 2007 21:15:09 +0000 Subject: [PATCH] KML should use the correct namespace for the placemark it is processing. This is slightly more expensive for parsing, but leads to the ability to load almost all KML Placemarks with geometries on the web, instead of a small portion of them. git-svn-id: http://svn.openlayers.org/trunk/openlayers@4797 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/Format/KML.js | 20 +++++++++++++++----- tests/Format/test_KML.html | 11 ++++++++++- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/OpenLayers/Format/KML.js b/lib/OpenLayers/Format/KML.js index 2f64c23b3b..210c85b9d4 100644 --- a/lib/OpenLayers/Format/KML.js +++ b/lib/OpenLayers/Format/KML.js @@ -48,6 +48,13 @@ OpenLayers.Format.KML = OpenLayers.Class(OpenLayers.Format.XML, { * {Boolean} Extract attributes from KML. Default is true. */ extractAttributes: true, + + /** + * Property: internalns + * {String} KML Namespace to use -- defaults to the namespace of the + * Placemark node being parsed, but falls back to kmlns. + */ + internalns: null, /** * Constructor: OpenLayers.Format.KML @@ -83,7 +90,7 @@ OpenLayers.Format.KML = OpenLayers.Class(OpenLayers.Format.XML, { data = OpenLayers.Format.XML.prototype.read.apply(this, [data]); } var featureNodes = this.getElementsByTagNameNS(data, - this.kmlns, + '*', "Placemark"); var numFeatures = featureNodes.length; var features = new Array(numFeatures); @@ -116,7 +123,10 @@ OpenLayers.Format.KML = OpenLayers.Class(OpenLayers.Format.XML, { var type, nodeList, geometry, parser; for(var i=0; i 0) { // only deal with first geometry of this type var parser = this.parseGeometry[type.toLowerCase()]; @@ -165,7 +175,7 @@ OpenLayers.Format.KML = OpenLayers.Class(OpenLayers.Format.XML, { * {} A point geometry. */ point: function(node) { - var nodeList = this.getElementsByTagNameNS(node, this.kmlns, + var nodeList = this.getElementsByTagNameNS(node, this.internalns, "coordinates"); var coords = []; if(nodeList.length > 0) { @@ -200,7 +210,7 @@ OpenLayers.Format.KML = OpenLayers.Class(OpenLayers.Format.XML, { * {} A linestring geometry. */ linestring: function(node, ring) { - var nodeList = this.getElementsByTagNameNS(node, this.kmlns, + var nodeList = this.getElementsByTagNameNS(node, this.internalns, "coordinates"); var line = null; if(nodeList.length > 0) { @@ -254,7 +264,7 @@ OpenLayers.Format.KML = OpenLayers.Class(OpenLayers.Format.XML, { * {} A polygon geometry. */ polygon: function(node) { - var nodeList = this.getElementsByTagNameNS(node, this.kmlns, + var nodeList = this.getElementsByTagNameNS(node, this.internalns, "LinearRing"); var numRings = nodeList.length; var components = new Array(numRings); diff --git a/tests/Format/test_KML.html b/tests/Format/test_KML.html index 1438106b6c..23cce029f8 100644 --- a/tests/Format/test_KML.html +++ b/tests/Format/test_KML.html @@ -28,7 +28,7 @@ "read geometry collection"); } - function test_Format_KML_readCdataAttributes(t) { + function test_Format_KML_readCdataAttributes_20(t) { t.plan(2); var cdata = '#rel1.0 17.266666, 48.283333'; var features = (new OpenLayers.Format.KML()).read(cdata); @@ -37,6 +37,15 @@ } + function test_Format_KML_readCdataAttributes_21(t) { + t.plan(2); + var cdata = '#rel1.0 17.266666, 48.283333'; + var features = (new OpenLayers.Format.KML()).read(cdata); + t.eq(features[0].attributes.description, "Full of text.", "Description attribute in cdata read correctly"); + t.eq(features[0].attributes.name, "Pezinok", "title attribute in cdata read correctly"); + + } + function test_Format_KML_write(t) { // make sure id, name, and description are preserved t.plan(1);