From 640b62adc8e0c5f2c2031315c7f7a2c56dd82500 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Thu, 20 Sep 2007 17:06:04 +0000 Subject: [PATCH] Adding a concatChildValues method to the XML parser. This gets around a messy 4kb limit for text node lengths - over which browsers split values among multiple siblings (see #1006). git-svn-id: http://svn.openlayers.org/trunk/openlayers@4410 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/Format/GML.js | 4 +-- lib/OpenLayers/Format/XML.js | 56 ++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/lib/OpenLayers/Format/GML.js b/lib/OpenLayers/Format/GML.js index 033536120e..36d8f5352c 100644 --- a/lib/OpenLayers/Format/GML.js +++ b/lib/OpenLayers/Format/GML.js @@ -295,7 +295,7 @@ OpenLayers.Format.GML = OpenLayers.Class(OpenLayers.Format.XML, { // look for nodeList = this.getElementsByTagNameNS(node, this.gmlns, "posList"); if(nodeList.length > 0) { - coordString = nodeList[0].firstChild.nodeValue; + coordString = this.concatChildValues(nodeList[0]); coordString = coordString.replace(this.regExes.trimSpace, ""); coords = coordString.split(this.regExes.splitSpace); var dim = parseInt(nodeList[0].getAttribute("dimension")); @@ -314,7 +314,7 @@ OpenLayers.Format.GML = OpenLayers.Class(OpenLayers.Format.XML, { nodeList = this.getElementsByTagNameNS(node, this.gmlns, "coordinates"); if(nodeList.length > 0) { - coordString = nodeList[0].firstChild.nodeValue; + coordString = this.concatChildValues(nodeList[0]); coordString = coordString.replace(this.regExes.trimSpace, ""); coordString = coordString.replace(this.regExes.trimComma, diff --git a/lib/OpenLayers/Format/XML.js b/lib/OpenLayers/Format/XML.js index 44920e06a8..15afffa1a5 100644 --- a/lib/OpenLayers/Format/XML.js +++ b/lib/OpenLayers/Format/XML.js @@ -256,6 +256,62 @@ OpenLayers.Format.XML = OpenLayers.Class(OpenLayers.Format, { } return attributeValue; }, + + /** + * APIMethod: getChildValue + * Get the value of the first child node if it exists, or return an + * optional default string. Returns an empty string if no first child + * exists and no default value is supplied. + * + * Parameters: + * node - {DOMElement} The element used to look for a first child value. + * def - {String} Optional string to return in the event that no + * first child value exists. + * + * Returns: + * {String} The value of the first child of the given node. + */ + getChildValue: function(node, def) { + var value; + try { + value = node.firstChild.nodeValue; + } catch(e) { + value = (def != undefined) ? def : ""; + } + return value; + }, + + /** + * APIMethod: concatChildValues + * Concatenate the value of all child nodes if any exist, or return an + * optional default string. Returns an empty string if no children + * exist and no default value is supplied. Not optimized for large + * numbers of child nodes. + * + * Parameters: + * node - {DOMElement} The element used to look for child values. + * def - {String} Optional string to return in the event that no + * child exist. + * + * Returns: + * {String} The concatenated value of all child nodes of the given node. + */ + concatChildValues: function(node, def) { + var value = ""; + var child = node.firstChild; + var childValue; + while(child) { + childValue = child.nodeValue; + if(childValue) { + value += childValue; + } + child = child.nextSibling; + } + if(value == "" && def != undefined) { + value = def; + } + return value; + }, /** * APIMethod: hasAttributeNS