mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-11-30 17:29:23 +00:00
Skip images hidden by hidden attribute or style none. Trying to avoid tracking pixels as thumbnails.
This commit is contained in:
1 parent
8955619c40
commit
b00dce516d
2 files changed
+36
-2
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -44,6 +44,17 @@ import RSParser
|
||||
#expect(firstSignificantImage(html) == nil)
|
||||
}
|
||||
|
||||
@Test func hiddenImagesAreSkipped() {
|
||||
let html = """
|
||||
<img src="https://t.example.com/o/abc" style="display:none">
|
||||
<img src="https://t.example.com/o/def" style="DISPLAY: none !important">
|
||||
<img src="https://t.example.com/o/ghi" hidden>
|
||||
<img src="https://t.example.com/o/jkl" style="width:1px;height:1px">
|
||||
<img src="/real.jpg" style="max-width:1px;width:100%">
|
||||
"""
|
||||
#expect(firstSignificantImage(html)?.urlString == "https://example.com/real.jpg")
|
||||
}
|
||||
|
||||
@Test func shareButtonsAreSkipped() {
|
||||
let html = """
|
||||
<div class="sharedaddy">
|
||||
|
||||
Reference in new issue
Block a user