comparison js/feed-parser.js @ 17:48cabd01ef64

Add support for enclosures in Atom feeds
author Guido Berhoerster <guido+feed-preview@berhoerster.name>
date Wed, 12 Dec 2018 22:38:45 +0100
parents 150f07c7595f
children 15db49e77deb
comparison
equal deleted inserted replaced
16:a59d322e5826 17:48cabd01ef64
473 return; 473 return;
474 } 474 }
475 return this.parseAtomTextConstruct(contentElement, false); 475 return this.parseAtomTextConstruct(contentElement, false);
476 } 476 }
477 477
478 parseAtomEntryFile(enclosureElement) {
479 let type;
480 let size;
481 let url = parseURL(enclosureElement.getAttribute('href'), this.url);
482 if (url === null) {
483 throw new TypeError('invalid URL in enclosure href attribute');
484 }
485
486 let typeAttribute = enclosureElement.getAttribute('type');
487 if (typeAttribute !== null) {
488 type = typeAttribute;
489 }
490
491 let length = parseInt(enclosureElement.getAttribute('length'), 10);
492 if (!isNaN(length)) {
493 size = length;
494 }
495
496 return new FeedEntryFile(url, {type, size});
497 }
498
478 parseAtomEntry(entryElement) { 499 parseAtomEntry(entryElement) {
479 let title; 500 let title;
480 let link; 501 let link;
481 let date; 502 let date;
482 let content; 503 let content;
504 let files = [];
483 let titleElement = feedQueryXPath(this.document, entryElement, 505 let titleElement = feedQueryXPath(this.document, entryElement,
484 './atom:title'); 506 './atom:title');
485 if (titleElement !== null) { 507 if (titleElement !== null) {
486 title = this.parseAtomTextConstruct(titleElement); 508 title = this.parseAtomTextConstruct(titleElement);
487 } 509 }
509 if (summaryElement !== null) { 531 if (summaryElement !== null) {
510 content = this.parseAtomTextConstruct(summaryElement, false); 532 content = this.parseAtomTextConstruct(summaryElement, false);
511 } 533 }
512 } 534 }
513 535
514 return new FeedEntry({title, link, date, content}); 536 for (let enclosureElement of feedQueryXPathAll(this.document,
537 entryElement, './atom:link[@href][@rel="enclosure"]')) {
538 try {
539 let entryFile = this.parseAtomEntryFile(enclosureElement);
540 files.push(entryFile);
541 } catch (e) {
542 if (!(e instanceof TypeError)) {
543 throw e;
544 }
545 }
546 }
547
548 return new FeedEntry({title, link, date, content, files});
515 } 549 }
516 550
517 parseAtomFeed() { 551 parseAtomFeed() {
518 let title; 552 let title;
519 let subtitle; 553 let subtitle;