changeset 5:341a0f4b7ce0

Handle feed entry content normalization with a setter
author Guido Berhoerster <guido+feed-preview@berhoerster.name>
date Sun, 04 Nov 2018 10:03:05 +0100
parents 086ee559acbb
children 5d7c13e998e9
files content_scripts/feed-preview.js
diffstat 1 files changed, 24 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/content_scripts/feed-preview.js	Sun Nov 04 09:54:37 2018 +0100
+++ b/content_scripts/feed-preview.js	Sun Nov 04 10:03:05 2018 +0100
@@ -43,17 +43,6 @@
     return url;
 }
 
-function normalizeHTML(text) {
-    let parsedDocument = (new DOMParser()).parseFromString(text, 'text/html');
-
-    let linkElement = parsedDocument.createElement('link');
-    linkElement.rel = 'stylesheet';
-    linkElement.href ='style/entry-content.css';
-    parsedDocument.head.appendChild(linkElement);
-
-    return (new XMLSerializer()).serializeToString(parsedDocument);
-}
-
 function feedNSResolver(prefix) {
     switch (prefix) {
         case 'atom':
@@ -177,9 +166,29 @@
         this.title = title;
         this.url = url;
         this.date = date;
+        this._content;
         this.content = content;
         this.files = files;
     }
+
+    set content(content) {
+        this._content = this.normalizeContent(content);
+    }
+
+    get content() {
+        return this._content;
+    }
+
+    normalizeContent(text) {
+        let parsedDocument = new DOMParser().parseFromString(text, 'text/html');
+
+        let linkElement = parsedDocument.createElement('link');
+        linkElement.rel = 'stylesheet';
+        linkElement.href ='style/entry-content.css';
+        parsedDocument.head.appendChild(linkElement);
+
+        return new XMLSerializer().serializeToString(parsedDocument);
+    }
 }
 
 class RSS1Entry extends FeedEntry {
@@ -223,7 +232,7 @@
         let descriptionElement = feedQueryXPath(feedDocument, itemElement,
                 './description');
         if (descriptionElement !== null) {
-            this.content = normalizeHTML(descriptionElement.textContent.trim());
+            this.content = descriptionElement.textContent.trim();
         }
 
         for (let enclosureElement of
@@ -271,13 +280,13 @@
             }
             contentType = contentType.toLowerCase();
             if (contentType === 'xhtml') {
-                this.content = normalizeHTML(contentElement.innerHTML);
+                this.content = contentElement.innerHTML;
             } else if (contentType === 'html') {
-                this.content = normalizeHTML(contentElement.textContent);
+                this.content = contentElement.textContent;
             } else {
                 let encodedContent =
                         encodeXML(contentElement.textContent.trim());
-                this.content = normalizeHTML(`<pre>${encodedContent}</pre>`);
+                this.content = `<pre>${encodedContent}</pre>`;
             }
         }
     }