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

View File

@@ -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' ||

View File

@@ -173,14 +173,12 @@ ol.xml.makeParsersNS = function(namespaceURIs, parsers, opt_parsersNS) {
*/
ol.xml.parse = function(parsersNS, node, objectStack, opt_obj) {
var n;
for (n = node.firstChild; !goog.isNull(n); n = n.nextSibling) {
if (n.nodeType == goog.dom.NodeType.ELEMENT) {
var parsers = parsersNS[n.namespaceURI];
if (goog.isDef(parsers)) {
var parser = parsers[n.localName];
if (goog.isDef(parser)) {
parser.call(opt_obj, n, objectStack);
}
for (n = node.firstElementChild; !goog.isNull(n); n = n.nextElementSibling) {
var parsers = parsersNS[n.namespaceURI];
if (goog.isDef(parsers)) {
var parser = parsers[n.localName];
if (goog.isDef(parser)) {
parser.call(opt_obj, n, objectStack);
}
}
}