0

I'm assuming this is something simple. However, I'm trying to process an RSS news feed and the title and description has some escaped HTML characters, specifically '. I was under the impression that decodeURI(...) would handle this, but I could be using the wrong function. Any help would be appreciated.

getNews(stocks: string, daysBack: number) {

this.newsItems = [];
const securityArray = stocks.split(',');
this.recordsProcessed = 0;
this.recordCount = securityArray.length;
this.securityService.progressStart.next(0);
this.securityService.progressFinish.next(this.recordCount);
this.showRecs = false;

for (let i = 0; i < securityArray.length; i++) {
  const stk = securityArray[i];
  this.securityService.getNews(securityArray[i]).takeUntil(this.ngUnsubscribe).subscribe(n => {
    try {

      for (let x = 0; x < n.rss.channel.item.length; x++) {

        const iDate = new Date(new Date().setDate(new Date().getDate() - daysBack));
        iDate.setHours(0, 0, 0, 0);
        const pDate = new Date(n.rss.channel.item[x].pubDate);

        if (pDate >= iDate) {

          const newsItem = new NewsItem(
            stk,
            decodeURI(n.rss.channel.item[x].description),
            n.rss.channel.item[x].guid,
            n.rss.channel.item[x].link,
            decodeURI(n.rss.channel.item[x].title),
            pDate
          );
          this.newsItems.push(newsItem);
        }
      }

      this.recordsProcessed++;
      this.securityService.progressStart.next(this.recordsProcessed);

      if (this.recordCount === this.recordsProcessed) {
        this.showRecs = true;
      }

    } catch (e) {
      console.log(e);
      this.recordsProcessed++;
      this.securityService.progressStart.next(this.recordsProcessed);
      if (this.recordCount === this.recordsProcessed) {
        this.showRecs = true;
      }
    }

  }, error => {
    console.log('Error', error);
    this.recordsProcessed++;
    this.securityService.progressStart.next(this.recordsProcessed);
  });
}
}
Leo Williams
  • 147
  • 1
  • 11

1 Answers1

1

You are indeed using the wrong function to decode HTML entities -- decodeURI() handles percent-sign escaping, not HTML entities.

I'd use the Lodash _.unescape() function for this.

kshetline
  • 12,547
  • 4
  • 37
  • 73