// // 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 image = HTMLImageParser.significantImages(html: html, baseURL: URL(string: item.url ?? ""), maximumCount: 1).first if let image { foundImageCount += 1 #expect(!image.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(image?.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(firstSignificantImage(html)?.urlString == "https://example.com/real.jpg")
}
@Test func shareButtonsAreSkipped() {
let html = """
"""
#expect(firstSignificantImage(html)?.urlString == "https://example.com/photo.jpg")
}
@Test func trackingPixelWithDeclaredDimensionsIsSkipped() {
let html = """
"""
#expect(firstSignificantImage(html) == HTMLImage(urlString: "https://example.com/photo.jpg", width: 640, height: 480))
}
@Test func imageWithUndeclaredDimensionsIsAccepted() {
// Real dimensions are checked at download time.
let html = #"
"#
#expect(firstSignificantImage(html) == HTMLImage(urlString: "https://example.com/photo.jpg", width: nil, height: nil))
}
@Test func gravatarIsSkipped() {
let html = """
"""
#expect(firstSignificantImage(html)?.urlString == "https://example.com/photo.jpg")
}
@Test func srcsetIsUsedWhenSrcIsPlaceholder() {
let html = """
"""
#expect(firstSignificantImage(html)?.urlString == "https://example.com/a-1024.jpg")
}
@Test func srcsetIsUsedBeforeSrc() {
let html = #"
"#
#expect(firstSignificantImage(html)?.urlString == "https://example.com/real-800.jpg")
}
@Test func srcsetUsesNarrowestCandidateWideEnoughForThumbnail() {
let html = #"
"""
#expect(firstSignificantImage(html)?.urlString == "https://example.com/uploads/photo.jpg")
}
@Test func lazySrcsetAndOriginalAreUsedWhenSrcIsPlaceholder() {
let placeholder = "data:image/gif;base64,R0lGODlhAQABAAAAACw="
#expect(firstSignificantImage(#"
"#)?.urlString == "https://example.com/a-800.jpg")
#expect(firstSignificantImage(#"
"#)?.urlString == "https://example.com/b-800.jpg")
#expect(firstSignificantImage(#"
"#)?.urlString == "https://example.com/c.jpg")
}
@Test func significantImagesAreFoundUpToMaximumCount() {
let html = """



"""
let images = HTMLImageParser.significantImages(html: html, baseURL: URL(string: "https://example.com/posts/1"), maximumCount: 2)
#expect(images.map(\.urlString) == ["https://example.com/a.jpg", "https://example.com/b.jpg"])
}
@Test func relativeURLsAreResolved() {
#expect(firstSignificantImage(#"
"#)?.urlString == "https://example.com/posts/images/a.jpg")
#expect(firstSignificantImage(#"
"#)?.urlString == "https://cdn.example.net/a.jpg")
}
@Test func relativeURLIsSkippedWithoutBaseURL() {
let html = #"
"#
let images = HTMLImageParser.significantImages(html: html, baseURL: nil, maximumCount: 3)
#expect(images.map(\.urlString) == ["https://example.com/b.jpg"])
}
@Test func dimensionsInPixelsAreParsed() {
let html = #"
"#
#expect(firstSignificantImage(html) == HTMLImage(urlString: "https://example.com/a.jpg", width: nil, height: 300))
}
@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: 250))
#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 firstSignificantImage(_ html: String) -> HTMLImage? {
HTMLImageParser.significantImages(html: html, baseURL: URL(string: "https://example.com/posts/1"), maximumCount: 1).first
}
"#
#expect(firstSignificantImage(html)?.urlString == "https://example.com/a-1536.jpg")
}
@Test func srcsetDescriptorCanEndWithNewlineOrHaveHeight() {
let htmlWithNewlines = """