react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / htmlparser2 / lib / FeedHandler.js
80684 viewsvar index = require("./index.js"),1DomHandler = index.DomHandler,2DomUtils = index.DomUtils;34//TODO: make this a streamable handler5function FeedHandler(callback, options){6this.init(callback, options);7}89require("util").inherits(FeedHandler, DomHandler);1011FeedHandler.prototype.init = DomHandler;1213function getElements(what, where){14return DomUtils.getElementsByTagName(what, where, true);15}16function getOneElement(what, where){17return DomUtils.getElementsByTagName(what, where, true, 1)[0];18}19function fetch(what, where, recurse){20return DomUtils.getText(21DomUtils.getElementsByTagName(what, where, recurse, 1)22).trim();23}2425function addConditionally(obj, prop, what, where, recurse){26var tmp = fetch(what, where, recurse);27if(tmp) obj[prop] = tmp;28}2930var isValidFeed = function(value){31return value === "rss" || value === "feed" || value === "rdf:RDF";32};3334FeedHandler.prototype.onend = function(){35var feed = {},36feedRoot = getOneElement(isValidFeed, this.dom),37tmp, childs;3839if(feedRoot){40if(feedRoot.name === "feed"){41childs = feedRoot.children;4243feed.type = "atom";44addConditionally(feed, "id", "id", childs);45addConditionally(feed, "title", "title", childs);46if((tmp = getOneElement("link", childs)) && (tmp = tmp.attribs) && (tmp = tmp.href)) feed.link = tmp;47addConditionally(feed, "description", "subtitle", childs);48if((tmp = fetch("updated", childs))) feed.updated = new Date(tmp);49addConditionally(feed, "author", "email", childs, true);5051feed.items = getElements("entry", childs).map(function(item){52var entry = {}, tmp;5354item = item.children;5556addConditionally(entry, "id", "id", item);57addConditionally(entry, "title", "title", item);58if((tmp = getOneElement("link", item)) && (tmp = tmp.attribs) && (tmp = tmp.href)) entry.link = tmp;59if((tmp = fetch("summary", item) || fetch("content", item))) entry.description = tmp;60if((tmp = fetch("updated", item))) entry.pubDate = new Date(tmp);61return entry;62});63} else {64childs = getOneElement("channel", feedRoot.children).children;6566feed.type = feedRoot.name.substr(0, 3);67feed.id = "";68addConditionally(feed, "title", "title", childs);69addConditionally(feed, "link", "link", childs);70addConditionally(feed, "description", "description", childs);71if((tmp = fetch("lastBuildDate", childs))) feed.updated = new Date(tmp);72addConditionally(feed, "author", "managingEditor", childs, true);7374feed.items = getElements("item", feedRoot.children).map(function(item){75var entry = {}, tmp;7677item = item.children;7879addConditionally(entry, "id", "guid", item);80addConditionally(entry, "title", "title", item);81addConditionally(entry, "link", "link", item);82addConditionally(entry, "description", "description", item);83if((tmp = fetch("pubDate", item))) entry.pubDate = new Date(tmp);84return entry;85});86}87}88this.dom = feed;89DomHandler.prototype._handleCallback.call(90this, feedRoot ? null : Error("couldn't find root of feed")91);92};9394module.exports = FeedHandler;959697