addons/firefox-addons/feed-preview

changeset 17:48cabd01ef64

Add support for enclosures in Atom feeds
author Guido Berhoerster <guido+feed-preview@berhoerster.name>
date Wed Dec 12 22:38:45 2018 +0100 (18 months ago)
parents a59d322e5826
children 15db49e77deb
files js/feed-parser.js
line diff
     1.1 --- a/js/feed-parser.js	Wed Dec 12 10:56:37 2018 +0100
     1.2 +++ b/js/feed-parser.js	Wed Dec 12 22:38:45 2018 +0100
     1.3 @@ -475,11 +475,33 @@
     1.4          return this.parseAtomTextConstruct(contentElement, false);
     1.5      }
     1.6  
     1.7 +    parseAtomEntryFile(enclosureElement) {
     1.8 +        let type;
     1.9 +        let size;
    1.10 +        let url = parseURL(enclosureElement.getAttribute('href'), this.url);
    1.11 +        if (url === null) {
    1.12 +            throw new TypeError('invalid URL in enclosure href attribute');
    1.13 +        }
    1.14 +
    1.15 +        let typeAttribute = enclosureElement.getAttribute('type');
    1.16 +        if (typeAttribute !== null) {
    1.17 +            type = typeAttribute;
    1.18 +        }
    1.19 +
    1.20 +        let length = parseInt(enclosureElement.getAttribute('length'), 10);
    1.21 +        if (!isNaN(length)) {
    1.22 +            size = length;
    1.23 +        }
    1.24 +
    1.25 +        return new FeedEntryFile(url, {type, size});
    1.26 +    }
    1.27 +
    1.28      parseAtomEntry(entryElement) {
    1.29          let title;
    1.30          let link;
    1.31          let date;
    1.32          let content;
    1.33 +        let files = [];
    1.34          let titleElement = feedQueryXPath(this.document, entryElement,
    1.35                  './atom:title');
    1.36          if (titleElement !== null) {
    1.37 @@ -511,7 +533,19 @@
    1.38              }
    1.39          }
    1.40  
    1.41 -        return new FeedEntry({title, link, date, content});
    1.42 +        for (let enclosureElement of feedQueryXPathAll(this.document,
    1.43 +                entryElement, './atom:link[@href][@rel="enclosure"]')) {
    1.44 +            try {
    1.45 +                let entryFile = this.parseAtomEntryFile(enclosureElement);
    1.46 +                files.push(entryFile);
    1.47 +            } catch (e) {
    1.48 +                if (!(e instanceof TypeError)) {
    1.49 +                    throw e;
    1.50 +                }
    1.51 +            }
    1.52 +        }
    1.53 +
    1.54 +        return new FeedEntry({title, link, date, content, files});
    1.55      }
    1.56  
    1.57      parseAtomFeed() {