Merge pull request #1466 from twpayne/vector-api-first-element-child

[vector-api] Use firstElementChild/nextElementSibling in XML parsers
This commit is contained in:
Tom Payne
2014-01-06 04:49:51 -08:00
2 changed files with 39 additions and 24 deletions
+33 -16
View File
@@ -1521,12 +1521,11 @@ ol.format.KML.prototype.readFeaturesFromNode = function(node) {
} else if (node.localName == 'kml') { } else if (node.localName == 'kml') {
features = []; features = [];
var n; var n;
for (n = node.firstChild; !goog.isNull(n); n = n.nextSibling) { for (n = node.firstElementChild; !goog.isNull(n);
if (n.nodeType == goog.dom.NodeType.ELEMENT) { n = n.nextElementSibling) {
var fs = this.readFeaturesFromNode(n); var fs = this.readFeaturesFromNode(n);
if (goog.isDef(fs)) { if (goog.isDef(fs)) {
goog.array.extend(features, fs); goog.array.extend(features, fs);
}
} }
} }
return features; return features;
@@ -1541,11 +1540,13 @@ ol.format.KML.prototype.readFeaturesFromNode = function(node) {
* @return {string|undefined} Name. * @return {string|undefined} Name.
*/ */
ol.format.KML.prototype.readName = function(source) { 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); return this.readNameFromNode(source);
} else if (goog.isString(source)) { } else if (goog.isString(source)) {
var doc = goog.dom.xml.loadXml(source); var doc = goog.dom.xml.loadXml(source);
return this.readNameFromNode(doc); return this.readNameFromDocument(doc);
} else { } else {
goog.asserts.fail(); goog.asserts.fail();
return undefined; 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. * @param {Node} node Node.
* @return {string|undefined} Name. * @return {string|undefined} Name.
*/ */
ol.format.KML.prototype.readNameFromNode = function(node) { ol.format.KML.prototype.readNameFromNode = function(node) {
var n; var n;
for (n = node.firstChild; !goog.isNull(n); n = n.nextSibling) { for (n = node.firstElementChild; !goog.isNull(n); n = n.nextElementSibling) {
if (n.nodeType == goog.dom.NodeType.ELEMENT && if (goog.array.indexOf(ol.format.KML.NAMESPACE_URIS_,
goog.array.indexOf( n.namespaceURI) != -1 &&
ol.format.KML.NAMESPACE_URIS_, n.namespaceURI) != -1 &&
n.localName == 'name') { n.localName == 'name') {
return ol.format.KML.readString_(n); return ol.format.KML.readString_(n);
} }
} }
for (n = node.firstChild; !goog.isNull(n); n = n.nextSibling) { for (n = node.firstElementChild; !goog.isNull(n); n = n.nextElementSibling) {
if (n.nodeType == goog.dom.NodeType.ELEMENT && if (goog.array.indexOf(ol.format.KML.NAMESPACE_URIS_,
goog.array.indexOf( n.namespaceURI) != -1 &&
ol.format.KML.NAMESPACE_URIS_, n.namespaceURI) != -1 &&
(n.localName == 'Document' || (n.localName == 'Document' ||
n.localName == 'Folder' || n.localName == 'Folder' ||
n.localName == 'Placemark' || n.localName == 'Placemark' ||
+6 -8
View File
@@ -173,14 +173,12 @@ ol.xml.makeParsersNS = function(namespaceURIs, parsers, opt_parsersNS) {
*/ */
ol.xml.parse = function(parsersNS, node, objectStack, opt_obj) { ol.xml.parse = function(parsersNS, node, objectStack, opt_obj) {
var n; var n;
for (n = node.firstChild; !goog.isNull(n); n = n.nextSibling) { for (n = node.firstElementChild; !goog.isNull(n); n = n.nextElementSibling) {
if (n.nodeType == goog.dom.NodeType.ELEMENT) { var parsers = parsersNS[n.namespaceURI];
var parsers = parsersNS[n.namespaceURI]; if (goog.isDef(parsers)) {
if (goog.isDef(parsers)) { var parser = parsers[n.localName];
var parser = parsers[n.localName]; if (goog.isDef(parser)) {
if (goog.isDef(parser)) { parser.call(opt_obj, n, objectStack);
parser.call(opt_obj, n, objectStack);
}
} }
} }
} }