mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-12-01 09:50:16 +00:00
Check for data: URLs without copying the entire value (which could be huge).
This commit is contained in:
1 parent
6d5d3c27a1
commit
dcc94fa185
1 file changed
+13
-2
@@ -16,6 +16,8 @@ import Foundation
|
||||
/// often have a placeholder in `src`.
|
||||
public enum HTMLImageParser {
|
||||
|
||||
private static let dataURLPrefix = "data:"
|
||||
|
||||
/// In document order, with no repeated URLs.
|
||||
public static func significantImages(html: String, baseURL: URL?, maximumCount: Int) -> [HTMLImage] {
|
||||
let delegate = ImageParserDelegate(baseURL: baseURL, maximumCount: maximumCount)
|
||||
@@ -26,10 +28,19 @@ public enum HTMLImageParser {
|
||||
|
||||
/// Nil for an empty string or a `data:` URL.
|
||||
static func resolvedURLString(_ urlString: String?, baseURL: URL?) -> String? {
|
||||
guard let urlString = urlString?.trimmingCharacters(in: .whitespacesAndNewlines), !urlString.isEmpty, !urlString.lowercased().hasPrefix("data:") else {
|
||||
guard let urlString, !isDataURL(urlString) else {
|
||||
return nil
|
||||
}
|
||||
return URL(string: urlString, relativeTo: baseURL)?.absoluteString
|
||||
let trimmedURLString = urlString.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
guard !trimmedURLString.isEmpty else {
|
||||
return nil
|
||||
}
|
||||
return URL(string: trimmedURLString, relativeTo: baseURL)?.absoluteString
|
||||
}
|
||||
|
||||
/// Looks at only the start of the string, since an inline image can be megabytes.
|
||||
private static func isDataURL(_ urlString: String) -> Bool {
|
||||
urlString.drop(while: \.isWhitespace).prefix(dataURLPrefix.count).lowercased() == dataURLPrefix
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user