# HG changeset patch # User Guido Berhoerster # Date 1541322185 -3600 # Node ID 341a0f4b7ce09ed05a94f9c3176f20b56193663c # Parent 086ee559acbb5d11d94227ac103e99310079f49b Handle feed entry content normalization with a setter diff -r 086ee559acbb -r 341a0f4b7ce0 content_scripts/feed-preview.js --- 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(`
${encodedContent}
`); + this.content = `
${encodedContent}
`; } } }