rowanmanning / rowanmanning/feed-parser

Feed item content gets unescaped

Open
#209 6 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
49
Forks
8
Avg merge
2m
Merged PRs (30d)
2

Description

I'm trying to parse a feed and render its contents on a website. The feed sometimes contains HTML code blocks (think tutorial posts explaining how to do something in HTML, like this).

Take this example feed for instance:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<item>
			<content:encoded><![CDATA[
				<pre><code>&lt;div class="wrapper">Lorem ipsum dolor sit amet&lt;/div></code></pre>
			]]></content:encoded>
		</item>
	</channel>
</rss>

Intuitively, I expected that parseFeed(xml).items[0].content would return something like:

<pre><code>&lt;div class="wrapper">Lorem ipsum dolor sit amet&lt;/div></code></pre>

Instead, the text for content gets unescaped (RSS, Atom), and this is returned instead:

<pre><code><div class="wrapper">Lorem ipsum dolor sit amet</div></code></pre>

While I do want the outer <pre> and <code> tags to be rendered as proper HTML tags on the final page, the inner div I want to keep verbatim, i.e. &lt;div class="wrapper">, so that it is rendered as text on the final website.

I made the changes to suit my needs in this commit, including some tests. I was unable to get most of the integration tests to actually pass, since the feedparser library (used to process feeds in tests) seems to unescape HTML in the same way, with no option to turn it off.

The way I did it would also be a breaking change; to avoid, assuming you even want to support this use case, perhaps we could add an options parameter to the parseFeed function to opt out of unescaping?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing the RSS and Atom content handling in lib/feed/item/rss.js and lib/feed/item/atom.js, then review the related cases in test/integration/sample-feeds.test.js. Examine commit 7b92b21218e03b99d6c56b43d2dcaf36aaf52d9c and determine whether the parser should preserve escaped inner markup by default or expose an option for opting out of unescaping. Update the relevant tests to cover the chosen behavior, accounting for the feedparser test dependency.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.