changeset 17:48cabd01ef64

Add support for enclosures in Atom feeds
author Guido Berhoerster <guido+feed-preview@berhoerster.name>
date Wed, 12 Dec 2018 22:38:45 +0100
parents a59d322e5826
children 15db49e77deb
files js/feed-parser.js
diffstat 1 files changed, 35 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/js/feed-parser.js	Wed Dec 12 10:56:37 2018 +0100
+++ b/js/feed-parser.js	Wed Dec 12 22:38:45 2018 +0100
@@ -475,11 +475,33 @@
         return this.parseAtomTextConstruct(contentElement, false);
     }
 
+    parseAtomEntryFile(enclosureElement) {
+        let type;
+        let size;
+        let url = parseURL(enclosureElement.getAttribute('href'), this.url);
+        if (url === null) {
+            throw new TypeError('invalid URL in enclosure href attribute');
+        }
+
+        let typeAttribute = enclosureElement.getAttribute('type');
+        if (typeAttribute !== null) {
+            type = typeAttribute;
+        }
+
+        let length = parseInt(enclosureElement.getAttribute('length'), 10);
+        if (!isNaN(length)) {
+            size = length;
+        }
+
+        return new FeedEntryFile(url, {type, size});
+    }
+
     parseAtomEntry(entryElement) {
         let title;
         let link;
         let date;
         let content;
+        let files = [];
         let titleElement = feedQueryXPath(this.document, entryElement,
                 './atom:title');
         if (titleElement !== null) {
@@ -511,7 +533,19 @@
             }
         }
 
-        return new FeedEntry({title, link, date, content});
+        for (let enclosureElement of feedQueryXPathAll(this.document,
+                entryElement, './atom:link[@href][@rel="enclosure"]')) {
+            try {
+                let entryFile = this.parseAtomEntryFile(enclosureElement);
+                files.push(entryFile);
+            } catch (e) {
+                if (!(e instanceof TypeError)) {
+                    throw e;
+                }
+            }
+        }
+
+        return new FeedEntry({title, link, date, content, files});
     }
 
     parseAtomFeed() {