react-rss
Version:
React component capable of reading and rendering any RSS feed
52 lines (51 loc) • 1.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseToJson = void 0;
var fillWithAttributes = function (object, node) {
if (node.attributes.length > 0) {
object['attributes'] = {};
}
for (var i = 0; i < node.attributes.length; i++) {
object['attributes'][node.attributes.item(i).nodeName] = node.attributes.item(i).nodeValue;
}
};
exports.parseToJson = function (xml, carry) {
if (carry === void 0) { carry = {}; }
xml.childNodes.forEach(function (node) {
if (node instanceof Element) {
var tagName = node.tagName;
if (!carry['children']) {
carry['children'] = {};
}
var children = carry['children'];
if (children[tagName]) {
if (children[tagName] instanceof Array) {
children[tagName].push({});
fillWithAttributes(children[tagName][children[tagName].length - 1], node);
exports.parseToJson(node, children[tagName][children[tagName].length - 1]);
}
else {
children[tagName] = [children[tagName], {}];
fillWithAttributes(children[tagName][1], node);
exports.parseToJson(node, children[tagName][1]);
}
}
else {
children[tagName] = {};
fillWithAttributes(children[tagName], node);
exports.parseToJson(node, children[tagName]);
}
}
else if (node.nodeType === node.TEXT_NODE || node.nodeType === node.CDATA_SECTION_NODE) {
if (!/^\s*$/.test(node.nodeValue)) {
if (!carry['text']) {
carry['text'] = node.nodeValue;
}
else {
carry['text'] += node.nodeValue;
}
}
}
});
return carry.children;
};