addons/firefox-addons/feed-preview

changeset 5:341a0f4b7ce0

Handle feed entry content normalization with a setter
author Guido Berhoerster <guido+feed-preview@berhoerster.name>
date Sun Nov 04 10:03:05 2018 +0100 (19 months ago)
parents 086ee559acbb
children 5d7c13e998e9
files content_scripts/feed-preview.js
line diff
     1.1 --- a/content_scripts/feed-preview.js	Sun Nov 04 09:54:37 2018 +0100
     1.2 +++ b/content_scripts/feed-preview.js	Sun Nov 04 10:03:05 2018 +0100
     1.3 @@ -43,17 +43,6 @@
     1.4      return url;
     1.5  }
     1.6  
     1.7 -function normalizeHTML(text) {
     1.8 -    let parsedDocument = (new DOMParser()).parseFromString(text, 'text/html');
     1.9 -
    1.10 -    let linkElement = parsedDocument.createElement('link');
    1.11 -    linkElement.rel = 'stylesheet';
    1.12 -    linkElement.href ='style/entry-content.css';
    1.13 -    parsedDocument.head.appendChild(linkElement);
    1.14 -
    1.15 -    return (new XMLSerializer()).serializeToString(parsedDocument);
    1.16 -}
    1.17 -
    1.18  function feedNSResolver(prefix) {
    1.19      switch (prefix) {
    1.20          case 'atom':
    1.21 @@ -177,9 +166,29 @@
    1.22          this.title = title;
    1.23          this.url = url;
    1.24          this.date = date;
    1.25 +        this._content;
    1.26          this.content = content;
    1.27          this.files = files;
    1.28      }
    1.29 +
    1.30 +    set content(content) {
    1.31 +        this._content = this.normalizeContent(content);
    1.32 +    }
    1.33 +
    1.34 +    get content() {
    1.35 +        return this._content;
    1.36 +    }
    1.37 +
    1.38 +    normalizeContent(text) {
    1.39 +        let parsedDocument = new DOMParser().parseFromString(text, 'text/html');
    1.40 +
    1.41 +        let linkElement = parsedDocument.createElement('link');
    1.42 +        linkElement.rel = 'stylesheet';
    1.43 +        linkElement.href ='style/entry-content.css';
    1.44 +        parsedDocument.head.appendChild(linkElement);
    1.45 +
    1.46 +        return new XMLSerializer().serializeToString(parsedDocument);
    1.47 +    }
    1.48  }
    1.49  
    1.50  class RSS1Entry extends FeedEntry {
    1.51 @@ -223,7 +232,7 @@
    1.52          let descriptionElement = feedQueryXPath(feedDocument, itemElement,
    1.53                  './description');
    1.54          if (descriptionElement !== null) {
    1.55 -            this.content = normalizeHTML(descriptionElement.textContent.trim());
    1.56 +            this.content = descriptionElement.textContent.trim();
    1.57          }
    1.58  
    1.59          for (let enclosureElement of
    1.60 @@ -271,13 +280,13 @@
    1.61              }
    1.62              contentType = contentType.toLowerCase();
    1.63              if (contentType === 'xhtml') {
    1.64 -                this.content = normalizeHTML(contentElement.innerHTML);
    1.65 +                this.content = contentElement.innerHTML;
    1.66              } else if (contentType === 'html') {
    1.67 -                this.content = normalizeHTML(contentElement.textContent);
    1.68 +                this.content = contentElement.textContent;
    1.69              } else {
    1.70                  let encodedContent =
    1.71                          encodeXML(contentElement.textContent.trim());
    1.72 -                this.content = normalizeHTML(`<pre>${encodedContent}</pre>`);
    1.73 +                this.content = `<pre>${encodedContent}</pre>`;
    1.74              }
    1.75          }
    1.76      }