annotate js/feed-parser.js @ 70:e405ff21ab31 version-14

Release version 14
author Guido Berhoerster <guido+feed-preview@berhoerster.name>
date Sun, 03 Mar 2024 18:12:27 +0100
parents d5506fdb65f0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
1 /*
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
2 * Copyright (C) 2018 Guido Berhoerster <guido+feed-preview@berhoerster.name>
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
3 *
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
4 * This Source Code Form is subject to the terms of the Mozilla Public
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
7 */
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
8
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
9 'use strict';
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
10
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
11 export const XMLNS = {
15
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
12 ATOM03: 'http://purl.org/atom/ns#',
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
13 ATOM10: 'http://www.w3.org/2005/Atom',
14
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
14 RSS09: 'http://my.netscape.com/rdf/simple/0.9/',
19
3fcd2209b39a Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 18
diff changeset
15 RSS10: 'http://purl.org/rss/1.0/',
47
b68880838990 Add support for the RSS Content module to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 31
diff changeset
16 CONTENT: 'http://purl.org/rss/1.0/modules/content/',
48
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
17 DC: 'http://purl.org/dc/elements/1.1/',
15
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
18 XHTML: 'http://www.w3.org/1999/xhtml',
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
19 PARSERERROR: 'http://www.mozilla.org/newlayout/xml/parsererror.xml'
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
20 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
21 const ALLOWED_LINK_PROTOCOLS = new Set(['http:', 'https:', 'ftp:']);
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
22
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
23 function encodeXML(str) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
24 return str.replace(/[<>&'"]/g, c => {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
25 switch (c) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
26 case '<': return '&lt;';
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
27 case '>': return '&gt;';
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
28 case '&': return '&amp;';
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
29 case '\'': return '&apos;';
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
30 case '"': return '&quot;';
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
31 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
32 });
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
33 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
34
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
35 function parseDate(s) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
36 let date = new Date(s);
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
37
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
38 return isNaN(date) ? new Date(0) : date;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
39 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
40
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
41 function parseURL(text, baseURL = '') {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
42 let url;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
43
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
44 try {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
45 url = new URL(text, baseURL);
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
46 } catch (e) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
47 return null;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
48 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
49 if (!ALLOWED_LINK_PROTOCOLS.has(url.protocol)) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
50 return null;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
51 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
52
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
53 return url;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
54 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
55
15
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
56 function base64Decode(base64Str) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
57 let encodedText;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
58 try {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
59 encodedText = atob(base64Str);
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
60 } catch (e) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
61 throw (e instanceof DOMException) ? new TypeError(e.message) : e;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
62 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
63 let byteBuffer = new Uint8Array(new ArrayBuffer(encodedText.length));
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
64 for (let i = 0; i < encodedText.length; i++) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
65 byteBuffer[i] = encodedText.charCodeAt(i);
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
66 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
67 return new TextDecoder().decode(byteBuffer);
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
68 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
69
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
70 function feedNSResolver(prefix) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
71 switch (prefix) {
15
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
72 case 'atom03':
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
73 return XMLNS.ATOM03;
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
74 case 'atom':
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
75 return XMLNS.ATOM10;
19
3fcd2209b39a Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 18
diff changeset
76 case 'rss09':
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
77 return XMLNS.RSS09;
19
3fcd2209b39a Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 18
diff changeset
78 case 'rss10':
3fcd2209b39a Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 18
diff changeset
79 return XMLNS.RSS10;
47
b68880838990 Add support for the RSS Content module to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 31
diff changeset
80 case 'content':
b68880838990 Add support for the RSS Content module to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 31
diff changeset
81 return XMLNS.CONTENT;
48
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
82 case 'dc':
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
83 return XMLNS.DC;
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
84 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
85 return null;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
86 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
87
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
88 function feedQueryXPath(feedDocument, scopeElement, xpathQuery) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
89 return feedDocument.evaluate(xpathQuery, scopeElement, feedNSResolver,
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
90 XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
91 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
92
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
93 function feedQueryXPathAll(feedDocument, scopeElement, xpathQuery) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
94 let result = feedDocument.evaluate(xpathQuery, scopeElement, feedNSResolver,
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
95 XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
96 let nodes = [];
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
97 for (let node = result.iterateNext(); node !== null;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
98 node = result.iterateNext()) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
99 nodes.push(node);
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
100 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
101
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
102 return nodes;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
103 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
104
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
105 export class ParserError extends Error {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
106 constructor() {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
107 super(...arguments);
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
108 this.name = this.constructor.name;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
109 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
110 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
111
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
112 export class UnsupportedFeedTypeError extends Error {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
113 constructor(message = 'Document is not a supported feed', ...params) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
114 super(message, ...params);
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
115 this.name = this.constructor.name;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
116 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
117 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
118
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
119 export class ProtocolError extends Error {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
120 constructor(url, status, statusText, ...params) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
121 let message = `Protocol error: Transfer of ${url} failed with: ` +
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
122 `${status} ${statusText}`
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
123 super(message, ...params);
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
124 this.name = this.constructor.name;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
125 this.url = url;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
126 this.status = status;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
127 this.statusText = statusText;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
128 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
129 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
130
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
131 class FeedLogo {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
132 constructor(url, {title = ''} = {}) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
133 this.url = url;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
134 this.title = title;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
135 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
136 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
137
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
138 class FeedEntryFile {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
139 constructor(url, {type = browser.i18n.getMessage('defaultFileType'),
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
140 size = 0} = {}) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
141 this.filename = undefined;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
142 this._url = undefined;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
143 this.url = url;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
144 this.type = type;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
145 this.size = size;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
146 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
147
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
148 set url(url) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
149 this._url = url;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
150 let filename = url.pathname.split('/').pop();
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
151 this.filename = filename !== '' ? filename :
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
152 browser.i18n.getMessage('defaultFileName');
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
153 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
154
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
155 get url() {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
156 return this._url;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
157 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
158 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
159
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
160 class FeedEntry {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
161 constructor({title = browser.i18n.getMessage('defaultFeedEntryTitle'),
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
162 link = undefined, date = new Date(0), content = '',
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
163 files = []} = {}) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
164 this.title = title;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
165 this.link = link;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
166 this.date = date;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
167 this._content = undefined;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
168 this.content = content;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
169 this.files = files;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
170 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
171
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
172 normalizeContent(text) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
173 if (typeof text === 'undefined') {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
174 return
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
175 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
176
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
177 let contentDocument = document.implementation.createHTMLDocument();
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
178 let parsedDocument = new DOMParser().parseFromString(text, 'text/html');
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
179 contentDocument.body = contentDocument.adoptNode(parsedDocument.body);
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
180 return new XMLSerializer().serializeToString(contentDocument);
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
181 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
182
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
183 set content(content) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
184 this._content = this.normalizeContent(content);
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
185 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
186
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
187 get content() {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
188 return this._content;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
189 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
190 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
191
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
192 class Feed {
31
76e23b361e92 Allow feed reader addons to subscribe to the currently viewed feed
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 28
diff changeset
193 constructor(url, type, {title = browser.i18n.getMessage('defaultFeedTitle'),
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
194 subtitle = '', logo, entries = []} = {}) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
195 this.url = url;
31
76e23b361e92 Allow feed reader addons to subscribe to the currently viewed feed
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 28
diff changeset
196 this.type = type;
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
197 this.title = title;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
198 this.subtitle = subtitle;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
199 this.logo = logo;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
200 this.entries = entries;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
201 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
202 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
203
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
204 export class FeedParser {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
205 static probeFeed(feedDocument) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
206 let documentElement = feedDocument.documentElement;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
207 if (documentElement.nodeName === 'feed' &&
15
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
208 documentElement.namespaceURI === XMLNS.ATOM03) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
209 return ['atom', '0.3'];
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
210 } else if (documentElement.nodeName === 'feed' &&
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
211 documentElement.namespaceURI === XMLNS.ATOM10) {
15
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
212 return ['atom', '1.0'];
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
213 } else if (documentElement.nodeName === 'rss') {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
214 let version = documentElement.getAttribute('version');
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
215 switch (version) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
216 case '0.90':
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
217 case '0.91':
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
218 case '0.92':
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
219 case '0.93':
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
220 case '0.94':
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
221 case '2.0':
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
222 return ['rss', version];
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
223 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
224 } else if (documentElement.localName.toLowerCase() === 'rdf' &&
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
225 documentElement.getAttribute('xmlns') === XMLNS.RSS09) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
226 return ['rss', '0.9'];
19
3fcd2209b39a Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 18
diff changeset
227 } else if (documentElement.localName.toLowerCase() === 'rdf' &&
3fcd2209b39a Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 18
diff changeset
228 documentElement.getAttribute('xmlns') === XMLNS.RSS10) {
3fcd2209b39a Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 18
diff changeset
229 return ['rss', '1.0'];
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
230 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
231
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
232 return [undefined, undefined];
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
233 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
234
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
235 constructor() {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
236 this.url = undefined;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
237 this.document = undefined;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
238 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
239
15
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
240 parseAtom03ContentConstruct(containerElement, textOnly = true) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
241 let contentType = containerElement.getAttribute('type');
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
242 let contentMode = containerElement.getAttribute('mode');
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
243 if (contentType === null) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
244 contentType = 'text/plain';
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
245 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
246 if (contentMode === null) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
247 contentMode = 'xml';
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
248 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
249 if (contentType === 'application/xhtml+xml') {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
250 let htmlText;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
251 if (contentMode === 'xml') {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
252 return textOnly ? containerElement.textContent.trim() :
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
253 containerElement.innerHTML;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
254 } else if (contentMode === 'escaped') {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
255 htmlText = containerElement.textContent;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
256 } else if (contentMode === 'base64') {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
257 htmlText = base64Decode(containerElement.textContent);
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
258 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
259 if (typeof htmlText === 'undefined') {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
260 return;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
261 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
262 if (textOnly) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
263 let htmlDocument = new DOMParser().parseFromString(htmlText,
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
264 'application/xhtml+xml');
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
265 if (htmlDocument.documentElement.namespaceURI ===
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
266 XMLNS.PARSERERROR) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
267 return;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
268 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
269 return htmlDocument.body.textContent.trim();
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
270 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
271 return htmlText;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
272 } else if (contentType === 'text/html') {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
273 let htmlText;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
274 if (contentMode === 'escaped') {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
275 htmlText = containerElement.textContent;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
276 } else if (contentMode === 'base64') {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
277 htmlText = base64Decode(containerElement.textContent);
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
278 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
279 if (typeof htmlText === 'undefined') {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
280 return;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
281 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
282 if (textOnly) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
283 let htmlDocument = new DOMParser().parseFromString(htmlText,
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
284 'text/html');
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
285 return htmlDocument.body.textContent.trim();
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
286 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
287 return htmlText;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
288 } else if (contentType === 'text/plain') {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
289 let text;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
290 if (contentMode === 'escaped') {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
291 text = containerElement.textContent;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
292 } else if (contentMode === 'base64') {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
293 text = base64Decode(containerElement.textContent);
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
294 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
295 if (typeof text === 'undefined') {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
296 return;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
297 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
298 return textOnly ? text : `<pre>${encodeXML(text)}</pre>`;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
299 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
300 return;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
301 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
302
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
303 parseAtom03Content(contentElement) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
304 // ordered from lowest to highest preference
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
305 const contentTypes = [
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
306 'text/plain',
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
307 'text/html',
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
308 'application/xhtml+xml'
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
309 ];
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
310 if (contentElement.getAttribute('type') === 'multipart/alternative' &&
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
311 contentElement.getAttribute('mode') === null) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
312 // select alternative according to above preference
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
313 let selectedTypeIndex = -1;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
314 let selectedElement;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
315 for (let innerContentElement of contentElement.children) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
316 if (innerContentElement.localName !== 'content' ||
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
317 innerContentElement.namespaceURI !== XMLNS.ATOM03) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
318 throw new TypeError('child elements of a multipart ' +
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
319 ' content elements must be content elements');
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
320 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
321 let innerContentType = innerContentElement.getAttribute('type');
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
322 if (innerContentType === null) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
323 innerContentType = 'text/plain';
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
324 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
325 let typeIndex = contentTypes.indexOf(innerContentType);
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
326 if (typeIndex > selectedTypeIndex) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
327 selectedTypeIndex = typeIndex;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
328 selectedElement = innerContentElement;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
329 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
330 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
331 if (selectedTypeIndex >= 0) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
332 contentElement = selectedElement;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
333 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
334 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
335
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
336 return this.parseAtom03ContentConstruct(contentElement, false);
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
337 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
338
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
339 parseAtom03Entry(entryElement) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
340 let title;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
341 let link;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
342 let date;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
343 let content;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
344 let titleElement = feedQueryXPath(this.document, entryElement,
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
345 './atom03:title');
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
346 if (titleElement !== null) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
347 title = titleElement.textContent.trim();
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
348 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
349
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
350 let linkElement = feedQueryXPath(this.document, entryElement,
28
104a1faebd43 Fix XPath query syntax
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 25
diff changeset
351 './atom03:link[@href][contains(concat(" ", ' +
104a1faebd43 Fix XPath query syntax
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 25
diff changeset
352 'normalize-space(@rel), " "), " alternate ")]');
15
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
353 if (linkElement !== null) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
354 link = parseURL(linkElement.getAttribute('href'), this.url);
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
355 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
356
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
357 let modifiedElement = feedQueryXPath(this.document, entryElement,
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
358 './atom03:modified');
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
359 if (modifiedElement !== null) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
360 date = parseDate(modifiedElement.textContent);
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
361 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
362
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
363 let contentElement = feedQueryXPath(this.document, entryElement,
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
364 './atom03:content');
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
365 if (contentElement !== null) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
366 try {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
367 content = this.parseAtom03Content(contentElement);
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
368 } catch (e) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
369 if (!(e instanceof TypeError)) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
370 throw e;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
371 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
372 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
373 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
374 if (typeof content === 'undefined') {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
375 let summaryElement = feedQueryXPath(this.document, entryElement,
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
376 './atom03:summary');
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
377 if (summaryElement !== null) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
378 try {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
379 content = this.parseAtom03ContentConstruct(summaryElement,
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
380 false);
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
381 } catch (e) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
382 if (!(e instanceof TypeError)) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
383 throw e;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
384 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
385 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
386 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
387 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
388
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
389 return new FeedEntry({title, link, date, content});
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
390 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
391
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
392 parseAtom03Feed() {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
393 let title;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
394 let subtitle;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
395 let logo;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
396 let entries = [];
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
397 let documentElement = this.document.documentElement;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
398
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
399 let titleElement = feedQueryXPath(this.document, documentElement,
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
400 './atom03:title');
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
401 if (titleElement !== null) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
402 try {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
403 title = this.parseAtom03ContentConstruct(titleElement);
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
404 } catch (e) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
405 if (!(e instanceof TypeError)) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
406 throw e;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
407 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
408 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
409 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
410
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
411 let taglineElement = feedQueryXPath(this.document, documentElement,
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
412 './atom03:tagline');
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
413 if (taglineElement !== null) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
414 try {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
415 title = this.parseAtom03ContentConstruct(taglineElement);
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
416 } catch (e) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
417 if (!(e instanceof TypeError)) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
418 throw e;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
419 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
420 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
421 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
422
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
423 let logoElement = feedQueryXPath(this.document, documentElement,
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
424 './atom03:logo');
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
425 if (logoElement !== null) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
426 try {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
427 logo = this.parseAtomLogo(logoElement);
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
428 } catch (e) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
429 if (!(e instanceof TypeError)) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
430 throw e;
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
431 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
432 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
433 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
434
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
435 let entryElements = feedQueryXPathAll(this.document, documentElement,
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
436 './atom03:entry');
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
437 for (let entryElement of entryElements) {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
438 entries.push(this.parseAtom03Entry(entryElement));
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
439 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
440
31
76e23b361e92 Allow feed reader addons to subscribe to the currently viewed feed
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 28
diff changeset
441 return new Feed(this.url, 'application/atom+xml',
76e23b361e92 Allow feed reader addons to subscribe to the currently viewed feed
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 28
diff changeset
442 {title, subtitle, logo, entries});
15
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
443 }
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
444
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
445 parseAtomLogo(logoElement) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
446 let url = parseURL(logoElement.textContent.trim(), this.url);
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
447 if (url === null) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
448 throw new TypeError('invalid URL in <logo> element');
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
449 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
450 return new FeedLogo(url);
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
451 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
452
14
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
453 parseAtomTextConstruct(containerElement, textOnly = true) {
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
454 let contentType = containerElement.getAttribute('type');
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
455 if (contentType === null) {
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
456 contentType = 'text';
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
457 }
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
458
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
459 if (contentType === 'xhtml') {
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
460 let xhtmlRootElement = containerElement.firstElementChild;
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
461 if (xhtmlRootElement !== null &&
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
462 xhtmlRootElement.localName === 'div' &&
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
463 xhtmlRootElement.namespaceURI === XMLNS.XHTML) {
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
464 return textOnly ? xhtmlRootElement.textContent.trim() :
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
465 xhtmlRootElement.innerHTML;
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
466 }
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
467 } else if (contentType === 'html') {
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
468 let htmlText = containerElement.textContent;
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
469 if (textOnly) {
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
470 let htmlDocument = new DOMParser().parseFromString(htmlText,
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
471 'text/html');
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
472 return htmlDocument.body.textContent.trim();
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
473 }
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
474 return htmlText
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
475 } else if (contentType === 'text') {
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
476 let text = containerElement.textContent.trim();
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
477 return textOnly ? text : `<pre>${encodeXML(text)}</pre>`;
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
478 }
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
479
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
480 // unsupported content type
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
481 return;
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
482 }
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
483
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
484 parseAtomContent(contentElement) {
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
485 let contentSrc = contentElement.getAttribute('src');
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
486 if (contentSrc !== null) {
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
487 // externally referenced content is not supported
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
488 return;
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
489 }
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
490 return this.parseAtomTextConstruct(contentElement, false);
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
491 }
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
492
17
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
493 parseAtomEntryFile(enclosureElement) {
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
494 let type;
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
495 let size;
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
496 let url = parseURL(enclosureElement.getAttribute('href'), this.url);
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
497 if (url === null) {
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
498 throw new TypeError('invalid URL in enclosure href attribute');
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
499 }
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
500
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
501 let typeAttribute = enclosureElement.getAttribute('type');
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
502 if (typeAttribute !== null) {
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
503 type = typeAttribute;
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
504 }
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
505
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
506 let length = parseInt(enclosureElement.getAttribute('length'), 10);
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
507 if (!isNaN(length)) {
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
508 size = length;
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
509 }
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
510
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
511 return new FeedEntryFile(url, {type, size});
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
512 }
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
513
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
514 parseAtomEntry(entryElement) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
515 let title;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
516 let link;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
517 let date;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
518 let content;
17
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
519 let files = [];
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
520 let titleElement = feedQueryXPath(this.document, entryElement,
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
521 './atom:title');
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
522 if (titleElement !== null) {
14
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
523 title = this.parseAtomTextConstruct(titleElement);
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
524 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
525
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
526 let linkElement = feedQueryXPath(this.document, entryElement,
28
104a1faebd43 Fix XPath query syntax
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 25
diff changeset
527 './atom:link[@href][not(@rel) or contains(concat(" ", ' +
104a1faebd43 Fix XPath query syntax
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 25
diff changeset
528 'normalize-space(@rel), " "), " alternate ")]');
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
529 if (linkElement !== null) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
530 link = parseURL(linkElement.getAttribute('href'), this.url);
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
531 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
532
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
533 let updatedElement = feedQueryXPath(this.document, entryElement,
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
534 './atom:updated');
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
535 if (updatedElement !== null) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
536 date = parseDate(updatedElement.textContent);
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
537 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
538
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
539 let contentElement = feedQueryXPath(this.document, entryElement,
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
540 './atom:content');
14
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
541 if (contentElement !== null) {
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
542 content = this.parseAtomContent(contentElement);
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
543 }
14
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
544 if (typeof content === 'undefined') {
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
545 let summaryElement = feedQueryXPath(this.document, entryElement,
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
546 './atom:summary');
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
547 if (summaryElement !== null) {
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
548 content = this.parseAtomTextConstruct(summaryElement, false);
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
549 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
550 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
551
17
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
552 for (let enclosureElement of feedQueryXPathAll(this.document,
28
104a1faebd43 Fix XPath query syntax
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 25
diff changeset
553 entryElement, './atom:link[@href]' +
104a1faebd43 Fix XPath query syntax
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 25
diff changeset
554 '[contains(concat(" ", normalize-space(@rel), " "), ' +
104a1faebd43 Fix XPath query syntax
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 25
diff changeset
555 '" enclosure ")]')) {
17
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
556 try {
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
557 let entryFile = this.parseAtomEntryFile(enclosureElement);
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
558 files.push(entryFile);
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
559 } catch (e) {
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
560 if (!(e instanceof TypeError)) {
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
561 throw e;
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
562 }
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
563 }
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
564 }
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
565
48cabd01ef64 Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 15
diff changeset
566 return new FeedEntry({title, link, date, content, files});
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
567 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
568
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
569 parseAtomFeed() {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
570 let title;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
571 let subtitle;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
572 let logo;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
573 let entries = [];
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
574 let documentElement = this.document.documentElement;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
575
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
576 let titleElement = feedQueryXPath(this.document, documentElement,
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
577 './atom:title');
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
578 if (titleElement !== null) {
14
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
579 title = this.parseAtomTextConstruct(titleElement);
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
580 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
581
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
582 let subtitleElement = feedQueryXPath(this.document, documentElement,
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
583 './atom:subtitle');
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
584 if (subtitleElement !== null) {
14
376a0e415bba Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 7
diff changeset
585 subtitle = this.parseAtomTextConstruct(subtitleElement);
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
586 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
587
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
588 let logoElement = feedQueryXPath(this.document, documentElement,
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
589 './atom:logo');
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
590 if (logoElement !== null) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
591 try {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
592 logo = this.parseAtomLogo(logoElement);
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
593 } catch (e) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
594 if (!(e instanceof TypeError)) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
595 throw e;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
596 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
597 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
598 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
599
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
600 let entryElements = feedQueryXPathAll(this.document, documentElement,
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
601 './atom:entry');
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
602 for (let entryElement of entryElements) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
603 entries.push(this.parseAtomEntry(entryElement));
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
604 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
605
31
76e23b361e92 Allow feed reader addons to subscribe to the currently viewed feed
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 28
diff changeset
606 return new Feed(this.url, 'application/atom+xml',
76e23b361e92 Allow feed reader addons to subscribe to the currently viewed feed
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 28
diff changeset
607 {title, subtitle, logo, entries});
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
608 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
609
19
3fcd2209b39a Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 18
diff changeset
610 parseRSS1Logo(imageElement, nsPrefix) {
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
611 let title;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
612 let urlElement = feedQueryXPath(this.document, imageElement,
19
3fcd2209b39a Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 18
diff changeset
613 `./${nsPrefix}:url`);
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
614 if (urlElement === null) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
615 throw new TypeError('missing <url> element in <logo> element');
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
616 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
617 let url = parseURL(urlElement.textContent.trim(), this.url);
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
618 if (url === null) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
619 throw new TypeError('invalid URL in <logo> element');
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
620 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
621
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
622 let titleElement = feedQueryXPath(this.document, imageElement,
19
3fcd2209b39a Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 18
diff changeset
623 `./${nsPrefix}:title`);
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
624 if (titleElement !== null) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
625 title = titleElement.textContent.trim();
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
626 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
627
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
628 return new FeedLogo(url, {title});
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
629 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
630
19
3fcd2209b39a Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 18
diff changeset
631 parseRSS1Entry(itemElement, nsPrefix) {
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
632 let title;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
633 let link;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
634 let titleElement = feedQueryXPath(this.document, itemElement,
19
3fcd2209b39a Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 18
diff changeset
635 `./${nsPrefix}:title`);
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
636 if (titleElement !== null) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
637 title = titleElement.textContent;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
638 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
639
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
640 let linkElement = feedQueryXPath(this.document, itemElement,
19
3fcd2209b39a Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 18
diff changeset
641 `./${nsPrefix}:link`);
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
642 if (linkElement !== null) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
643 link = parseURL(linkElement.textContent, this.url);
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
644 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
645
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
646 return new FeedEntry({title, link});
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
647 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
648
19
3fcd2209b39a Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 18
diff changeset
649 parseRSS1Feed(version) {
3fcd2209b39a Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 18
diff changeset
650 let nsPrefix = version === '0.9' ? 'rss09' : 'rss10';
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
651 let title;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
652 let subtitle;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
653 let logo;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
654 let entries = [];
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
655 let documentElement = this.document.documentElement;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
656 let titleElement = feedQueryXPath(this.document, documentElement,
19
3fcd2209b39a Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 18
diff changeset
657 `./${nsPrefix}:channel/${nsPrefix}:title`);
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
658 if (titleElement !== null) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
659 title = titleElement.textContent;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
660 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
661
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
662 let descriptionElement = feedQueryXPath(this.document, documentElement,
19
3fcd2209b39a Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 18
diff changeset
663 `./${nsPrefix}:channel/${nsPrefix}:description`);
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
664 if (descriptionElement !== null) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
665 subtitle = descriptionElement.textContent;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
666 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
667
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
668 let imageElement = feedQueryXPath(this.document, documentElement,
19
3fcd2209b39a Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 18
diff changeset
669 `./${nsPrefix}:image`);
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
670 if (imageElement !== null) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
671 try {
19
3fcd2209b39a Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 18
diff changeset
672 logo = this.parseRSS1Logo(imageElement, nsPrefix);
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
673 } catch (e) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
674 if (!(e instanceof TypeError)) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
675 throw e;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
676 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
677 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
678 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
679
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
680 let itemElements = feedQueryXPathAll(this.document, documentElement,
19
3fcd2209b39a Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 18
diff changeset
681 `./${nsPrefix}:item`);
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
682 for (let itemElement of itemElements) {
19
3fcd2209b39a Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 18
diff changeset
683 let entry = this.parseRSS1Entry(itemElement, nsPrefix);
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
684 if (typeof entry !== 'undefined') {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
685 entries.push(entry);
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
686 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
687 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
688
31
76e23b361e92 Allow feed reader addons to subscribe to the currently viewed feed
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 28
diff changeset
689 return new Feed(this.url, 'application/rss+xml',
76e23b361e92 Allow feed reader addons to subscribe to the currently viewed feed
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 28
diff changeset
690 {title, subtitle, logo, entries});
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
691 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
692
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
693 parseRSS2Logo(imageElement) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
694 let title;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
695 let urlElement = feedQueryXPath(this.document, imageElement, './url');
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
696 if (urlElement === null) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
697 throw new TypeError('missing <url> element in <logo> element');
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
698 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
699 let url = parseURL(urlElement.textContent.trim(), this.url);
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
700 if (url === null) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
701 throw new TypeError('invalid URL in <logo> element');
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
702 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
703
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
704 let titleElement = feedQueryXPath(this.document, imageElement,
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
705 './title');
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
706 if (titleElement !== null) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
707 title = titleElement.textContent.trim();
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
708 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
709
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
710 return new FeedLogo(url, {title});
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
711 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
712
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
713 parseRSS2EntryFile(enclosureElement) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
714 let type;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
715 let size;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
716 let url = parseURL(enclosureElement.getAttribute('url'), this.url);
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
717 if (url === null) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
718 throw new TypeError('invalid URL in <enclosure> element');
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
719 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
720
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
721 let typeAttribute = enclosureElement.getAttribute('type');
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
722 if (typeAttribute !== null) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
723 type = typeAttribute;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
724 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
725
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
726 let length = parseInt(enclosureElement.getAttribute('length'),
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
727 10);
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
728 if (!isNaN(length)) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
729 size = length;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
730 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
731
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
732 return new FeedEntryFile(url, {type, size});
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
733 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
734
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
735 parseRSS2Entry(itemElement) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
736 let title;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
737 let link;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
738 let date;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
739 let content;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
740 let files = [];
48
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
741 let titleElement;
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
742 let dateElement;
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
743 let contentElement;
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
744
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
745 titleElement = feedQueryXPath(this.document, itemElement,
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
746 './dc:title');
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
747 if (titleElement === null) {
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
748 titleElement = feedQueryXPath(this.document, itemElement,
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
749 './title');
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
750 }
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
751 if (titleElement !== null) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
752 title = titleElement.textContent;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
753 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
754
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
755 let linkElement = feedQueryXPath(this.document, itemElement, './link');
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
756 if (linkElement !== null) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
757 link = parseURL(linkElement.textContent, this.url);
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
758 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
759
48
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
760 dateElement = feedQueryXPath(this.document, itemElement, './dc:date');
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
761 if (dateElement === null) {
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
762 dateElement = feedQueryXPath(this.document, itemElement,
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
763 './pubDate');
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
764 }
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
765 if (dateElement !== null) {
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
766 date = parseDate(dateElement.textContent);
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
767 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
768
48
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
769 contentElement = feedQueryXPath(this.document, itemElement,
47
b68880838990 Add support for the RSS Content module to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 31
diff changeset
770 './content:encoded');
48
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
771 if (contentElement === null) {
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
772 contentElement = feedQueryXPath(this.document, itemElement,
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
773 './dc:description');
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
774 }
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
775 if (contentElement === null) {
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
776 contentElement = feedQueryXPath(this.document, itemElement,
47
b68880838990 Add support for the RSS Content module to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 31
diff changeset
777 './description');
48
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
778 }
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
779 if (contentElement !== null) {
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
780 content = contentElement.textContent.trim();
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
781 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
782
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
783 for (let enclosureElement of
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
784 feedQueryXPathAll(this.document, itemElement, './enclosure')) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
785 try {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
786 let entryFile = this.parseRSS2EntryFile(enclosureElement);
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
787 files.push(entryFile);
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
788 } catch (e) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
789 if (!(e instanceof TypeError)) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
790 throw e;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
791 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
792 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
793 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
794
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
795 return new FeedEntry({title, link, date, content, files});
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
796 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
797
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
798 parseRSS2Feed() {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
799 let title;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
800 let subtitle;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
801 let logo;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
802 let entries = [];
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
803 let documentElement = this.document.documentElement;
48
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
804 let titleElement;
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
805 let descriptionElement;
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
806
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
807
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
808 titleElement = feedQueryXPath(this.document, documentElement,
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
809 './channel/dc:title');
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
810 if (titleElement === null) {
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
811 titleElement = feedQueryXPath(this.document, documentElement,
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
812 './channel/title');
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
813 }
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
814 if (titleElement !== null) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
815 title = titleElement.textContent;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
816 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
817
48
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
818 descriptionElement = feedQueryXPath(this.document, documentElement,
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
819 './channel/dc:description');
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
820 if (descriptionElement === null) {
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
821 descriptionElement = feedQueryXPath(this.document, documentElement,
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
822 './channel/description');
d5506fdb65f0 Add support for Dublin Core elements to the RSS 2.0 feed parser
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 47
diff changeset
823 }
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
824 if (descriptionElement !== null) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
825 subtitle = descriptionElement.textContent;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
826 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
827
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
828 let imageElement = feedQueryXPath(this.document, documentElement,
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
829 './channel/image');
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
830 if (imageElement !== null) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
831 try {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
832 logo = this.parseRSS2Logo(imageElement);
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
833 } catch (e) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
834 if (!(e instanceof TypeError)) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
835 throw e;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
836 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
837 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
838 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
839
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
840 let itemElements = feedQueryXPathAll(this.document, documentElement,
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
841 './channel/item');
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
842 for (let itemElement of itemElements) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
843 let entry = this.parseRSS2Entry(itemElement);
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
844 if (typeof entry !== 'undefined') {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
845 entries.push(entry);
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
846 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
847 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
848
31
76e23b361e92 Allow feed reader addons to subscribe to the currently viewed feed
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 28
diff changeset
849 return new Feed(this.url, 'application/rss+xml',
76e23b361e92 Allow feed reader addons to subscribe to the currently viewed feed
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 28
diff changeset
850 {title, subtitle, logo, entries});
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
851 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
852
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
853 parseFromString(xmlString, url) {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
854 this.url = url;
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
855 this.document = new DOMParser().parseFromString(xmlString,
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
856 'application/xml');
15
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
857 if (this.document.documentElement.namespaceURI === XMLNS.PARSERERROR) {
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
858 throw new ParserError(this.document.documentElement.textContent);
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
859 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
860
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
861 let [type, version] = this.constructor.probeFeed(this.document);
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
862 if (type === 'atom') {
15
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
863 if (version === '0.3') {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
864 return this.parseAtom03Feed();
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
865 } else if (version === '1.0') {
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
866 return this.parseAtomFeed();
150f07c7595f Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 14
diff changeset
867 }
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
868 } else if (type === 'rss') {
19
3fcd2209b39a Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 18
diff changeset
869 if (version === '0.9' || version === '1.0') {
3fcd2209b39a Add support for RSS 1.0 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents: 18
diff changeset
870 return this.parseRSS1Feed(version);
6
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
871 } else {
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
872 return this.parseRSS2Feed();
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
873 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
874 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
875 throw new UnsupportedFeedTypeError();
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
876 }
5d7c13e998e9 Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
877 }