From 37dac8e0cf7e1bea56f023373c90f720a8b24e30 Mon Sep 17 00:00:00 2001 From: Brian Reavis Date: Mon, 12 Jan 2015 15:03:06 -0700 Subject: [PATCH] Fixed "Cannot read property 'firstElementChild' of null" on WFS readProjectionFromNode. Occurs when a FeatureCollection is empty. Code style changes @bartvde Added test case for #3118. Attempt to make jshint happy. Fixed tab character. Another code style change (jshint).... --- src/ol/format/wfsformat.js | 7 +++++-- .../spec/ol/format/wfs/EmptyFeatureCollection.xml | 9 +++++++++ test/spec/ol/format/wfsformat.test.js | 15 +++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 test/spec/ol/format/wfs/EmptyFeatureCollection.xml diff --git a/src/ol/format/wfsformat.js b/src/ol/format/wfsformat.js index bede4019cc..3f09348a5b 100644 --- a/src/ol/format/wfsformat.js +++ b/src/ol/format/wfsformat.js @@ -747,8 +747,10 @@ ol.format.WFS.prototype.readProjectionFromDocument = function(doc) { ol.format.WFS.prototype.readProjectionFromNode = function(node) { goog.asserts.assert(node.nodeType == goog.dom.NodeType.ELEMENT); goog.asserts.assert(node.localName == 'FeatureCollection'); - node = node.firstElementChild.firstElementChild; - if (goog.isDefAndNotNull(node)) { + + if (goog.isDefAndNotNull(node.firstElementChild) && + goog.isDefAndNotNull(node.firstElementChild.firstElementChild)) { + node = node.firstElementChild.firstElementChild; for (var n = node.firstElementChild; !goog.isNull(n); n = n.nextElementSibling) { if (!(n.childNodes.length === 0 || @@ -760,5 +762,6 @@ ol.format.WFS.prototype.readProjectionFromNode = function(node) { } } } + return null; }; diff --git a/test/spec/ol/format/wfs/EmptyFeatureCollection.xml b/test/spec/ol/format/wfs/EmptyFeatureCollection.xml new file mode 100644 index 0000000000..631df2bd91 --- /dev/null +++ b/test/spec/ol/format/wfs/EmptyFeatureCollection.xml @@ -0,0 +1,9 @@ + + + diff --git a/test/spec/ol/format/wfsformat.test.js b/test/spec/ol/format/wfsformat.test.js index 09a3f6efb5..a13def7f39 100644 --- a/test/spec/ol/format/wfsformat.test.js +++ b/test/spec/ol/format/wfsformat.test.js @@ -89,6 +89,21 @@ describe('ol.format.WFS', function() { }); + describe('when parsing FeatureCollection', function() { + var xml; + before(function(done) { + afterLoadText('spec/ol/format/wfs/EmptyFeatureCollection.xml', + function(_xml) { + xml = _xml; + done(); + }); + }); + it('returns an empty array of features when none exist', function() { + var result = new ol.format.WFS().readFeatures(xml); + expect(result).to.have.length(0); + }); + }); + describe('when parsing FeatureCollection', function() { var response; before(function(done) {