Permalink
Browse files

Additional fixes for strange feeds' meta.link & meta.xmlUrl

  • Loading branch information...
1 parent d6313b2 commit 00d91f9ec4d5d4cec2013f86f0db697d657df825 @danmactough committed Dec 26, 2011
Showing with 67 additions and 28 deletions.
  1. +66 −27 lib/feedparser.js
  2. +1 −1 package.json
View
@@ -295,19 +295,33 @@ FeedParser.prototype.handleCloseTag = function (el, scope){
self.meta.title = getValue(self.stack[0].title)
self.meta.description = getValue(self.stack[0].subtitle);
self.meta.pubDate = getValue(self.stack[0].updated) ? new Date(self.stack[0].updated['#']) : null;
- if ( self.stack[0].link && self.stack[0].link.length ) {
- self.stack[0].link.forEach(function(link){
- if (link['@'] && link['@']['rel'] && link['@']['href'])
- switch(link['@']['rel']){
- case('alternate'):
- self.meta.link = link['@']['href'];
- break;
- case('self'):
- self.meta.xmlUrl = link['@']['href'];
- break;
- };
- });
- }
+ ;['link', 'atom:link', 'atom10:link'].forEach(function (linkEl){
+ if ( self.stack[0][linkEl] ) {
+ if ( self.stack[0][linkEl].length ) {
+ self.stack[0][linkEl].forEach(function(link){
+ if (link['@'] && link['@']['rel'] && link['@']['href'])
+ switch(link['@']['rel']){
+ case('alternate'):
+ self.meta.link = link['@']['href'];
+ break;
+ case('self'):
+ self.meta.xmlUrl = link['@']['href'];
+ break;
+ };
+ });
+ } else {
+ if (self.stack[0][linkEl]['@'] && self.stack[0][linkEl]['@']['rel'] && self.stack[0][linkEl]['@']['href'])
+ switch(self.stack[0][linkEl]['@']['rel']){
+ case('alternate'):
+ self.meta.link = self.stack[0][linkEl]['@']['href'];
+ break;
+ case('self'):
+ self.meta.xmlUrl = self.stack[0][linkEl]['@']['href'];
+ break;
+ };
+ }
+ }
+ });
break;
case 'rss':
if (self.stack[0].title) {
@@ -335,6 +349,7 @@ FeedParser.prototype.handleCloseTag = function (el, scope){
}
break;
case('atom:link'):
+ case('atom10:link'):
if (Array.isArray(self.stack[0][el])) {
self.stack[0][el].forEach(function(link){
if(link['@'] && getValue(link['@'], 'rel') == 'self')
@@ -367,6 +382,7 @@ FeedParser.prototype.handleCloseTag = function (el, scope){
self.meta.link = getValue(self.stack[0].channel[el]);
break;
case('atom:link'):
+ case('atom10:link'):
if (Array.isArray(self.stack[0].channel[el])) {
self.stack[0].channel[el].forEach(function(link){
if(link['@'] && getValue(link['@'], 'rel') == 'self')
@@ -455,19 +471,33 @@ FeedParser.prototype.handleCloseTag = function (el, scope){
self.meta.title = getValue(n.title)
self.meta.description = getValue(n.subtitle);
self.meta.pubDate = getValue(n.updated) ? new Date(n.updated['#']) : null;
- if ( n.link && n.link.length ) {
- n.link.forEach(function(link){
- if (link['@'] && link['@']['rel'] && link['@']['href'])
- switch(link['@']['rel']){
- case('alternate'):
- self.meta.link = link['@']['href'];
- break;
- case('self'):
- self.meta.xmlUrl = link['@']['href'];
- break;
- };
- });
- }
+ ;['link', 'atom:link', 'atom10:link'].forEach(function (linkEl){
+ if ( n[linkEl] ) {
+ if ( n[linkEl].length ) {
+ n[linkEl].forEach(function(link){
+ if (link['@'] && link['@']['rel'] && link['@']['href'])
+ switch(link['@']['rel']){
+ case('alternate'):
+ self.meta.link = link['@']['href'];
+ break;
+ case('self'):
+ self.meta.xmlUrl = link['@']['href'];
+ break;
+ };
+ });
+ } else {
+ if (n[linkEl]['@'] && n[linkEl]['@']['rel'] && n[linkEl]['@']['href'])
+ switch(n[linkEl]['@']['rel']){
+ case('alternate'):
+ self.meta.link = n[linkEl]['@']['href'];
+ break;
+ case('self'):
+ self.meta.xmlUrl = n[linkEl]['@']['href'];
+ break;
+ };
+ }
+ }
+ });
break;
case 'rss':
if (n.title) {
@@ -485,9 +515,17 @@ FeedParser.prototype.handleCloseTag = function (el, scope){
self.meta.pubDate = getValue(n[el]) ? new Date(n[el]['#']) : null;
break;
case('link'):
- self.meta.link = getValue(n[el]);
+ if (Array.isArray(n[el])) { // How anyone thinks this is valid is beyond me...
+ n[el].forEach(function(link){
+ if(!self.meta.link && Object.keys(link['@']).length === 0)
+ self.meta.link = getValue(link);
+ });
+ } else {
+ self.meta.link = getValue(n[el]);
+ }
break;
case('atom:link'):
+ case('atom10:link'):
if (Array.isArray(n[el])) {
n[el].forEach(function(link){
if(link['@'] && getValue(link['@'], 'rel') == 'self')
@@ -520,6 +558,7 @@ FeedParser.prototype.handleCloseTag = function (el, scope){
self.meta.link = getValue(n[el]);
break;
case('atom:link'):
+ case('atom10:link'):
if (Array.isArray(n[el])) {
n[el].forEach(function(link){
if(link['@'] && getValue(link['@'], 'rel') == 'self')
View
@@ -2,7 +2,7 @@
"name": "feedparser",
"author": "Dan MacTough <danmactough@gmail.com>",
"description": "Robust RSS, Atom, and RDF feed parsing using sax js",
- "version": "0.4.4",
+ "version": "0.4.5",
"repository": {
"type": "git",
"url": "git://github.com/danmactough/node-feedparser.git"

0 comments on commit 00d91f9

Please sign in to comment.