From 098ee21d499a3eecd528c3fea889c4ebadffef79 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sun, 25 Oct 2026 11:45:36 -0700 Subject: [PATCH] Add HTMLImageParserTests. --- .../HTML/HTMLImageParserTests.swift | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 Modules/RSParser/Tests/RSParserTests/HTML/HTMLImageParserTests.swift diff --git a/Modules/RSParser/Tests/RSParserTests/HTML/HTMLImageParserTests.swift b/Modules/RSParser/Tests/RSParserTests/HTML/HTMLImageParserTests.swift new file mode 100644 index 000000000..9e9b8c7ca --- /dev/null +++ b/Modules/RSParser/Tests/RSParserTests/HTML/HTMLImageParserTests.swift @@ -0,0 +1,111 @@ +// +// 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.firstSignificantImage(with: ParserData(url: item.url ?? "", data: Data(html.utf8))) + 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) == nil) + } + + @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/pixel.gif", 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 relativeURLsAreResolved() { + #expect(firstSignificantImage(#""#)?.urlString == "https://example.com/posts/images/a.jpg") + #expect(firstSignificantImage(#""#)?.urlString == "https://cdn.example.net/a.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: 300)) + } +} + +// MARK: - Helpers + +private func firstSignificantImage(_ html: String) -> HTMLImage? { + HTMLImageParser.firstSignificantImage(with: ParserData(url: "https://example.com/posts/1", data: Data(html.utf8))) +}