Mercurial > addons > firefox-addons > feed-preview
annotate js/feed-parser.js @ 18:15db49e77deb
Fix parsing the description of RSS 0.9 feeds
Add missing namespace to XPath query.
author | Guido Berhoerster <guido+feed-preview@berhoerster.name> |
---|---|
date | Thu, 13 Dec 2018 08:47:06 +0100 |
parents | 48cabd01ef64 |
children | 3fcd2209b39a |
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/', |
15
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
15 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
|
16 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
|
17 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
18 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
|
19 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
20 function encodeXML(str) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
21 return str.replace(/[<>&'"]/g, c => { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
22 switch (c) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
23 case '<': return '<'; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
24 case '>': return '>'; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
25 case '&': return '&'; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
26 case '\'': return '''; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
27 case '"': return '"'; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
28 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
29 }); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
30 } |
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 function parseDate(s) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
33 let date = new Date(s); |
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 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
|
36 } |
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 function parseURL(text, baseURL = '') { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
39 let url; |
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 try { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
42 url = new URL(text, baseURL); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
43 } catch (e) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
44 return null; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
45 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
46 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
|
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 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
50 return url; |
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 |
15
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
53 function base64Decode(base64Str) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
54 let encodedText; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
55 try { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
56 encodedText = atob(base64Str); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
57 } catch (e) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
58 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
|
59 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
60 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
|
61 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
|
62 byteBuffer[i] = encodedText.charCodeAt(i); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
63 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
64 return new TextDecoder().decode(byteBuffer); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
65 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
66 |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
67 function feedNSResolver(prefix) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
68 switch (prefix) { |
15
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
69 case 'atom03': |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
70 return XMLNS.ATOM03; |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
71 case 'atom': |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
72 return XMLNS.ATOM10; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
73 case 'rss': |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
74 return XMLNS.RSS09; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
75 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
76 return null; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
77 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
78 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
79 function feedQueryXPath(feedDocument, scopeElement, xpathQuery) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
80 return feedDocument.evaluate(xpathQuery, scopeElement, feedNSResolver, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
81 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
|
82 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
83 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
84 function feedQueryXPathAll(feedDocument, scopeElement, xpathQuery) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
85 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
|
86 XPathResult.ORDERED_NODE_ITERATOR_TYPE, null); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
87 let nodes = []; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
88 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
|
89 node = result.iterateNext()) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
90 nodes.push(node); |
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 return nodes; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
94 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
95 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
96 export class ParserError extends Error { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
97 constructor() { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
98 super(...arguments); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
99 this.name = this.constructor.name; |
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 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
103 export class UnsupportedFeedTypeError extends Error { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
104 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
|
105 super(message, ...params); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
106 this.name = this.constructor.name; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
107 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
108 } |
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 export class ProtocolError extends Error { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
111 constructor(url, status, statusText, ...params) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
112 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
|
113 `${status} ${statusText}` |
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 this.url = url; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
117 this.status = status; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
118 this.statusText = statusText; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
119 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
120 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
121 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
122 class FeedLogo { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
123 constructor(url, {title = ''} = {}) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
124 this.url = url; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
125 this.title = title; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
126 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
127 } |
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 class FeedEntryFile { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
130 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
|
131 size = 0} = {}) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
132 this.filename = undefined; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
133 this._url = undefined; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
134 this.url = url; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
135 this.type = type; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
136 this.size = size; |
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 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
139 set url(url) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
140 this._url = url; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
141 let filename = url.pathname.split('/').pop(); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
142 this.filename = filename !== '' ? filename : |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
143 browser.i18n.getMessage('defaultFileName'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
144 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
145 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
146 get url() { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
147 return this._url; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
148 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
149 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
150 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
151 class FeedEntry { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
152 constructor({title = browser.i18n.getMessage('defaultFeedEntryTitle'), |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
153 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
|
154 files = []} = {}) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
155 this.title = title; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
156 this.link = link; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
157 this.date = date; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
158 this._content = undefined; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
159 this.content = content; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
160 this.files = files; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
161 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
162 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
163 normalizeContent(text) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
164 if (typeof text === 'undefined') { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
165 return |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
166 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
167 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
168 let contentDocument = document.implementation.createHTMLDocument(); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
169 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
|
170 contentDocument.body = contentDocument.adoptNode(parsedDocument.body); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
171 return new XMLSerializer().serializeToString(contentDocument); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
172 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
173 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
174 set content(content) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
175 this._content = this.normalizeContent(content); |
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 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
178 get content() { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
179 return this._content; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
180 } |
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 class Feed { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
184 constructor(url, {title = browser.i18n.getMessage('defaultFeedTitle'), |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
185 subtitle = '', logo, entries = []} = {}) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
186 this.url = url; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
187 this.title = title; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
188 this.subtitle = subtitle; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
189 this.logo = logo; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
190 this.entries = entries; |
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 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
193 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
194 export class FeedParser { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
195 static probeFeed(feedDocument) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
196 let documentElement = feedDocument.documentElement; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
197 if (documentElement.nodeName === 'feed' && |
15
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
198 documentElement.namespaceURI === XMLNS.ATOM03) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
199 return ['atom', '0.3']; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
200 } else if (documentElement.nodeName === 'feed' && |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
201 documentElement.namespaceURI === XMLNS.ATOM10) { |
15
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
202 return ['atom', '1.0']; |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
203 } else if (documentElement.nodeName === 'rss') { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
204 let version = documentElement.getAttribute('version'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
205 switch (version) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
206 case '0.90': |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
207 case '0.91': |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
208 case '0.92': |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
209 case '0.93': |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
210 case '0.94': |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
211 case '2.0': |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
212 return ['rss', version]; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
213 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
214 } else if (documentElement.localName.toLowerCase() === 'rdf' && |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
215 documentElement.getAttribute('xmlns') === XMLNS.RSS09) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
216 return ['rss', '0.9']; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
217 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
218 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
219 return [undefined, undefined]; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
220 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
221 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
222 constructor() { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
223 this.url = undefined; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
224 this.document = undefined; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
225 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
226 |
15
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
227 parseAtom03ContentConstruct(containerElement, textOnly = true) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
228 let contentType = containerElement.getAttribute('type'); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
229 let contentMode = containerElement.getAttribute('mode'); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
230 if (contentType === null) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
231 contentType = 'text/plain'; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
232 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
233 if (contentMode === null) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
234 contentMode = 'xml'; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
235 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
236 if (contentType === 'application/xhtml+xml') { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
237 let htmlText; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
238 if (contentMode === 'xml') { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
239 return textOnly ? containerElement.textContent.trim() : |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
240 containerElement.innerHTML; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
241 } else if (contentMode === 'escaped') { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
242 htmlText = containerElement.textContent; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
243 } else if (contentMode === 'base64') { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
244 htmlText = base64Decode(containerElement.textContent); |
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 (typeof htmlText === 'undefined') { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
247 return; |
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 (textOnly) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
250 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
|
251 'application/xhtml+xml'); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
252 if (htmlDocument.documentElement.namespaceURI === |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
253 XMLNS.PARSERERROR) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
254 return; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
255 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
256 return htmlDocument.body.textContent.trim(); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
257 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
258 return htmlText; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
259 } else if (contentType === 'text/html') { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
260 let htmlText; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
261 if (contentMode === 'escaped') { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
262 htmlText = containerElement.textContent; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
263 } else if (contentMode === 'base64') { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
264 htmlText = base64Decode(containerElement.textContent); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
265 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
266 if (typeof htmlText === 'undefined') { |
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 if (textOnly) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
270 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
|
271 'text/html'); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
272 return htmlDocument.body.textContent.trim(); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
273 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
274 return htmlText; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
275 } else if (contentType === 'text/plain') { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
276 let text; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
277 if (contentMode === 'escaped') { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
278 text = containerElement.textContent; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
279 } else if (contentMode === 'base64') { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
280 text = base64Decode(containerElement.textContent); |
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 (typeof text === 'undefined') { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
283 return; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
284 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
285 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
|
286 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
287 return; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
288 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
289 |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
290 parseAtom03Content(contentElement) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
291 // 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
|
292 const contentTypes = [ |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
293 'text/plain', |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
294 'text/html', |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
295 'application/xhtml+xml' |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
296 ]; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
297 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
|
298 contentElement.getAttribute('mode') === null) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
299 // 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
|
300 let selectedTypeIndex = -1; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
301 let selectedElement; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
302 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
|
303 if (innerContentElement.localName !== 'content' || |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
304 innerContentElement.namespaceURI !== XMLNS.ATOM03) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
305 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
|
306 ' 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
|
307 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
308 let innerContentType = innerContentElement.getAttribute('type'); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
309 if (innerContentType === null) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
310 innerContentType = 'text/plain'; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
311 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
312 let typeIndex = contentTypes.indexOf(innerContentType); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
313 if (typeIndex > selectedTypeIndex) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
314 selectedTypeIndex = typeIndex; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
315 selectedElement = innerContentElement; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
316 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
317 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
318 if (selectedTypeIndex >= 0) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
319 contentElement = selectedElement; |
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 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
322 |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
323 return this.parseAtom03ContentConstruct(contentElement, false); |
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 |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
326 parseAtom03Entry(entryElement) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
327 let title; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
328 let link; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
329 let date; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
330 let content; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
331 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
|
332 './atom03:title'); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
333 if (titleElement !== null) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
334 title = titleElement.textContent.trim(); |
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 |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
337 let linkElement = feedQueryXPath(this.document, entryElement, |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
338 './atom03:link[@href][@rel="alternate"]'); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
339 if (linkElement !== null) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
340 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
|
341 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
342 |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
343 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
|
344 './atom03:modified'); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
345 if (modifiedElement !== null) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
346 date = parseDate(modifiedElement.textContent); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
347 } |
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 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
|
350 './atom03:content'); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
351 if (contentElement !== null) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
352 try { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
353 content = this.parseAtom03Content(contentElement); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
354 } catch (e) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
355 if (!(e instanceof TypeError)) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
356 throw e; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
357 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
358 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
359 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
360 if (typeof content === 'undefined') { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
361 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
|
362 './atom03:summary'); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
363 if (summaryElement !== null) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
364 try { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
365 content = this.parseAtom03ContentConstruct(summaryElement, |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
366 false); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
367 } catch (e) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
368 if (!(e instanceof TypeError)) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
369 throw e; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
370 } |
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 |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
375 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
|
376 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
377 |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
378 parseAtom03Feed() { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
379 let title; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
380 let subtitle; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
381 let logo; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
382 let entries = []; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
383 let documentElement = this.document.documentElement; |
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 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
|
386 './atom03:title'); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
387 if (titleElement !== null) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
388 try { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
389 title = this.parseAtom03ContentConstruct(titleElement); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
390 } catch (e) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
391 if (!(e instanceof TypeError)) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
392 throw e; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
393 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
394 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
395 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
396 |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
397 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
|
398 './atom03:tagline'); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
399 if (taglineElement !== null) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
400 try { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
401 title = this.parseAtom03ContentConstruct(taglineElement); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
402 } catch (e) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
403 if (!(e instanceof TypeError)) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
404 throw e; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
405 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
406 } |
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 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
|
410 './atom03:logo'); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
411 if (logoElement !== null) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
412 try { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
413 logo = this.parseAtomLogo(logoElement); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
414 } catch (e) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
415 if (!(e instanceof TypeError)) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
416 throw e; |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
417 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
418 } |
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 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
|
422 './atom03:entry'); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
423 for (let entryElement of entryElements) { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
424 entries.push(this.parseAtom03Entry(entryElement)); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
425 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
426 |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
427 return new Feed(this.url, {title, subtitle, logo, entries}); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
428 } |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
429 |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
430 parseAtomLogo(logoElement) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
431 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
|
432 if (url === null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
433 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
|
434 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
435 return new FeedLogo(url); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
436 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
437 |
14
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
438 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
|
439 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
|
440 if (contentType === null) { |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
441 contentType = 'text'; |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
442 } |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
443 |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
444 if (contentType === 'xhtml') { |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
445 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
|
446 if (xhtmlRootElement !== null && |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
447 xhtmlRootElement.localName === 'div' && |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
448 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
|
449 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
|
450 xhtmlRootElement.innerHTML; |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
451 } |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
452 } 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
|
453 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
|
454 if (textOnly) { |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
455 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
|
456 'text/html'); |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
457 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
|
458 } |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
459 return htmlText |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
460 } 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
|
461 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
|
462 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
|
463 } |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
464 |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
465 // unsupported content type |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
466 return; |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
467 } |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
468 |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
469 parseAtomContent(contentElement) { |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
470 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
|
471 if (contentSrc !== null) { |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
472 // 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
|
473 return; |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
474 } |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
475 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
|
476 } |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
477 |
17
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
478 parseAtomEntryFile(enclosureElement) { |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
479 let type; |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
480 let size; |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
481 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
|
482 if (url === null) { |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
483 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
|
484 } |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
485 |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
486 let typeAttribute = enclosureElement.getAttribute('type'); |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
487 if (typeAttribute !== null) { |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
488 type = typeAttribute; |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
489 } |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
490 |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
491 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
|
492 if (!isNaN(length)) { |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
493 size = length; |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
494 } |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
495 |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
496 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
|
497 } |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
498 |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
499 parseAtomEntry(entryElement) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
500 let title; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
501 let link; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
502 let date; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
503 let content; |
17
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
504 let files = []; |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
505 let titleElement = feedQueryXPath(this.document, entryElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
506 './atom:title'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
507 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
|
508 title = this.parseAtomTextConstruct(titleElement); |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
509 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
510 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
511 let linkElement = feedQueryXPath(this.document, entryElement, |
7
2bbb7617dd13
Alternatively use Atom links without the rel attribute
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
6
diff
changeset
|
512 './atom:link[@href][not(@rel) or @rel="alternate"]'); |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
513 if (linkElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
514 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
|
515 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
516 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
517 let updatedElement = feedQueryXPath(this.document, entryElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
518 './atom:updated'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
519 if (updatedElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
520 date = parseDate(updatedElement.textContent); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
521 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
522 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
523 let contentElement = feedQueryXPath(this.document, entryElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
524 './atom:content'); |
14
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
525 if (contentElement !== null) { |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
526 content = this.parseAtomContent(contentElement); |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
527 } |
14
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
528 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
|
529 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
|
530 './atom:summary'); |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
531 if (summaryElement !== null) { |
376a0e415bba
Properly handle non-text content in Atom feed elements
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
7
diff
changeset
|
532 content = this.parseAtomTextConstruct(summaryElement, false); |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
533 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
534 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
535 |
17
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
536 for (let enclosureElement of feedQueryXPathAll(this.document, |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
537 entryElement, './atom:link[@href][@rel="enclosure"]')) { |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
538 try { |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
539 let entryFile = this.parseAtomEntryFile(enclosureElement); |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
540 files.push(entryFile); |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
541 } catch (e) { |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
542 if (!(e instanceof TypeError)) { |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
543 throw e; |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
544 } |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
545 } |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
546 } |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
547 |
48cabd01ef64
Add support for enclosures in Atom feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
15
diff
changeset
|
548 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
|
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 parseAtomFeed() { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
552 let title; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
553 let subtitle; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
554 let logo; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
555 let entries = []; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
556 let documentElement = this.document.documentElement; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
557 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
558 let titleElement = feedQueryXPath(this.document, documentElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
559 './atom:title'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
560 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
|
561 title = this.parseAtomTextConstruct(titleElement); |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
562 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
563 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
564 let subtitleElement = feedQueryXPath(this.document, documentElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
565 './atom:subtitle'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
566 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
|
567 subtitle = this.parseAtomTextConstruct(subtitleElement); |
6
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 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
570 let logoElement = feedQueryXPath(this.document, documentElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
571 './atom:logo'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
572 if (logoElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
573 try { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
574 logo = this.parseAtomLogo(logoElement); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
575 } catch (e) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
576 if (!(e instanceof TypeError)) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
577 throw e; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
578 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
579 } |
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 entryElements = feedQueryXPathAll(this.document, documentElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
583 './atom:entry'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
584 for (let entryElement of entryElements) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
585 entries.push(this.parseAtomEntry(entryElement)); |
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 return new Feed(this.url, {title, subtitle, logo, entries}); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
589 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
590 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
591 parseRSS1Logo(imageElement) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
592 let title; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
593 let urlElement = feedQueryXPath(this.document, imageElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
594 './rss:url'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
595 if (urlElement === null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
596 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
|
597 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
598 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
|
599 if (url === null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
600 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
|
601 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
602 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
603 let titleElement = feedQueryXPath(this.document, imageElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
604 './rss:title'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
605 if (titleElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
606 title = titleElement.textContent.trim(); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
607 } |
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 return new FeedLogo(url, {title}); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
610 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
611 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
612 parseRSS1Entry(itemElement) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
613 let title; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
614 let link; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
615 let titleElement = feedQueryXPath(this.document, itemElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
616 './rss:title'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
617 if (titleElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
618 title = titleElement.textContent; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
619 } |
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 let linkElement = feedQueryXPath(this.document, itemElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
622 './rss:link'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
623 if (linkElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
624 link = parseURL(linkElement.textContent, this.url); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
625 } |
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 return new FeedEntry({title, link}); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
628 } |
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 parseRSS1Feed() { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
631 let title; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
632 let subtitle; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
633 let logo; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
634 let entries = []; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
635 let documentElement = this.document.documentElement; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
636 let titleElement = feedQueryXPath(this.document, documentElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
637 './rss:channel/rss:title'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
638 if (titleElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
639 title = titleElement.textContent; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
640 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
641 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
642 let descriptionElement = feedQueryXPath(this.document, documentElement, |
18
15db49e77deb
Fix parsing the description of RSS 0.9 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
17
diff
changeset
|
643 './rss:channel/rss:description'); |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
644 if (descriptionElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
645 subtitle = descriptionElement.textContent; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
646 } |
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 let imageElement = feedQueryXPath(this.document, documentElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
649 './rss:image'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
650 if (imageElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
651 try { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
652 logo = this.parseRSS1Logo(imageElement); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
653 } catch (e) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
654 if (!(e instanceof TypeError)) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
655 throw e; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
656 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
657 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
658 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
659 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
660 let itemElements = feedQueryXPathAll(this.document, documentElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
661 './rss:item'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
662 for (let itemElement of itemElements) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
663 let entry = this.parseRSS1Entry(itemElement); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
664 if (typeof entry !== 'undefined') { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
665 entries.push(entry); |
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 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
669 return new Feed(this.url, {title, subtitle, logo, entries}); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
670 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
671 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
672 parseRSS2Logo(imageElement) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
673 let title; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
674 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
|
675 if (urlElement === null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
676 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
|
677 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
678 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
|
679 if (url === null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
680 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
|
681 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
682 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
683 let titleElement = feedQueryXPath(this.document, imageElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
684 './title'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
685 if (titleElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
686 title = titleElement.textContent.trim(); |
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 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
689 return new FeedLogo(url, {title}); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
690 } |
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 parseRSS2EntryFile(enclosureElement) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
693 let type; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
694 let size; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
695 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
|
696 if (url === null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
697 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
|
698 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
699 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
700 let typeAttribute = enclosureElement.getAttribute('type'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
701 if (typeAttribute !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
702 type = typeAttribute; |
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 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
705 let length = parseInt(enclosureElement.getAttribute('length'), |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
706 10); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
707 if (!isNaN(length)) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
708 size = length; |
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 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
711 return new FeedEntryFile(url, {type, size}); |
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 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
714 parseRSS2Entry(itemElement) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
715 let title; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
716 let link; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
717 let date; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
718 let content; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
719 let files = []; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
720 let titleElement = feedQueryXPath(this.document, itemElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
721 './title'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
722 if (titleElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
723 title = titleElement.textContent; |
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 linkElement = feedQueryXPath(this.document, itemElement, './link'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
727 if (linkElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
728 link = parseURL(linkElement.textContent, this.url); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
729 } |
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 let pubDateElement = feedQueryXPath(this.document, itemElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
732 './pubDate'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
733 if (pubDateElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
734 date = parseDate(pubDateElement.textContent); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
735 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
736 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
737 let descriptionElement = feedQueryXPath(this.document, itemElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
738 './description'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
739 if (descriptionElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
740 content = descriptionElement.textContent.trim(); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
741 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
742 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
743 for (let enclosureElement of |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
744 feedQueryXPathAll(this.document, itemElement, './enclosure')) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
745 try { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
746 let entryFile = this.parseRSS2EntryFile(enclosureElement); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
747 files.push(entryFile); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
748 } catch (e) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
749 if (!(e instanceof TypeError)) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
750 throw e; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
751 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
752 } |
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 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
|
756 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
757 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
758 parseRSS2Feed() { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
759 let title; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
760 let subtitle; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
761 let logo; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
762 let entries = []; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
763 let documentElement = this.document.documentElement; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
764 let titleElement = feedQueryXPath(this.document, documentElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
765 './channel/title'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
766 if (titleElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
767 title = titleElement.textContent; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
768 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
769 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
770 let descriptionElement = feedQueryXPath(this.document, documentElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
771 './channel/description'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
772 if (descriptionElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
773 subtitle = descriptionElement.textContent; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
774 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
775 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
776 let imageElement = feedQueryXPath(this.document, documentElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
777 './channel/image'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
778 if (imageElement !== null) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
779 try { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
780 logo = this.parseRSS2Logo(imageElement); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
781 } catch (e) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
782 if (!(e instanceof TypeError)) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
783 throw e; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
784 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
785 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
786 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
787 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
788 let itemElements = feedQueryXPathAll(this.document, documentElement, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
789 './channel/item'); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
790 for (let itemElement of itemElements) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
791 let entry = this.parseRSS2Entry(itemElement); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
792 if (typeof entry !== 'undefined') { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
793 entries.push(entry); |
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 } |
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 return new Feed(this.url, {title, subtitle, logo, entries}); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
798 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
799 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
800 parseFromString(xmlString, url) { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
801 this.url = url; |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
802 this.document = new DOMParser().parseFromString(xmlString, |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
803 'application/xml'); |
15
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
804 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
|
805 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
|
806 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
807 |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
808 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
|
809 if (type === 'atom') { |
15
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
810 if (version === '0.3') { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
811 return this.parseAtom03Feed(); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
812 } else if (version === '1.0') { |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
813 return this.parseAtomFeed(); |
150f07c7595f
Add support for Atom 0.3 feeds
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
14
diff
changeset
|
814 } |
6
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
815 } else if (type === 'rss') { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
816 if (version === '0.9') { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
817 return this.parseRSS1Feed(); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
818 } else { |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
819 return this.parseRSS2Feed(); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
820 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
821 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
822 throw new UnsupportedFeedTypeError(); |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
823 } |
5d7c13e998e9
Create feed previews using a stream filter
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff
changeset
|
824 } |