mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-11-30 09:18:18 +00:00
Handle relative <link> elements in Atom parser — use xml:base when present to resolve. Add test case.
This commit is contained in:
1 parent
ccb1b8294e
commit
d22f3819cd
3 files changed
+52
-8
No files matched your search
@@ -25,6 +25,7 @@ final class AtomParser {
|
||||
attributesStack.last
|
||||
}
|
||||
|
||||
private var xmlBaseURL: URL?
|
||||
private var parsingXHTML = false
|
||||
private var xhtmlString: String?
|
||||
|
||||
@@ -90,6 +91,7 @@ private extension AtomParser {
|
||||
static let type = "type"
|
||||
static let length = "length"
|
||||
static let xmlLang = "xml:lang"
|
||||
static let xmlBase = "xml:base"
|
||||
}
|
||||
|
||||
func currentString(_ saxParser: SAXParser) -> String? {
|
||||
@@ -139,13 +141,23 @@ private extension AtomParser {
|
||||
}
|
||||
}
|
||||
|
||||
func addFeedLanguage() {
|
||||
func addFeedAttributes() {
|
||||
|
||||
guard feed.language == nil, let currentAttributes else {
|
||||
guard let currentAttributes else {
|
||||
return
|
||||
}
|
||||
|
||||
feed.language = currentAttributes[XMLString.xmlLang]
|
||||
if feed.language == nil {
|
||||
feed.language = currentAttributes[XMLString.xmlLang]
|
||||
}
|
||||
|
||||
if xmlBaseURL == nil {
|
||||
if let xmlBase = currentAttributes[XMLString.xmlBase] {
|
||||
if let baseURL = URL(string: xmlBase) {
|
||||
xmlBaseURL = baseURL
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func addArticle() {
|
||||
@@ -225,7 +237,8 @@ private extension AtomParser {
|
||||
guard let urlString = attributes[XMLString.href], !urlString.isEmpty else {
|
||||
return
|
||||
}
|
||||
|
||||
let resolvedURLString = linkResolvedAgainstXMLBase(urlString)
|
||||
|
||||
var rel = attributes[XMLString.rel]
|
||||
if rel?.isEmpty ?? true {
|
||||
rel = XMLString.alternate
|
||||
@@ -233,21 +246,33 @@ private extension AtomParser {
|
||||
|
||||
if rel == XMLString.related {
|
||||
if article.link == nil {
|
||||
article.link = urlString
|
||||
article.link = resolvedURLString
|
||||
}
|
||||
}
|
||||
else if rel == XMLString.alternate {
|
||||
if article.permalink == nil {
|
||||
article.permalink = urlString
|
||||
article.permalink = resolvedURLString
|
||||
}
|
||||
}
|
||||
else if rel == XMLString.enclosure {
|
||||
if let enclosure = enclosure(urlString, attributes) {
|
||||
if let enclosure = enclosure(resolvedURLString, attributes) {
|
||||
article.addEnclosure(enclosure)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func linkResolvedAgainstXMLBase(_ urlString: String) -> String {
|
||||
|
||||
guard let xmlBaseURL else {
|
||||
return urlString
|
||||
}
|
||||
|
||||
if let resolvedURL = URL(string: urlString, relativeTo: xmlBaseURL) {
|
||||
return resolvedURL.absoluteString
|
||||
}
|
||||
return urlString
|
||||
}
|
||||
|
||||
func enclosure(_ urlString: String, _ attributes: StringDictionary) -> RSSEnclosure? {
|
||||
|
||||
let enclosure = RSSEnclosure(url: urlString)
|
||||
@@ -351,7 +376,7 @@ extension AtomParser: SAXParserDelegate {
|
||||
}
|
||||
|
||||
if SAXEqualTags(localName, XMLName.feed) {
|
||||
addFeedLanguage()
|
||||
addFeedAttributes()
|
||||
}
|
||||
|
||||
saxParser.beginStoringCharacters()
|
||||
|
||||
@@ -109,4 +109,22 @@ final class AtomParserTests: XCTestCase {
|
||||
XCTAssertNotEqual(article.title, "Default Title")
|
||||
}
|
||||
}
|
||||
|
||||
func testLinkElementsWithRelativeURLs() {
|
||||
|
||||
// This feed has <link> elements that look like this…
|
||||
// <link href="/en/publish/2022/07/01/great-moments-in-document-history-reimagining-the-declaration-of-independence-as-pdf"/>
|
||||
// …and it also has, in the feed declaration…
|
||||
// xml:base="https://blog.adobe.com"
|
||||
// …and so the <link> values should be parsed as (for example):
|
||||
// https://blog.adobe.com/en/publish/2022/07/01/great-moments-in-document-history-reimagining-the-declaration-of-independence-as-pdf
|
||||
// Issue: https://github.com/Ranchero-Software/NetNewsWire/issues/3662
|
||||
|
||||
let d = parserData("adobe", "atom", "https://blog.adobe.com/feed.xml")
|
||||
let parsedFeed = try! FeedParser.parse(d)!
|
||||
|
||||
for article in parsedFeed.items {
|
||||
XCTAssertTrue(article.url!.hasPrefix("https://blog.adobe.com/en/publish/20"))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<feed xmlns="http://www.w3.org/2005/Atom" xml:base="https://blog.adobe.com"><title>Adobe Blog</title><updated>2022-07-03T19:54:31.492Z</updated><author><name>Adobe</name></author><id>https://blog.adobe.com/</id><entry><title>Great moments in document history: Reimagining the Declaration of Independence as a PDF</title><link href="/en/publish/2022/07/01/great-moments-in-document-history-reimagining-the-declaration-of-independence-as-pdf"/><author><name>Adobe Acrobat Team</name></author><id>https://blog.adobe.com/en/publish/2022/07/01/great-moments-in-document-history-reimagining-the-declaration-of-independence-as-pdf</id><updated>2022-07-01T00:00:00.000Z</updated><summary>By the time the Declaration of Independence was ratified, nearly 100 manual alterations had been made to the handwritten document. If only they had PDF.</summary></entry><entry><title>Sakshi Sachdev on being fueled by challenges in the technical realm</title><link href="/en/publish/2022/06/30/sakshi-sachdev-fuelled-by-challenges-in-tech"/><author><name>Adobe Life Team</name></author><id>https://blog.adobe.com/en/publish/2022/06/30/sakshi-sachdev-fuelled-by-challenges-in-tech</id><updated>2022-06-30T00:00:00.000Z</updated><summary>From individual contributor to senior director of engineering, Sakshi Sachdev’s journey has been one of creating, ideating, and solving. Find out what motivates her to work alongside her Adobe team.</summary></entry><entry><title>Using certified digital signatures on documents to increase trust in online information</title><link href="/en/publish/2022/06/30/using-certified-digital-signatures-on-documents-to-increase-trust-in-online-information"/><author><name>Steven Gottwals</name></author><id>https://blog.adobe.com/en/publish/2022/06/30/using-certified-digital-signatures-on-documents-to-increase-trust-in-online-information</id><updated>2022-06-30T00:00:00.000Z</updated><summary>In a world where people increasingly question the validity of the information they consume online, readers need to know that the documents they download come from a trusted source.</summary></entry><entry><title>Fresh new foundries and fonts from the Americas, and Hidden Treasures revisited</title><link href="/en/publish/2022/06/30/fresh-new-foundries-fonts-from-americas-hidden-treasures-revisited"/><author><name>Yves Peters</name></author><id>https://blog.adobe.com/en/publish/2022/06/30/fresh-new-foundries-fonts-from-americas-hidden-treasures-revisited</id><updated>2022-06-30T00:00:00.000Z</updated><summary>Three new foundries add fonts to Adobe Fonts library included with Creative Cloud subscription</summary></entry><entry><title>Advocating for the planet and representation of the LGBTQIA+ community with Adobe Lightroom Ambassador Julia Nimke</title><link href="/en/publish/2022/06/29/advocating-for-planet-representation-lgbtqia-community-with-adobe-lightroom-ambassador-julia-nimke"/><author><name>Rani Mani</name></author><id>https://blog.adobe.com/en/publish/2022/06/29/advocating-for-planet-representation-lgbtqia-community-with-adobe-lightroom-ambassador-julia-nimke</id><updated>2022-06-29T00:00:00.000Z</updated><summary>Adobe Lightroom Ambassador and commercial photographer Julia Nimke advocates for the planet and representation of the LGBTQIA+ community</summary></entry><entry><title>Do customers care about your brand’s integrity?</title><link href="/en/publish/2022/06/28/do-customers-care-about-your-brands-integrity"/><author><name>Trustpilot</name></author><id>https://blog.adobe.com/en/publish/2022/06/28/do-customers-care-about-your-brands-integrity</id><updated>2022-06-28T00:00:00.000Z</updated><summary>Many brands are getting vocal about ethical purpose and honesty. But does brand integrity really influence the behavior of consumers?</summary></entry><entry><title>How to get started in user experience design</title><link href="/en/publish/2022/06/27/how-to-get-started-in-ux-design"/><author><name>Torin Edwards</name></author><id>https://blog.adobe.com/en/publish/2022/06/27/how-to-get-started-in-ux-design</id><updated>2022-06-27T00:00:00.000Z</updated><summary>User experience design is essential – and everywhere. Senior experience designer Torin Edwards breaks down the fundamentals of UX, important resources, and how to get started and build your portfolio - the right way.</summary></entry><entry><title>How to capture aerial real estate photography</title><link href="/en/publish/2022/06/27/aerial-photography-real-estate-photographer-guide"/><author><name>Marc Weisberg</name></author><id>https://blog.adobe.com/en/publish/2022/06/27/aerial-photography-real-estate-photographer-guide</id><updated>2022-06-27T00:00:00.000Z</updated><summary>Whether you’re shooting large commercial complexes, or residential properties, mastering aerial photography is paramount for capturing the perfect shot.</summary></entry><entry><title>Behold the bold — announcing the 2022 Adobe Experience Maker Awards winners</title><link href="/en/publish/20Line truncated
|
||||
Reference in new issue
Block a user