From ca081c41a95296213d6719662980c6db7fa8132d Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sat, 18 Nov 2017 12:41:15 -0800 Subject: [PATCH] Decode HTML entities in the JSON Feed parser for content_html. Fix #176. --- .../RSParser/Feeds/JSON/JSONFeedParser.swift | 3 ++- .../RSParserTests/JSONFeedParserTests.swift | 21 +++++++++++++++++++ .../RSParser/Utilities/NSString+RSParser.h | 3 +++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Frameworks/RSParser/Feeds/JSON/JSONFeedParser.swift b/Frameworks/RSParser/Feeds/JSON/JSONFeedParser.swift index 4ec70f628..c97744244 100644 --- a/Frameworks/RSParser/Feeds/JSON/JSONFeedParser.swift +++ b/Frameworks/RSParser/Feeds/JSON/JSONFeedParser.swift @@ -99,6 +99,7 @@ private extension JSONFeedParser { if contentHTML == nil && contentText == nil { return nil } + let decodedContentHTML = contentHTML?.rsparser_stringByDecodingHTMLEntities() let url = itemDictionary["url"] as? String let externalURL = itemDictionary["external_url"] as? String @@ -117,7 +118,7 @@ private extension JSONFeedParser { } let attachments = parseAttachments(itemDictionary) - return ParsedItem(syncServiceID: nil, uniqueID: uniqueID, feedURL: feedURL, url: url, externalURL: externalURL, title: title, contentHTML: contentHTML, contentText: contentText, summary: summary, imageURL: imageURL, bannerImageURL: bannerImageURL, datePublished: datePublished, dateModified: dateModified, authors: authors, tags: tags, attachments: attachments) + return ParsedItem(syncServiceID: nil, uniqueID: uniqueID, feedURL: feedURL, url: url, externalURL: externalURL, title: title, contentHTML: decodedContentHTML, contentText: contentText, summary: summary, imageURL: imageURL, bannerImageURL: bannerImageURL, datePublished: datePublished, dateModified: dateModified, authors: authors, tags: tags, attachments: attachments) } static func parseUniqueID(_ itemDictionary: JSONDictionary) -> String? { diff --git a/Frameworks/RSParser/RSParserTests/JSONFeedParserTests.swift b/Frameworks/RSParser/RSParserTests/JSONFeedParserTests.swift index f43e0fb8e..b7e1a34c4 100644 --- a/Frameworks/RSParser/RSParserTests/JSONFeedParserTests.swift +++ b/Frameworks/RSParser/RSParserTests/JSONFeedParserTests.swift @@ -29,4 +29,25 @@ class JSONFeedParserTests: XCTestCase { } } + func testThatEntitiesAreDecoded() { + + let d = parserData("DaringFireball", "json", "http://daringfireball.net/") + let parsedFeed = try! FeedParser.parse(d) + + // https://github.com/brentsimmons/Evergreen/issues/176 + // In the article titled "The Talk Show: ‘I Do Like Throwing a Baby’", + // make sure the content HTML starts with "\n

New episode of America’s" + // instead of "\n

New episode of America’s" — this will tell us + // that entities are being decoded. + + for article in parsedFeed!.items { + if article.title == "The Talk Show: ‘I Do Like Throwing a Baby’" { + XCTAssert(article.contentHTML!.hasPrefix("\n

New episode of America’s")) + return + } + } + + XCTAssert(false, "Expected to find “The Talk Show: ‘I Do Like Throwing a Baby’” article.") + } + } diff --git a/Frameworks/RSParser/Utilities/NSString+RSParser.h b/Frameworks/RSParser/Utilities/NSString+RSParser.h index 86a99be8b..6b9b65d9c 100755 --- a/Frameworks/RSParser/Utilities/NSString+RSParser.h +++ b/Frameworks/RSParser/Utilities/NSString+RSParser.h @@ -8,6 +8,8 @@ @import Foundation; +NS_ASSUME_NONNULL_BEGIN + @interface NSString (RSParser) - (NSString *)rsparser_stringByDecodingHTMLEntities; @@ -16,3 +18,4 @@ @end +NS_ASSUME_NONNULL_END