diff --git a/Modules/RSParser/Sources/RSParser/HTML/HTMLImageParser.swift b/Modules/RSParser/Sources/RSParser/HTML/HTMLImageParser.swift
index 68d4ce7eb..0d2d5ecc4 100644
--- a/Modules/RSParser/Sources/RSParser/HTML/HTMLImageParser.swift
+++ b/Modules/RSParser/Sources/RSParser/HTML/HTMLImageParser.swift
@@ -77,8 +77,14 @@ private final class ImageParserDelegate: HTMLScannerDelegate {
return
}
- let width = pixelDimension(attributes["width"])
- let height = pixelDimension(attributes["height"])
+ let style = styleProperties(attributes["style"])
+ if attributes["hidden"] != nil || style["display"]?.hasPrefix("none") == true {
+ return
+ }
+
+ // CSS overrides the attributes.
+ let width = pixelDimension(style["width"]) ?? pixelDimension(attributes["width"])
+ let height = pixelDimension(style["height"]) ?? pixelDimension(attributes["height"])
let htmlClass = attributes["class"]
for urlString in imageURLStrings(attributes) {
if SignificantImageFilter.isSignificant(urlString: urlString, width: width, height: height, htmlClass: htmlClass, enclosingLinkURLString: enclosingLinkURLString) {
@@ -165,6 +171,23 @@ private final class ImageParserDelegate: HTMLScannerDelegate {
return String(candidate.url)
}
+ /// Lowercase property names and values from a `style` attribute.
+ private func styleProperties(_ style: String?) -> [String: String] {
+ guard let style else {
+ return [:]
+ }
+ var properties = [String: String]()
+ for declaration in style.split(separator: ";") {
+ let nameAndValue = declaration.split(separator: ":", maxSplits: 1)
+ guard nameAndValue.count == 2 else {
+ continue
+ }
+ let name = nameAndValue[0].trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
+ properties[name] = nameAndValue[1].trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
+ }
+ return properties
+ }
+
/// `"300"` and `"300px"` are pixel dimensions. Percentages and other units are not.
private func pixelDimension(_ value: String?) -> Int? {
guard var value = value?.trimmingCharacters(in: .whitespaces).lowercased() else {
diff --git a/Modules/RSParser/Tests/RSParserTests/HTML/HTMLImageParserTests.swift b/Modules/RSParser/Tests/RSParserTests/HTML/HTMLImageParserTests.swift
index f59d36326..4a1454beb 100644
--- a/Modules/RSParser/Tests/RSParserTests/HTML/HTMLImageParserTests.swift
+++ b/Modules/RSParser/Tests/RSParserTests/HTML/HTMLImageParserTests.swift
@@ -44,6 +44,17 @@ import RSParser
#expect(firstSignificantImage(html) == nil)
}
+ @Test func hiddenImagesAreSkipped() {
+ let html = """
+
+
+
+
+
+ """
+ #expect(firstSignificantImage(html)?.urlString == "https://example.com/real.jpg")
+ }
+
@Test func shareButtonsAreSkipped() {
let html = """