From 89f009ced042640d35d780e4598df626678f9e65 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Mon, 20 Apr 2026 10:58:15 -0700 Subject: [PATCH] Use new HTMLMetadata and HTMLLink parsers. --- .../Sources/FeedFinder/HTMLFeedFinder.swift | 6 +- .../HTMLLinkPerformanceTests.swift | 2 +- .../Tests/RSParserTests/HTMLLinkTests.swift | 118 +++++++++++++++++- .../HTMLMetadataPerformanceTests.swift | 10 +- .../RSParserTests/HTMLMetadataTests.swift | 99 +++++++++++++-- .../Sources/RSWeb/HTMLMetadataCache.swift | 4 +- .../RSWeb/HTMLMetadataDownloader.swift | 4 +- Shared/Favicons/FaviconDownloader.swift | 4 +- ...ion.swift => HTMLMetadata+Extension.swift} | 8 +- 9 files changed, 228 insertions(+), 27 deletions(-) rename Shared/Images/{RSHTMLMetadata+Extension.swift => HTMLMetadata+Extension.swift} (91%) diff --git a/Modules/FeedFinder/Sources/FeedFinder/HTMLFeedFinder.swift b/Modules/FeedFinder/Sources/FeedFinder/HTMLFeedFinder.swift index 980a23d7b..4d3363060 100644 --- a/Modules/FeedFinder/Sources/FeedFinder/HTMLFeedFinder.swift +++ b/Modules/FeedFinder/Sources/FeedFinder/HTMLFeedFinder.swift @@ -20,7 +20,7 @@ final class HTMLFeedFinder { private var feedSpecifiersDictionary = [String: FeedSpecifier]() init(parserData: ParserData) { - let metadata = RSHTMLMetadataParser.htmlMetadata(with: parserData) + let metadata = HTMLMetadataParser.htmlMetadata(with: parserData) var orderFound = 0 for oneFeedLink in metadata.feedLinks { @@ -31,7 +31,7 @@ final class HTMLFeedFinder { } } - let bodyLinks = RSHTMLLinkParser.htmlLinks(with: parserData) + let bodyLinks = HTMLLinkParser.htmlLinks(with: parserData) for oneBodyLink in bodyLinks { if linkMightBeFeed(oneBodyLink), let normalizedURL = oneBodyLink.urlString?.normalizedURL { orderFound += 1 @@ -68,7 +68,7 @@ private extension HTMLFeedFinder { return false } - func linkMightBeFeed(_ link: RSHTMLLink) -> Bool { + func linkMightBeFeed(_ link: HTMLLink) -> Bool { if let linkURLString = link.urlString, urlStringMightBeFeed(linkURLString) { return true } diff --git a/Modules/RSParser/Tests/RSParserTests/HTMLLinkPerformanceTests.swift b/Modules/RSParser/Tests/RSParserTests/HTMLLinkPerformanceTests.swift index e9324c4fd..be6776295 100644 --- a/Modules/RSParser/Tests/RSParserTests/HTMLLinkPerformanceTests.swift +++ b/Modules/RSParser/Tests/RSParserTests/HTMLLinkPerformanceTests.swift @@ -17,7 +17,7 @@ final class HTMLLinkPerformanceTests: XCTestCase { // 0.003 sec on my 2012 iMac let d = parserData("sixcolors", "html", "http://sixcolors.com/") self.measure { - _ = RSHTMLLinkParser.htmlLinks(with: d) + _ = HTMLLinkParser.htmlLinks(with: d) } } } diff --git a/Modules/RSParser/Tests/RSParserTests/HTMLLinkTests.swift b/Modules/RSParser/Tests/RSParserTests/HTMLLinkTests.swift index cd56a7113..493209074 100644 --- a/Modules/RSParser/Tests/RSParserTests/HTMLLinkTests.swift +++ b/Modules/RSParser/Tests/RSParserTests/HTMLLinkTests.swift @@ -15,7 +15,7 @@ import RSParserObjC @Test func sixColorsLink() { let d = parserData("sixcolors", "html", "http://sixcolors.com/") - let links = RSHTMLLinkParser.htmlLinks(with: d) + let links = HTMLLinkParser.htmlLinks(with: d) let linkToFind = "https://www.theincomparable.com/theincomparable/290/index.php" let textToFind = "this week’s episode of The Incomparable" @@ -30,4 +30,120 @@ import RSParserObjC #expect(found) #expect(links.count == 131) } + + // MARK: - Inline fixture tests + + @Test func linksInsideScriptAreIgnored() { + // The scanner enters raw-text mode for + real 2 + + """ + let d = ParserData(url: "http://example.com/", data: Data(html.utf8)) + let links = HTMLLinkParser.htmlLinks(with: d) + let urls = links.compactMap { $0.urlString } + #expect(urls.contains("http://real.example.com/")) + #expect(urls.contains("http://real2.example.com/")) + #expect(!urls.contains("http://not-a-link.example.com/")) + #expect(!urls.contains("http://also-not.example.com/")) + } + + @Test func linksInsideStyleAreIgnored() { + // + real + + """ + let d = ParserData(url: "http://example.com/", data: Data(html.utf8)) + let links = HTMLLinkParser.htmlLinks(with: d) + let urls = links.compactMap { $0.urlString } + #expect(urls == ["http://real.example.com/"]) + } + + @Test func relativeURLsResolvedAgainstPageURL() { + // The parser's second argument is the page URL; relative hrefs must + // resolve against it so the caller gets absolute URLs back. + let html = """ + + absolute path + relative path + already absolute + + """ + let d = ParserData(url: "http://example.com/page/", data: Data(html.utf8)) + let links = HTMLLinkParser.htmlLinks(with: d) + let urls = links.compactMap { $0.urlString } + #expect(urls.contains("http://example.com/foo")) + #expect(urls.contains("http://example.com/page/bar.html")) + #expect(urls.contains("http://other.example.com/x")) + } + + @Test func unclosedTagsDoNotCrashAndStillFindLinks() { + // Malformed markup: the scanner must not hang or lose later links. + let html = """ + +

Orphan bold italic + first +

Unclosed div + second + + """ + let d = ParserData(url: "http://example.com/", data: Data(html.utf8)) + let links = HTMLLinkParser.htmlLinks(with: d) + let urls = links.compactMap { $0.urlString } + #expect(urls.contains("http://example.com/first")) + #expect(urls.contains("http://example.com/second")) + } + + @Test func anchorWithoutHrefIgnored() { + // `` without href is an anchor target, not a link. Must not appear. + let html = """ + + + real + no href at all + + """ + let d = ParserData(url: "http://example.com/", data: Data(html.utf8)) + let links = HTMLLinkParser.htmlLinks(with: d) + let urls = links.compactMap { $0.urlString } + #expect(urls == ["http://example.com/"]) + } + + @Test func entitiesInHrefDecoded() { + // href attribute values routinely contain `&` as a query-parameter + // separator. After extraction the caller expects the decoded `&` (so + // the URL they get is the real URL, not the HTML-escaped spelling). + // A regression that stopped decoding entities in attribute values + // would silently break every URL with a `?a=1&b=2`-style query. + let html = """ + + two-param + three-param + + """ + let d = ParserData(url: "http://example.com/", data: Data(html.utf8)) + let links = HTMLLinkParser.htmlLinks(with: d) + let urls = links.compactMap { $0.urlString } + #expect(urls.contains("http://example.com/search?q=foo&page=2")) + #expect(urls.contains("http://example.com/a?x=1&y=2&z=3")) + // No `&` should leak through. + for u in urls { + #expect(!u.contains("&"), "entity not decoded in href: \(u)") + } + } } diff --git a/Modules/RSParser/Tests/RSParserTests/HTMLMetadataPerformanceTests.swift b/Modules/RSParser/Tests/RSParserTests/HTMLMetadataPerformanceTests.swift index 6167bb762..079811bf9 100644 --- a/Modules/RSParser/Tests/RSParserTests/HTMLMetadataPerformanceTests.swift +++ b/Modules/RSParser/Tests/RSParserTests/HTMLMetadataPerformanceTests.swift @@ -17,7 +17,7 @@ final class HTMLMetadataPerformanceTests: XCTestCase { // 0.002 sec on my 2012 iMac let d = parserData("DaringFireball", "html", "http://daringfireball.net/") self.measure { - _ = RSHTMLMetadataParser.htmlMetadata(with: d) + _ = HTMLMetadataParser.htmlMetadata(with: d) } } @@ -25,7 +25,7 @@ final class HTMLMetadataPerformanceTests: XCTestCase { // 0.001 sec on my 2012 iMac let d = parserData("furbo", "html", "http://furbo.org/") self.measure { - _ = RSHTMLMetadataParser.htmlMetadata(with: d) + _ = HTMLMetadataParser.htmlMetadata(with: d) } } @@ -33,7 +33,7 @@ final class HTMLMetadataPerformanceTests: XCTestCase { // 0.001 sec on my 2012 iMac let d = parserData("inessential", "html", "http://inessential.com/") self.measure { - _ = RSHTMLMetadataParser.htmlMetadata(with: d) + _ = HTMLMetadataParser.htmlMetadata(with: d) } } @@ -41,7 +41,7 @@ final class HTMLMetadataPerformanceTests: XCTestCase { // 0.004 sec on my 2012 iMac let d = parserData("coco", "html", "https://www.theatlantic.com/entertainment/archive/2017/11/coco-is-among-pixars-best-movies-in-years/546695/") self.measure { - _ = RSHTMLMetadataParser.htmlMetadata(with: d) + _ = HTMLMetadataParser.htmlMetadata(with: d) } } @@ -49,7 +49,7 @@ final class HTMLMetadataPerformanceTests: XCTestCase { // 0.002 sec on my 2012 iMac let d = parserData("sixcolors", "html", "http://sixcolors.com/") self.measure { - _ = RSHTMLMetadataParser.htmlMetadata(with: d) + _ = HTMLMetadataParser.htmlMetadata(with: d) } } } diff --git a/Modules/RSParser/Tests/RSParserTests/HTMLMetadataTests.swift b/Modules/RSParser/Tests/RSParserTests/HTMLMetadataTests.swift index 1533e77d2..3d5991229 100644 --- a/Modules/RSParser/Tests/RSParserTests/HTMLMetadataTests.swift +++ b/Modules/RSParser/Tests/RSParserTests/HTMLMetadataTests.swift @@ -15,7 +15,7 @@ import RSParserObjC @Test func daringFireball() throws { let d = parserData("DaringFireball", "html", "http://daringfireball.net/") - let metadata = RSHTMLMetadataParser.htmlMetadata(with: d) + let metadata = HTMLMetadataParser.htmlMetadata(with: d) #expect(metadata.favicons.first?.urlString == "http://daringfireball.net/graphics/favicon.ico?v=005") @@ -29,7 +29,7 @@ import RSParserObjC @Test func furbo() throws { let d = parserData("furbo", "html", "http://furbo.org/") - let metadata = RSHTMLMetadataParser.htmlMetadata(with: d) + let metadata = HTMLMetadataParser.htmlMetadata(with: d) #expect(metadata.favicons.first?.urlString == "http://furbo.org/favicon.ico") @@ -42,7 +42,7 @@ import RSParserObjC @Test func inessential() throws { let d = parserData("inessential", "html", "http://inessential.com/") - let metadata = RSHTMLMetadataParser.htmlMetadata(with: d) + let metadata = HTMLMetadataParser.htmlMetadata(with: d) #expect(metadata.favicons.first?.urlString == nil) @@ -57,7 +57,7 @@ import RSParserObjC @Test func sixColors() throws { let d = parserData("sixcolors", "html", "http://sixcolors.com/") - let metadata = RSHTMLMetadataParser.htmlMetadata(with: d) + let metadata = HTMLMetadataParser.htmlMetadata(with: d) #expect(metadata.favicons.first?.urlString == "https://sixcolors.com/images/favicon.ico") @@ -76,7 +76,7 @@ import RSParserObjC @Test func cocoOGImage() throws { let d = parserData("coco", "html", "https://www.theatlantic.com/entertainment/archive/2017/11/coco-is-among-pixars-best-movies-in-years/546695/") - let metadata = RSHTMLMetadataParser.htmlMetadata(with: d) + let metadata = HTMLMetadataParser.htmlMetadata(with: d) let openGraphData = metadata.openGraphProperties let image = try #require(openGraphData.images.first) #expect(image.url == "https://cdn.theatlantic.com/assets/media/img/mt/2017/11/1033101_first_full_length_trailer_arrives_pixars_coco/facebook.jpg?1511382177") @@ -84,7 +84,7 @@ import RSParserObjC @Test func cocoTwitterImage() throws { let d = parserData("coco", "html", "https://www.theatlantic.com/entertainment/archive/2017/11/coco-is-among-pixars-best-movies-in-years/546695/") - let metadata = RSHTMLMetadataParser.htmlMetadata(with: d) + let metadata = HTMLMetadataParser.htmlMetadata(with: d) let twitterData = metadata.twitterProperties let imageURL = try #require(twitterData.imageURL) #expect(imageURL == "https://cdn.theatlantic.com/assets/media/img/mt/2017/11/1033101_first_full_length_trailer_arrives_pixars_coco/facebook.jpg?1511382177") @@ -93,7 +93,7 @@ import RSParserObjC @Test func youTube() throws { // YouTube is a special case — the feed links appear after the head section, in the body section. let d = parserData("YouTubeTheVolvoRocks", "html", "https://www.youtube.com/user/TheVolvorocks") - let metadata = RSHTMLMetadataParser.htmlMetadata(with: d) + let metadata = HTMLMetadataParser.htmlMetadata(with: d) #expect(metadata.feedLinks.count == 1) let feedLink = try #require(metadata.feedLinks.first) @@ -101,4 +101,89 @@ import RSParserObjC #expect(feedLink.type == "application/rss+xml") #expect(feedLink.urlString == "https://www.youtube.com/feeds/videos.xml?channel_id=UCct7QF2jcWRY6dhXWMSq9LQ") } + + // MARK: - Inline fixture tests + + @Test func bothRSSAndAtomFeedLinksReported() throws { + // When a page offers both RSS and Atom, both should appear in feedLinks. + // Feed-discovery UI can then pick whichever it prefers. + let html = """ + + + + + """ + let d = ParserData(url: "http://example.com/", data: Data(html.utf8)) + let metadata = HTMLMetadataParser.htmlMetadata(with: d) + #expect(metadata.feedLinks.count == 2) + let types = Set(metadata.feedLinks.compactMap { $0.type }) + #expect(types == ["application/rss+xml", "application/atom+xml"]) + } + + @Test func pageWithNoHeadReturnsEmptyMetadata() { + // Malformed page, no , no / . Parser must not crash + // and must report an empty-but-usable metadata struct. + let html = "Hello world" + let d = ParserData(url: "http://example.com/", data: Data(html.utf8)) + let metadata = HTMLMetadataParser.htmlMetadata(with: d) + #expect(metadata.feedLinks.isEmpty) + #expect(metadata.favicons.isEmpty) + #expect(metadata.appleTouchIcons.isEmpty) + } + + @Test func relativeFaviconResolvedAgainstPageURL() throws { + // Relative favicon href must resolve against the page URL so downloaders + // don't have to. Also checks that multiple apple-touch-icons are preserved. + let html = """ + + + + + + """ + let d = ParserData(url: "http://example.com/some/page.html", data: Data(html.utf8)) + let metadata = HTMLMetadataParser.htmlMetadata(with: d) + #expect(metadata.favicons.first?.urlString == "http://example.com/favicon.ico") + #expect(metadata.appleTouchIcons.count == 2) + let sizes = Set(metadata.appleTouchIcons.compactMap { $0.sizes }) + #expect(sizes == ["60x60", "120x120"]) + let urls = Set(metadata.appleTouchIcons.compactMap { $0.urlString }) + #expect(urls == [ + "http://example.com/touch-60.png", + "http://example.com/touch-120.png" + ]) + } + + @Test func typelessFeedLinkAccepted() throws { + // Some pages advertise their feed with `` + // and no `type` attribute. We accept those as feed links so auto- + // discovery still works. An alternate with an explicit non-feed type + // (`text/html` for i18n variants etc.) is still filtered out — see + // `nonFeedTypedAlternateRejected`. + let html = """ + + + + """ + let d = ParserData(url: "http://example.com/", data: Data(html.utf8)) + let metadata = HTMLMetadataParser.htmlMetadata(with: d) + #expect(metadata.feedLinks.count == 1) + let link = try #require(metadata.feedLinks.first) + #expect(link.urlString == "http://example.com/feed.xml") + #expect(link.type == nil) + } + + @Test func nonFeedTypedAlternateRejected() { + // `rel="alternate"` with an explicit non-feed type must NOT be treated + // as a feed link. This is the other half of the typeless-accepted rule: + // we trust an explicit type when it says "not a feed." + let html = """ + + + + """ + let d = ParserData(url: "http://example.com/", data: Data(html.utf8)) + let metadata = HTMLMetadataParser.htmlMetadata(with: d) + #expect(metadata.feedLinks.isEmpty) + } } diff --git a/Modules/RSWeb/Sources/RSWeb/HTMLMetadataCache.swift b/Modules/RSWeb/Sources/RSWeb/HTMLMetadataCache.swift index 522f73323..43558fdc7 100644 --- a/Modules/RSWeb/Sources/RSWeb/HTMLMetadataCache.swift +++ b/Modules/RSWeb/Sources/RSWeb/HTMLMetadataCache.swift @@ -25,7 +25,7 @@ nonisolated public final class HTMLMetadataCache: Sendable { } private struct HTMLMetadataCacheRecord: CacheRecord { - let metadata: RSHTMLMetadata + let metadata: HTMLMetadata let dateCreated = Date() } @@ -35,7 +35,7 @@ nonisolated public final class HTMLMetadataCache: Sendable { cache.removeAll() } - subscript(_ url: String) -> RSHTMLMetadata? { + subscript(_ url: String) -> HTMLMetadata? { get { return cache[url]?.metadata } diff --git a/Modules/RSWeb/Sources/RSWeb/HTMLMetadataDownloader.swift b/Modules/RSWeb/Sources/RSWeb/HTMLMetadataDownloader.swift index 50838f620..9883fd2e3 100644 --- a/Modules/RSWeb/Sources/RSWeb/HTMLMetadataDownloader.swift +++ b/Modules/RSWeb/Sources/RSWeb/HTMLMetadataDownloader.swift @@ -36,7 +36,7 @@ nonisolated public final class HTMLMetadataDownloader: Sendable { cache.removeAll() } - public func cachedMetadata(for url: String) -> RSHTMLMetadata? { + public func cachedMetadata(for url: String) -> HTMLMetadata? { if Self.shouldSkipDownloadingMetadata(url) { Self.logger.debug("HTMLMetadataDownloader: Skipping requested cached metadata for \(url)") return nil @@ -105,7 +105,7 @@ nonisolated private extension HTMLMetadataDownloader { if let data, !data.isEmpty, let response, response.statusIsOK { let urlToUse = response.url ?? actualURL let parserData = ParserData(url: urlToUse.absoluteString, data: data) - let htmlMetadata = RSHTMLMetadataParser.htmlMetadata(with: parserData) + let htmlMetadata = HTMLMetadataParser.htmlMetadata(with: parserData) Self.logger.debug("HTMLMetadataDownloader caching parsed metadata for \(url)") cache[url] = htmlMetadata return diff --git a/Shared/Favicons/FaviconDownloader.swift b/Shared/Favicons/FaviconDownloader.swift index 5f92eb1e1..eaeae7e9d 100644 --- a/Shared/Favicons/FaviconDownloader.swift +++ b/Shared/Favicons/FaviconDownloader.swift @@ -335,7 +335,7 @@ private extension FaviconDownloader { } } -private extension RSHTMLMetadata { +private extension HTMLMetadata { func usableFaviconURLs() -> [String]? { @@ -346,7 +346,7 @@ private extension RSHTMLMetadata { static let ignoredTypes = [UTType.svg] - private func shouldAllowFavicon(_ favicon: RSHTMLMetadataFavicon) -> Bool { + private func shouldAllowFavicon(_ favicon: HTMLMetadataFavicon) -> Bool { // Check mime type. if let mimeType = favicon.type, let utType = UTType(mimeType: mimeType) { diff --git a/Shared/Images/RSHTMLMetadata+Extension.swift b/Shared/Images/HTMLMetadata+Extension.swift similarity index 91% rename from Shared/Images/RSHTMLMetadata+Extension.swift rename to Shared/Images/HTMLMetadata+Extension.swift index 9a40cfe0f..ec809b757 100644 --- a/Shared/Images/RSHTMLMetadata+Extension.swift +++ b/Shared/Images/HTMLMetadata+Extension.swift @@ -1,5 +1,5 @@ // -// RSHTMLMetadata+Extension.swift +// HTMLMetadata+Extension.swift // NetNewsWire // // Created by Brent Simmons on 11/26/17. @@ -9,7 +9,7 @@ import Foundation import RSParser -extension RSHTMLMetadata { +extension HTMLMetadata { func largestOpenGraphImageURL() -> String? { let openGraphImages = openGraphProperties.images @@ -18,7 +18,7 @@ extension RSHTMLMetadata { return nil } - var bestImage: RSHTMLOpenGraphImage? + var bestImage: HTMLOpenGraphImage? for image in openGraphImages { if image.width / image.height > 2 { @@ -54,7 +54,7 @@ extension RSHTMLMetadata { return nil } - var bestImage: RSHTMLMetadataAppleTouchIcon? + var bestImage: HTMLMetadataAppleTouchIcon? for image in icons { if image.size.width / image.size.height > 2 {