// // HTMLImageParserTests.swift // RSParserTests // // Created by Brent Simmons on 10/25/26. // import Foundation import Testing import RSParser @Suite struct HTMLImageParserTests { @Test func smileysAreSkipped() throws { let parsedFeed = try #require(try FeedParser.parse(parserData("natasha", "xml", "https://www.natashatherobot.com/feed/"))) var foundImageCount = 0 for item in parsedFeed.items { guard let html = item.contentHTML else { continue } let urlString = HTMLImageParser.significantImageURLStrings(html: html, baseURL: URL(string: item.url ?? ""), maximumCount: 1).first if let urlString { foundImageCount += 1 #expect(!urlString.contains("s.w.org")) } if item.url == "https://www.natashatherobot.com/url-apple-wallet-passkit-pass/" { // The first image is 503×88, too wide to stand for the article. #expect(urlString == "https://www.natashatherobot.com/wp-content/uploads/Screen-Shot-2017-07-07-at-12.35.13-PM.png") } } #expect(foundImageCount > 0) } @Test func feedBurnerFlaresAndTrackingPixelAreSkipped() { let html = """

Text.

""" #expect(firstSignificantImageURLString(html) == nil) } @Test func hiddenImagesAreSkipped() { let html = """ """ #expect(firstSignificantImageURLString(html) == "https://example.com/real.jpg") } @Test func shareButtonsAreSkipped() { let html = """
""" #expect(firstSignificantImageURLString(html) == "https://example.com/photo.jpg") } @Test func trackingPixelWithDeclaredDimensionsIsSkipped() { let html = """ """ #expect(firstSignificantImageURLString(html) == "https://example.com/photo.jpg") } @Test func imageWithUndeclaredDimensionsIsAccepted() { // Real dimensions are checked at download time. let html = #""# #expect(firstSignificantImageURLString(html) == "https://example.com/photo.jpg") } @Test func gravatarIsSkipped() { let html = """ """ #expect(firstSignificantImageURLString(html) == "https://example.com/photo.jpg") } @Test func srcsetIsUsedWhenSrcIsPlaceholder() { let html = """ """ #expect(firstSignificantImageURLString(html) == "https://example.com/a-1024.jpg") } @Test func srcsetIsUsedBeforeSrc() { let html = #""# #expect(firstSignificantImageURLString(html) == "https://example.com/real-800.jpg") } @Test func srcsetUsesNarrowestCandidateWideEnoughForThumbnail() { let html = #""# #expect(firstSignificantImageURLString(html) == "https://example.com/a-1536.jpg") } @Test func srcsetDescriptorCanEndWithNewlineOrHaveHeight() { let htmlWithNewlines = """ """ #expect(firstSignificantImageURLString(htmlWithNewlines) == "https://example.com/a-800.jpg") let htmlWithHeights = #""# #expect(firstSignificantImageURLString(htmlWithHeights) == "https://example.com/a-800.jpg") } @Test func lazySourceIsUsedWhenSrcIsPlaceholder() { let html = """ """ #expect(firstSignificantImageURLString(html) == "https://example.com/uploads/photo.jpg") } @Test func lazySrcsetAndOriginalAreUsedWhenSrcIsPlaceholder() { let placeholder = "data:image/gif;base64,R0lGODlhAQABAAAAACw=" #expect(firstSignificantImageURLString(#""#) == "https://example.com/a-800.jpg") #expect(firstSignificantImageURLString(#""#) == "https://example.com/b-800.jpg") #expect(firstSignificantImageURLString(#""#) == "https://example.com/c.jpg") } @Test func significantImageURLStringsAreFoundUpToMaximumCount() { let html = """ """ let urlStrings = HTMLImageParser.significantImageURLStrings(html: html, baseURL: URL(string: "https://example.com/posts/1"), maximumCount: 2) #expect(urlStrings == ["https://example.com/a.jpg", "https://example.com/b.jpg"]) } @Test func relativeURLsAreResolved() { #expect(firstSignificantImageURLString(#""#) == "https://example.com/posts/images/a.jpg") #expect(firstSignificantImageURLString(#""#) == "https://cdn.example.net/a.jpg") } @Test func relativeURLIsSkippedWithoutBaseURL() { let html = #""# let urlStrings = HTMLImageParser.significantImageURLStrings(html: html, baseURL: nil, maximumCount: 3) #expect(urlStrings == ["https://example.com/b.jpg"]) } @Test func dimensionsInPixelsAreParsed() { // The 1px height is too small. The 50% width isn’t in pixels, so it doesn’t count toward the aspect ratio. let html = #""# #expect(firstSignificantImageURLString(html) == "https://example.com/b.jpg") } @Test func decorativeURLsAreRejected() { #expect(!SignificantImageFilter.isSignificant(urlString: "https://example.com/logo.svg", width: nil, height: nil)) #expect(!SignificantImageFilter.isSignificant(urlString: "https://s0.wp.com/i/blank.jpg", width: nil, height: nil)) #expect(!SignificantImageFilter.isSignificant(urlString: "https://pixel.wp.com/b.gif?v=1", width: nil, height: nil)) #expect(!SignificantImageFilter.isSignificant(urlString: "ftp://example.com/a.jpg", width: nil, height: nil)) #expect(!SignificantImageFilter.isSignificant(urlString: "https://example.com/banner.jpg", width: 1200, height: 150)) #expect(!SignificantImageFilter.isSignificant(urlString: "https://example.com/images/spacer.gif", width: nil, height: nil)) #expect(!SignificantImageFilter.isSignificant(urlString: "https://email.example.net/open/abc123.gif", width: nil, height: nil)) #expect(!SignificantImageFilter.isSignificant(urlString: "https://email.example.net/wf/open?upn=abc123", width: nil, height: nil)) #expect(SignificantImageFilter.isSignificant(urlString: "https://cdn.example.com/track/cover.jpg", width: nil, height: nil)) #expect(SignificantImageFilter.isSignificant(urlString: "https://example.com/2026/open-source-pixel-art.jpg", width: nil, height: nil)) } } // MARK: - Helpers private func firstSignificantImageURLString(_ html: String) -> String? { HTMLImageParser.significantImageURLStrings(html: html, baseURL: URL(string: "https://example.com/posts/1"), maximumCount: 1).first }