From 33bf2b746f2e5c527e6f8f2923270984cf43aa63 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Thu, 19 Dec 2013 13:06:18 +0100 Subject: [PATCH] Add ol.format.KML#readName --- src/ol/format/kmlformat.js | 50 +++++++++++++++++++++++ test/spec/ol/format/kmlformat.test.js | 59 +++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) diff --git a/src/ol/format/kmlformat.js b/src/ol/format/kmlformat.js index 3a2a269e40..2319cf4291 100644 --- a/src/ol/format/kmlformat.js +++ b/src/ol/format/kmlformat.js @@ -14,6 +14,7 @@ goog.require('goog.Uri'); goog.require('goog.array'); goog.require('goog.asserts'); goog.require('goog.dom.NodeType'); +goog.require('goog.dom.xml'); goog.require('goog.math'); goog.require('goog.object'); goog.require('goog.string'); @@ -1343,6 +1344,55 @@ ol.format.KML.prototype.readFeaturesFromNode = function(node) { }; +/** + * @param {Document|Node|string} source Souce. + * @return {string|undefined} Name. + */ +ol.format.KML.prototype.readName = function(source) { + if (source instanceof Node) { + return this.readNameFromNode(source); + } else if (goog.isString(source)) { + var doc = goog.dom.xml.loadXml(source); + return this.readNameFromNode(doc); + } else { + goog.asserts.fail(); + 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 && + 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 && + (n.localName == 'Document' || + n.localName == 'Folder' || + n.localName == 'Placemark' || + n.localName == 'kml')) { + var name = this.readNameFromNode(n); + if (goog.isDef(name)) { + return name; + } + } + } + return undefined; +}; + + /** * @inheritDoc */ diff --git a/test/spec/ol/format/kmlformat.test.js b/test/spec/ol/format/kmlformat.test.js index ff4375216d..01a8bea472 100644 --- a/test/spec/ol/format/kmlformat.test.js +++ b/test/spec/ol/format/kmlformat.test.js @@ -1292,6 +1292,65 @@ describe('ol.format.KML', function() { }); + describe('#readName', function() { + + it('returns undefined if there is no name', function() { + var kml = + '' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ''; + expect(format.readName(kml)).to.be(undefined); + }); + + it('returns the name of the first Document', function() { + var kml = + '' + + ' ' + + ' Document name' + + ' ' + + ''; + expect(format.readName(kml)).to.be('Document name'); + }); + + it('returns the name of the first Folder', function() { + var kml = + '' + + ' ' + + ' Folder name' + + ' ' + + ''; + expect(format.readName(kml)).to.be('Folder name'); + }); + + it('returns the name of the first Placemark', function() { + var kml = + '' + + ' ' + + ' Placemark name' + + ' ' + + ''; + expect(format.readName(kml)).to.be('Placemark name'); + }); + + it('searches breadth-first', function() { + var kml = + '' + + ' ' + + ' ' + + ' Placemark name' + + ' ' + + ' Document name' + + ' ' + + ''; + expect(format.readName(kml)).to.be('Document name'); + }); + + }); + });