From 0b4e8bf6f18728b196956252f1aae6b2845d50f5 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Mon, 6 Jan 2014 13:27:37 +0100 Subject: [PATCH] Use firstElementChild in ol.format.KML --- src/ol/format/kmlformat.js | 49 +++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/src/ol/format/kmlformat.js b/src/ol/format/kmlformat.js index 95ab5c65d1..4f0445bbc2 100644 --- a/src/ol/format/kmlformat.js +++ b/src/ol/format/kmlformat.js @@ -1521,12 +1521,11 @@ ol.format.KML.prototype.readFeaturesFromNode = function(node) { } else if (node.localName == 'kml') { features = []; var n; - for (n = node.firstChild; !goog.isNull(n); n = n.nextSibling) { - if (n.nodeType == goog.dom.NodeType.ELEMENT) { - var fs = this.readFeaturesFromNode(n); - if (goog.isDef(fs)) { - goog.array.extend(features, fs); - } + for (n = node.firstElementChild; !goog.isNull(n); + n = n.nextElementSibling) { + var fs = this.readFeaturesFromNode(n); + if (goog.isDef(fs)) { + goog.array.extend(features, fs); } } return features; @@ -1541,11 +1540,13 @@ ol.format.KML.prototype.readFeaturesFromNode = function(node) { * @return {string|undefined} Name. */ ol.format.KML.prototype.readName = function(source) { - if (source instanceof Node) { + if (source instanceof Document) { + return this.readNameFromDocument(source); + } else if (source instanceof Node) { return this.readNameFromNode(source); } else if (goog.isString(source)) { var doc = goog.dom.xml.loadXml(source); - return this.readNameFromNode(doc); + return this.readNameFromDocument(doc); } else { goog.asserts.fail(); return undefined; @@ -1553,24 +1554,40 @@ ol.format.KML.prototype.readName = function(source) { }; +/** + * @param {Document} doc Document. + * @return {string|undefined} Name. + */ +ol.format.KML.prototype.readNameFromDocument = function(doc) { + var n; + for (n = doc.firstChild; !goog.isNull(n); n = n.nextSibling) { + if (n.nodeType == goog.dom.NodeType.ELEMENT) { + var name = this.readNameFromNode(n); + if (goog.isDef(name)) { + return name; + } + } + } + return undefined; +}; + + /** * @param {Node} node Node. * @return {string|undefined} Name. */ ol.format.KML.prototype.readNameFromNode = function(node) { var n; - for (n = node.firstChild; !goog.isNull(n); n = n.nextSibling) { - if (n.nodeType == goog.dom.NodeType.ELEMENT && - goog.array.indexOf( - ol.format.KML.NAMESPACE_URIS_, n.namespaceURI) != -1 && + for (n = node.firstElementChild; !goog.isNull(n); n = n.nextElementSibling) { + if (goog.array.indexOf(ol.format.KML.NAMESPACE_URIS_, + n.namespaceURI) != -1 && n.localName == 'name') { return ol.format.KML.readString_(n); } } - for (n = node.firstChild; !goog.isNull(n); n = n.nextSibling) { - if (n.nodeType == goog.dom.NodeType.ELEMENT && - goog.array.indexOf( - ol.format.KML.NAMESPACE_URIS_, n.namespaceURI) != -1 && + for (n = node.firstElementChild; !goog.isNull(n); n = n.nextElementSibling) { + if (goog.array.indexOf(ol.format.KML.NAMESPACE_URIS_, + n.namespaceURI) != -1 && (n.localName == 'Document' || n.localName == 'Folder' || n.localName == 'Placemark' ||