From cfafe90235403c3df30e51b3a3f516c74ad1e0f9 Mon Sep 17 00:00:00 2001 From: Nick Hamblet Date: Mon, 22 Jun 2015 16:54:27 -0400 Subject: [PATCH] Handle CDATA in attribute parsing for GML format Following [3827](https://github.com/openlayers/ol3/issues/3827), handle CDATA XML nodes in attribute parsing of GML data. Currently such data will be expected to be a geometry, and will fail to parse. Treating the CDATA node as text is the easiest way to handle such an attribute. --- src/ol/format/gml/gmlbaseformat.js | 5 ++-- test/spec/ol/format/gmlformat.test.js | 41 +++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/ol/format/gml/gmlbaseformat.js b/src/ol/format/gml/gmlbaseformat.js index 1878296f8f..a65717cff8 100644 --- a/src/ol/format/gml/gmlbaseformat.js +++ b/src/ol/format/gml/gmlbaseformat.js @@ -203,10 +203,11 @@ ol.format.GMLBase.prototype.readFeatureElement = function(node, objectStack) { n = n.nextElementSibling) { var localName = ol.xml.getLocalName(n); // Assume attribute elements have one child node and that the child - // is a text node. Otherwise assume it is a geometry node. + // is a text or CDATA node (to be treated as text). + // Otherwise assume it is a geometry node. if (n.childNodes.length === 0 || (n.childNodes.length === 1 && - n.firstChild.nodeType === 3)) { + (n.firstChild.nodeType === 3 || n.firstChild.nodeType === 4))) { var value = ol.xml.getAllTextContent(n, false); if (goog.string.isEmpty(value)) { value = undefined; diff --git a/test/spec/ol/format/gmlformat.test.js b/test/spec/ol/format/gmlformat.test.js index 8597ad26ce..0080cbed75 100644 --- a/test/spec/ol/format/gmlformat.test.js +++ b/test/spec/ol/format/gmlformat.test.js @@ -907,6 +907,47 @@ describe('ol.format.GML3', function() { }); }); + describe('when parsing CDATA attribute', function() { + var features; + before(function(done) { + try { + var text = + '' + + ' ' + + ' Aflu' + + ' ' + + ' ' + + ' 34.12 2.09' + + ' ' + + ' ' + + ' 84683' + + ' Algeria' + + ' place' + + ' Aflu' + + ' b]]>' + + ' ' + + ''; + var config = { + 'featureNS': 'http://www.openplans.org/topp', + 'featureType': 'gnis_pop' + }; + features = new ol.format.GML(config).readFeatures(text); + } catch (e) { + done(e); + } + done(); + }); + + it('creates 1 feature', function() { + expect(features).to.have.length(1); + }); + + it('converts XML attribute to text', function() { + expect(features[0].get('cdata')).to.be('b'); + }); + }); + describe('when parsing TOPP states WFS with autoconfigure', function() { var features, text, gmlFormat; before(function(done) {