angular 4 decodeURI not unescaping'

问题描述 投票:0回答:1

我假设这很简单。但是,我正在尝试处理RSS新闻源,标题和说明有一些转义的HTML字符,特别是'。我的印象是decodeURI(...)会处理这个问题,但我可能会使用错误的函数。任何帮助,将不胜感激。

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);
  });
}
}
angular typescript unescapestring
1个回答
1
投票

您确实使用了错误的函数来解码HTML实体 - decodeURI()处理百分号符号转义,而不是HTML实体。

我会使用Lodash _.unescape()功能。

© www.soinside.com 2019 - 2024. All rights reserved.