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