addons/firefox-addons/feed-preview

changeset 19:3fcd2209b39a

Add support for RSS 1.0 feeds
author Guido Berhoerster <guido+feed-preview@berhoerster.name>
date Thu Dec 13 09:02:40 2018 +0100 (17 months ago)
parents 15db49e77deb
children a6625fd9a30d
files js/feed-parser.js
line diff
     1.1 --- a/js/feed-parser.js	Thu Dec 13 08:47:06 2018 +0100
     1.2 +++ b/js/feed-parser.js	Thu Dec 13 09:02:40 2018 +0100
     1.3 @@ -12,6 +12,7 @@
     1.4      ATOM03: 'http://purl.org/atom/ns#',
     1.5      ATOM10: 'http://www.w3.org/2005/Atom',
     1.6      RSS09: 'http://my.netscape.com/rdf/simple/0.9/',
     1.7 +    RSS10: 'http://purl.org/rss/1.0/',
     1.8      XHTML: 'http://www.w3.org/1999/xhtml',
     1.9      PARSERERROR: 'http://www.mozilla.org/newlayout/xml/parsererror.xml'
    1.10  }
    1.11 @@ -70,8 +71,10 @@
    1.12              return XMLNS.ATOM03;
    1.13          case 'atom':
    1.14              return XMLNS.ATOM10;
    1.15 -        case 'rss':
    1.16 +        case 'rss09':
    1.17              return XMLNS.RSS09;
    1.18 +        case 'rss10':
    1.19 +            return XMLNS.RSS10;
    1.20      }
    1.21      return null;
    1.22  }
    1.23 @@ -214,6 +217,9 @@
    1.24          } else if (documentElement.localName.toLowerCase() === 'rdf' &&
    1.25                  documentElement.getAttribute('xmlns') === XMLNS.RSS09) {
    1.26              return ['rss', '0.9'];
    1.27 +        } else if (documentElement.localName.toLowerCase() === 'rdf' &&
    1.28 +                documentElement.getAttribute('xmlns') === XMLNS.RSS10) {
    1.29 +            return ['rss', '1.0'];
    1.30          }
    1.31  
    1.32          return [undefined, undefined];
    1.33 @@ -588,10 +594,10 @@
    1.34          return new Feed(this.url, {title, subtitle, logo, entries});
    1.35      }
    1.36  
    1.37 -    parseRSS1Logo(imageElement) {
    1.38 +    parseRSS1Logo(imageElement, nsPrefix) {
    1.39          let title;
    1.40          let urlElement = feedQueryXPath(this.document, imageElement,
    1.41 -                './rss:url');
    1.42 +                `./${nsPrefix}:url`);
    1.43          if (urlElement === null) {
    1.44              throw new TypeError('missing <url> element in <logo> element');
    1.45          }
    1.46 @@ -601,7 +607,7 @@
    1.47          }
    1.48  
    1.49          let titleElement = feedQueryXPath(this.document, imageElement,
    1.50 -                './rss:title');
    1.51 +                `./${nsPrefix}:title`);
    1.52          if (titleElement !== null) {
    1.53              title = titleElement.textContent.trim();
    1.54          }
    1.55 @@ -609,17 +615,17 @@
    1.56          return new FeedLogo(url, {title});
    1.57      }
    1.58  
    1.59 -    parseRSS1Entry(itemElement) {
    1.60 +    parseRSS1Entry(itemElement, nsPrefix) {
    1.61          let title;
    1.62          let link;
    1.63          let titleElement = feedQueryXPath(this.document, itemElement,
    1.64 -                './rss:title');
    1.65 +                `./${nsPrefix}:title`);
    1.66          if (titleElement !== null) {
    1.67              title = titleElement.textContent;
    1.68          }
    1.69  
    1.70          let linkElement = feedQueryXPath(this.document, itemElement,
    1.71 -                './rss:link');
    1.72 +                `./${nsPrefix}:link`);
    1.73          if (linkElement !== null) {
    1.74              link = parseURL(linkElement.textContent, this.url);
    1.75          }
    1.76 @@ -627,29 +633,30 @@
    1.77          return new FeedEntry({title, link});
    1.78      }
    1.79  
    1.80 -    parseRSS1Feed() {
    1.81 +    parseRSS1Feed(version) {
    1.82 +        let nsPrefix = version === '0.9' ? 'rss09' : 'rss10';
    1.83          let title;
    1.84          let subtitle;
    1.85          let logo;
    1.86          let entries = [];
    1.87          let documentElement = this.document.documentElement;
    1.88          let titleElement = feedQueryXPath(this.document, documentElement,
    1.89 -                './rss:channel/rss:title');
    1.90 +                `./${nsPrefix}:channel/${nsPrefix}:title`);
    1.91          if (titleElement !== null) {
    1.92              title = titleElement.textContent;
    1.93          }
    1.94  
    1.95          let descriptionElement = feedQueryXPath(this.document, documentElement,
    1.96 -                './rss:channel/rss:description');
    1.97 +                `./${nsPrefix}:channel/${nsPrefix}:description`);
    1.98          if (descriptionElement !== null) {
    1.99              subtitle = descriptionElement.textContent;
   1.100          }
   1.101  
   1.102          let imageElement = feedQueryXPath(this.document, documentElement,
   1.103 -                './rss:image');
   1.104 +                `./${nsPrefix}:image`);
   1.105          if (imageElement !== null) {
   1.106              try {
   1.107 -                logo = this.parseRSS1Logo(imageElement);
   1.108 +                logo = this.parseRSS1Logo(imageElement, nsPrefix);
   1.109              } catch (e) {
   1.110                  if (!(e instanceof TypeError)) {
   1.111                      throw e;
   1.112 @@ -658,9 +665,9 @@
   1.113          }
   1.114  
   1.115          let itemElements = feedQueryXPathAll(this.document, documentElement,
   1.116 -                './rss:item');
   1.117 +                `./${nsPrefix}:item`);
   1.118          for (let itemElement of itemElements) {
   1.119 -            let entry = this.parseRSS1Entry(itemElement);
   1.120 +            let entry = this.parseRSS1Entry(itemElement, nsPrefix);
   1.121              if (typeof entry !== 'undefined') {
   1.122                  entries.push(entry);
   1.123              }
   1.124 @@ -813,8 +820,8 @@
   1.125                  return this.parseAtomFeed();
   1.126              }
   1.127          } else if (type === 'rss') {
   1.128 -            if (version === '0.9') {
   1.129 -                return this.parseRSS1Feed();
   1.130 +            if (version === '0.9' || version === '1.0') {
   1.131 +                return this.parseRSS1Feed(version);
   1.132              } else {
   1.133                  return this.parseRSS2Feed();
   1.134              }