mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-12-02 18:32:55 +00:00
Use new HTMLMetadata and HTMLLink parsers.
This commit is contained in:
1 parent
88abb40a67
commit
89f009ced0
9 files changed
+228
-27
No files matched your search
@@ -20,7 +20,7 @@ final class HTMLFeedFinder {
|
||||
private var feedSpecifiersDictionary = [String: FeedSpecifier]()
|
||||
|
||||
init(parserData: ParserData) {
|
||||
let metadata = RSHTMLMetadataParser.htmlMetadata(with: parserData)
|
||||
let metadata = HTMLMetadataParser.htmlMetadata(with: parserData)
|
||||
var orderFound = 0
|
||||
|
||||
for oneFeedLink in metadata.feedLinks {
|
||||
@@ -31,7 +31,7 @@ final class HTMLFeedFinder {
|
||||
}
|
||||
}
|
||||
|
||||
let bodyLinks = RSHTMLLinkParser.htmlLinks(with: parserData)
|
||||
let bodyLinks = HTMLLinkParser.htmlLinks(with: parserData)
|
||||
for oneBodyLink in bodyLinks {
|
||||
if linkMightBeFeed(oneBodyLink), let normalizedURL = oneBodyLink.urlString?.normalizedURL {
|
||||
orderFound += 1
|
||||
@@ -68,7 +68,7 @@ private extension HTMLFeedFinder {
|
||||
return false
|
||||
}
|
||||
|
||||
func linkMightBeFeed(_ link: RSHTMLLink) -> Bool {
|
||||
func linkMightBeFeed(_ link: HTMLLink) -> Bool {
|
||||
if let linkURLString = link.urlString, urlStringMightBeFeed(linkURLString) {
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ final class HTMLLinkPerformanceTests: XCTestCase {
|
||||
// 0.003 sec on my 2012 iMac
|
||||
let d = parserData("sixcolors", "html", "http://sixcolors.com/")
|
||||
self.measure {
|
||||
_ = RSHTMLLinkParser.htmlLinks(with: d)
|
||||
_ = HTMLLinkParser.htmlLinks(with: d)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ import RSParserObjC
|
||||
|
||||
@Test func sixColorsLink() {
|
||||
let d = parserData("sixcolors", "html", "http://sixcolors.com/")
|
||||
let links = RSHTMLLinkParser.htmlLinks(with: d)
|
||||
let links = HTMLLinkParser.htmlLinks(with: d)
|
||||
|
||||
let linkToFind = "https://www.theincomparable.com/theincomparable/290/index.php"
|
||||
let textToFind = "this week’s episode of The Incomparable"
|
||||
@@ -30,4 +30,120 @@ import RSParserObjC
|
||||
#expect(found)
|
||||
#expect(links.count == 131)
|
||||
}
|
||||
|
||||
// MARK: - Inline fixture tests
|
||||
|
||||
@Test func linksInsideScriptAreIgnored() {
|
||||
// The scanner enters raw-text mode for <script> — `<a href>` inside JS
|
||||
// string literals must not be picked up. Test content has to look enough
|
||||
// like markup to trip a naive scanner.
|
||||
let html = """
|
||||
<html><body>
|
||||
<a href="http://real.example.com/">real</a>
|
||||
<script>
|
||||
var markup = '<a href="http://not-a-link.example.com/">nope</a>';
|
||||
var also = "<a href='http://also-not.example.com/'>also nope</a>";
|
||||
</script>
|
||||
<a href="http://real2.example.com/">real 2</a>
|
||||
</body></html>
|
||||
"""
|
||||
let d = ParserData(url: "http://example.com/", data: Data(html.utf8))
|
||||
let links = HTMLLinkParser.htmlLinks(with: d)
|
||||
let urls = links.compactMap { $0.urlString }
|
||||
#expect(urls.contains("http://real.example.com/"))
|
||||
#expect(urls.contains("http://real2.example.com/"))
|
||||
#expect(!urls.contains("http://not-a-link.example.com/"))
|
||||
#expect(!urls.contains("http://also-not.example.com/"))
|
||||
}
|
||||
|
||||
@Test func linksInsideStyleAreIgnored() {
|
||||
// <style> is also raw-text. Background-image URL inside CSS must not
|
||||
// be picked up as a link.
|
||||
let html = """
|
||||
<html><body>
|
||||
<style>
|
||||
.banner { background: url('http://css-url.example.com/bg.png'); }
|
||||
/* <a href="http://commented-out.example.com/">x</a> */
|
||||
</style>
|
||||
<a href="http://real.example.com/">real</a>
|
||||
</body></html>
|
||||
"""
|
||||
let d = ParserData(url: "http://example.com/", data: Data(html.utf8))
|
||||
let links = HTMLLinkParser.htmlLinks(with: d)
|
||||
let urls = links.compactMap { $0.urlString }
|
||||
#expect(urls == ["http://real.example.com/"])
|
||||
}
|
||||
|
||||
@Test func relativeURLsResolvedAgainstPageURL() {
|
||||
// The parser's second argument is the page URL; relative hrefs must
|
||||
// resolve against it so the caller gets absolute URLs back.
|
||||
let html = """
|
||||
<html><body>
|
||||
<a href="/foo">absolute path</a>
|
||||
<a href="bar.html">relative path</a>
|
||||
<a href="http://other.example.com/x">already absolute</a>
|
||||
</body></html>
|
||||
"""
|
||||
let d = ParserData(url: "http://example.com/page/", data: Data(html.utf8))
|
||||
let links = HTMLLinkParser.htmlLinks(with: d)
|
||||
let urls = links.compactMap { $0.urlString }
|
||||
#expect(urls.contains("http://example.com/foo"))
|
||||
#expect(urls.contains("http://example.com/page/bar.html"))
|
||||
#expect(urls.contains("http://other.example.com/x"))
|
||||
}
|
||||
|
||||
@Test func unclosedTagsDoNotCrashAndStillFindLinks() {
|
||||
// Malformed markup: the scanner must not hang or lose later links.
|
||||
let html = """
|
||||
<html><body>
|
||||
<p>Orphan <b>bold <i>italic
|
||||
<a href="http://example.com/first">first</a>
|
||||
<div>Unclosed div
|
||||
<a href="http://example.com/second">second</a>
|
||||
</body>
|
||||
"""
|
||||
let d = ParserData(url: "http://example.com/", data: Data(html.utf8))
|
||||
let links = HTMLLinkParser.htmlLinks(with: d)
|
||||
let urls = links.compactMap { $0.urlString }
|
||||
#expect(urls.contains("http://example.com/first"))
|
||||
#expect(urls.contains("http://example.com/second"))
|
||||
}
|
||||
|
||||
@Test func anchorWithoutHrefIgnored() {
|
||||
// `<a>` without href is an anchor target, not a link. Must not appear.
|
||||
let html = """
|
||||
<html><body>
|
||||
<a name="top"></a>
|
||||
<a href="http://example.com/">real</a>
|
||||
<a>no href at all</a>
|
||||
</body></html>
|
||||
"""
|
||||
let d = ParserData(url: "http://example.com/", data: Data(html.utf8))
|
||||
let links = HTMLLinkParser.htmlLinks(with: d)
|
||||
let urls = links.compactMap { $0.urlString }
|
||||
#expect(urls == ["http://example.com/"])
|
||||
}
|
||||
|
||||
@Test func entitiesInHrefDecoded() {
|
||||
// href attribute values routinely contain `&` as a query-parameter
|
||||
// separator. After extraction the caller expects the decoded `&` (so
|
||||
// the URL they get is the real URL, not the HTML-escaped spelling).
|
||||
// A regression that stopped decoding entities in attribute values
|
||||
// would silently break every URL with a `?a=1&b=2`-style query.
|
||||
let html = """
|
||||
<html><body>
|
||||
<a href="http://example.com/search?q=foo&page=2">two-param</a>
|
||||
<a href="http://example.com/a?x=1&y=2&z=3">three-param</a>
|
||||
</body></html>
|
||||
"""
|
||||
let d = ParserData(url: "http://example.com/", data: Data(html.utf8))
|
||||
let links = HTMLLinkParser.htmlLinks(with: d)
|
||||
let urls = links.compactMap { $0.urlString }
|
||||
#expect(urls.contains("http://example.com/search?q=foo&page=2"))
|
||||
#expect(urls.contains("http://example.com/a?x=1&y=2&z=3"))
|
||||
// No `&` should leak through.
|
||||
for u in urls {
|
||||
#expect(!u.contains("&"), "entity not decoded in href: \(u)")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,7 @@ final class HTMLMetadataPerformanceTests: XCTestCase {
|
||||
// 0.002 sec on my 2012 iMac
|
||||
let d = parserData("DaringFireball", "html", "http://daringfireball.net/")
|
||||
self.measure {
|
||||
_ = RSHTMLMetadataParser.htmlMetadata(with: d)
|
||||
_ = HTMLMetadataParser.htmlMetadata(with: d)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ final class HTMLMetadataPerformanceTests: XCTestCase {
|
||||
// 0.001 sec on my 2012 iMac
|
||||
let d = parserData("furbo", "html", "http://furbo.org/")
|
||||
self.measure {
|
||||
_ = RSHTMLMetadataParser.htmlMetadata(with: d)
|
||||
_ = HTMLMetadataParser.htmlMetadata(with: d)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ final class HTMLMetadataPerformanceTests: XCTestCase {
|
||||
// 0.001 sec on my 2012 iMac
|
||||
let d = parserData("inessential", "html", "http://inessential.com/")
|
||||
self.measure {
|
||||
_ = RSHTMLMetadataParser.htmlMetadata(with: d)
|
||||
_ = HTMLMetadataParser.htmlMetadata(with: d)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ final class HTMLMetadataPerformanceTests: XCTestCase {
|
||||
// 0.004 sec on my 2012 iMac
|
||||
let d = parserData("coco", "html", "https://www.theatlantic.com/entertainment/archive/2017/11/coco-is-among-pixars-best-movies-in-years/546695/")
|
||||
self.measure {
|
||||
_ = RSHTMLMetadataParser.htmlMetadata(with: d)
|
||||
_ = HTMLMetadataParser.htmlMetadata(with: d)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ final class HTMLMetadataPerformanceTests: XCTestCase {
|
||||
// 0.002 sec on my 2012 iMac
|
||||
let d = parserData("sixcolors", "html", "http://sixcolors.com/")
|
||||
self.measure {
|
||||
_ = RSHTMLMetadataParser.htmlMetadata(with: d)
|
||||
_ = HTMLMetadataParser.htmlMetadata(with: d)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ import RSParserObjC
|
||||
|
||||
@Test func daringFireball() throws {
|
||||
let d = parserData("DaringFireball", "html", "http://daringfireball.net/")
|
||||
let metadata = RSHTMLMetadataParser.htmlMetadata(with: d)
|
||||
let metadata = HTMLMetadataParser.htmlMetadata(with: d)
|
||||
|
||||
#expect(metadata.favicons.first?.urlString == "http://daringfireball.net/graphics/favicon.ico?v=005")
|
||||
|
||||
@@ -29,7 +29,7 @@ import RSParserObjC
|
||||
|
||||
@Test func furbo() throws {
|
||||
let d = parserData("furbo", "html", "http://furbo.org/")
|
||||
let metadata = RSHTMLMetadataParser.htmlMetadata(with: d)
|
||||
let metadata = HTMLMetadataParser.htmlMetadata(with: d)
|
||||
|
||||
#expect(metadata.favicons.first?.urlString == "http://furbo.org/favicon.ico")
|
||||
|
||||
@@ -42,7 +42,7 @@ import RSParserObjC
|
||||
|
||||
@Test func inessential() throws {
|
||||
let d = parserData("inessential", "html", "http://inessential.com/")
|
||||
let metadata = RSHTMLMetadataParser.htmlMetadata(with: d)
|
||||
let metadata = HTMLMetadataParser.htmlMetadata(with: d)
|
||||
|
||||
#expect(metadata.favicons.first?.urlString == nil)
|
||||
|
||||
@@ -57,7 +57,7 @@ import RSParserObjC
|
||||
|
||||
@Test func sixColors() throws {
|
||||
let d = parserData("sixcolors", "html", "http://sixcolors.com/")
|
||||
let metadata = RSHTMLMetadataParser.htmlMetadata(with: d)
|
||||
let metadata = HTMLMetadataParser.htmlMetadata(with: d)
|
||||
|
||||
#expect(metadata.favicons.first?.urlString == "https://sixcolors.com/images/favicon.ico")
|
||||
|
||||
@@ -76,7 +76,7 @@ import RSParserObjC
|
||||
|
||||
@Test func cocoOGImage() throws {
|
||||
let d = parserData("coco", "html", "https://www.theatlantic.com/entertainment/archive/2017/11/coco-is-among-pixars-best-movies-in-years/546695/")
|
||||
let metadata = RSHTMLMetadataParser.htmlMetadata(with: d)
|
||||
let metadata = HTMLMetadataParser.htmlMetadata(with: d)
|
||||
let openGraphData = metadata.openGraphProperties
|
||||
let image = try #require(openGraphData.images.first)
|
||||
#expect(image.url == "https://cdn.theatlantic.com/assets/media/img/mt/2017/11/1033101_first_full_length_trailer_arrives_pixars_coco/facebook.jpg?1511382177")
|
||||
@@ -84,7 +84,7 @@ import RSParserObjC
|
||||
|
||||
@Test func cocoTwitterImage() throws {
|
||||
let d = parserData("coco", "html", "https://www.theatlantic.com/entertainment/archive/2017/11/coco-is-among-pixars-best-movies-in-years/546695/")
|
||||
let metadata = RSHTMLMetadataParser.htmlMetadata(with: d)
|
||||
let metadata = HTMLMetadataParser.htmlMetadata(with: d)
|
||||
let twitterData = metadata.twitterProperties
|
||||
let imageURL = try #require(twitterData.imageURL)
|
||||
#expect(imageURL == "https://cdn.theatlantic.com/assets/media/img/mt/2017/11/1033101_first_full_length_trailer_arrives_pixars_coco/facebook.jpg?1511382177")
|
||||
@@ -93,7 +93,7 @@ import RSParserObjC
|
||||
@Test func youTube() throws {
|
||||
// YouTube is a special case — the feed links appear after the head section, in the body section.
|
||||
let d = parserData("YouTubeTheVolvoRocks", "html", "https://www.youtube.com/user/TheVolvorocks")
|
||||
let metadata = RSHTMLMetadataParser.htmlMetadata(with: d)
|
||||
let metadata = HTMLMetadataParser.htmlMetadata(with: d)
|
||||
|
||||
#expect(metadata.feedLinks.count == 1)
|
||||
let feedLink = try #require(metadata.feedLinks.first)
|
||||
@@ -101,4 +101,89 @@ import RSParserObjC
|
||||
#expect(feedLink.type == "application/rss+xml")
|
||||
#expect(feedLink.urlString == "https://www.youtube.com/feeds/videos.xml?channel_id=UCct7QF2jcWRY6dhXWMSq9LQ")
|
||||
}
|
||||
|
||||
// MARK: - Inline fixture tests
|
||||
|
||||
@Test func bothRSSAndAtomFeedLinksReported() throws {
|
||||
// When a page offers both RSS and Atom, both should appear in feedLinks.
|
||||
// Feed-discovery UI can then pick whichever it prefers.
|
||||
let html = """
|
||||
<html><head>
|
||||
<link rel="alternate" type="application/rss+xml" title="RSS" href="/feed.rss">
|
||||
<link rel="alternate" type="application/atom+xml" title="Atom" href="/feed.atom">
|
||||
</head><body></body></html>
|
||||
"""
|
||||
let d = ParserData(url: "http://example.com/", data: Data(html.utf8))
|
||||
let metadata = HTMLMetadataParser.htmlMetadata(with: d)
|
||||
#expect(metadata.feedLinks.count == 2)
|
||||
let types = Set(metadata.feedLinks.compactMap { $0.type })
|
||||
#expect(types == ["application/rss+xml", "application/atom+xml"])
|
||||
}
|
||||
|
||||
@Test func pageWithNoHeadReturnsEmptyMetadata() {
|
||||
// Malformed page, no <head>, no <link> / <meta>. Parser must not crash
|
||||
// and must report an empty-but-usable metadata struct.
|
||||
let html = "<html><body>Hello world</body></html>"
|
||||
let d = ParserData(url: "http://example.com/", data: Data(html.utf8))
|
||||
let metadata = HTMLMetadataParser.htmlMetadata(with: d)
|
||||
#expect(metadata.feedLinks.isEmpty)
|
||||
#expect(metadata.favicons.isEmpty)
|
||||
#expect(metadata.appleTouchIcons.isEmpty)
|
||||
}
|
||||
|
||||
@Test func relativeFaviconResolvedAgainstPageURL() throws {
|
||||
// Relative favicon href must resolve against the page URL so downloaders
|
||||
// don't have to. Also checks that multiple apple-touch-icons are preserved.
|
||||
let html = """
|
||||
<html><head>
|
||||
<link rel="icon" href="/favicon.ico">
|
||||
<link rel="apple-touch-icon" sizes="60x60" href="/touch-60.png">
|
||||
<link rel="apple-touch-icon" sizes="120x120" href="/touch-120.png">
|
||||
</head><body></body></html>
|
||||
"""
|
||||
let d = ParserData(url: "http://example.com/some/page.html", data: Data(html.utf8))
|
||||
let metadata = HTMLMetadataParser.htmlMetadata(with: d)
|
||||
#expect(metadata.favicons.first?.urlString == "http://example.com/favicon.ico")
|
||||
#expect(metadata.appleTouchIcons.count == 2)
|
||||
let sizes = Set(metadata.appleTouchIcons.compactMap { $0.sizes })
|
||||
#expect(sizes == ["60x60", "120x120"])
|
||||
let urls = Set(metadata.appleTouchIcons.compactMap { $0.urlString })
|
||||
#expect(urls == [
|
||||
"http://example.com/touch-60.png",
|
||||
"http://example.com/touch-120.png"
|
||||
])
|
||||
}
|
||||
|
||||
@Test func typelessFeedLinkAccepted() throws {
|
||||
// Some pages advertise their feed with `<link rel="alternate" href=…>`
|
||||
// and no `type` attribute. We accept those as feed links so auto-
|
||||
// discovery still works. An alternate with an explicit non-feed type
|
||||
// (`text/html` for i18n variants etc.) is still filtered out — see
|
||||
// `nonFeedTypedAlternateRejected`.
|
||||
let html = """
|
||||
<html><head>
|
||||
<link rel="alternate" href="/feed.xml">
|
||||
</head><body></body></html>
|
||||
"""
|
||||
let d = ParserData(url: "http://example.com/", data: Data(html.utf8))
|
||||
let metadata = HTMLMetadataParser.htmlMetadata(with: d)
|
||||
#expect(metadata.feedLinks.count == 1)
|
||||
let link = try #require(metadata.feedLinks.first)
|
||||
#expect(link.urlString == "http://example.com/feed.xml")
|
||||
#expect(link.type == nil)
|
||||
}
|
||||
|
||||
@Test func nonFeedTypedAlternateRejected() {
|
||||
// `rel="alternate"` with an explicit non-feed type must NOT be treated
|
||||
// as a feed link. This is the other half of the typeless-accepted rule:
|
||||
// we trust an explicit type when it says "not a feed."
|
||||
let html = """
|
||||
<html><head>
|
||||
<link rel="alternate" hreflang="fr" type="text/html" href="/fr/page.html">
|
||||
</head><body></body></html>
|
||||
"""
|
||||
let d = ParserData(url: "http://example.com/", data: Data(html.utf8))
|
||||
let metadata = HTMLMetadataParser.htmlMetadata(with: d)
|
||||
#expect(metadata.feedLinks.isEmpty)
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,7 @@ nonisolated public final class HTMLMetadataCache: Sendable {
|
||||
}
|
||||
|
||||
private struct HTMLMetadataCacheRecord: CacheRecord {
|
||||
let metadata: RSHTMLMetadata
|
||||
let metadata: HTMLMetadata
|
||||
let dateCreated = Date()
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ nonisolated public final class HTMLMetadataCache: Sendable {
|
||||
cache.removeAll()
|
||||
}
|
||||
|
||||
subscript(_ url: String) -> RSHTMLMetadata? {
|
||||
subscript(_ url: String) -> HTMLMetadata? {
|
||||
get {
|
||||
return cache[url]?.metadata
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ nonisolated public final class HTMLMetadataDownloader: Sendable {
|
||||
cache.removeAll()
|
||||
}
|
||||
|
||||
public func cachedMetadata(for url: String) -> RSHTMLMetadata? {
|
||||
public func cachedMetadata(for url: String) -> HTMLMetadata? {
|
||||
if Self.shouldSkipDownloadingMetadata(url) {
|
||||
Self.logger.debug("HTMLMetadataDownloader: Skipping requested cached metadata for \(url)")
|
||||
return nil
|
||||
@@ -105,7 +105,7 @@ nonisolated private extension HTMLMetadataDownloader {
|
||||
if let data, !data.isEmpty, let response, response.statusIsOK {
|
||||
let urlToUse = response.url ?? actualURL
|
||||
let parserData = ParserData(url: urlToUse.absoluteString, data: data)
|
||||
let htmlMetadata = RSHTMLMetadataParser.htmlMetadata(with: parserData)
|
||||
let htmlMetadata = HTMLMetadataParser.htmlMetadata(with: parserData)
|
||||
Self.logger.debug("HTMLMetadataDownloader caching parsed metadata for \(url)")
|
||||
cache[url] = htmlMetadata
|
||||
return
|
||||
|
||||
@@ -335,7 +335,7 @@ private extension FaviconDownloader {
|
||||
}
|
||||
}
|
||||
|
||||
private extension RSHTMLMetadata {
|
||||
private extension HTMLMetadata {
|
||||
|
||||
func usableFaviconURLs() -> [String]? {
|
||||
|
||||
@@ -346,7 +346,7 @@ private extension RSHTMLMetadata {
|
||||
|
||||
static let ignoredTypes = [UTType.svg]
|
||||
|
||||
private func shouldAllowFavicon(_ favicon: RSHTMLMetadataFavicon) -> Bool {
|
||||
private func shouldAllowFavicon(_ favicon: HTMLMetadataFavicon) -> Bool {
|
||||
|
||||
// Check mime type.
|
||||
if let mimeType = favicon.type, let utType = UTType(mimeType: mimeType) {
|
||||
|
||||
+4
-4
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// RSHTMLMetadata+Extension.swift
|
||||
// HTMLMetadata+Extension.swift
|
||||
// NetNewsWire
|
||||
//
|
||||
// Created by Brent Simmons on 11/26/17.
|
||||
@@ -9,7 +9,7 @@
|
||||
import Foundation
|
||||
import RSParser
|
||||
|
||||
extension RSHTMLMetadata {
|
||||
extension HTMLMetadata {
|
||||
|
||||
func largestOpenGraphImageURL() -> String? {
|
||||
let openGraphImages = openGraphProperties.images
|
||||
@@ -18,7 +18,7 @@ extension RSHTMLMetadata {
|
||||
return nil
|
||||
}
|
||||
|
||||
var bestImage: RSHTMLOpenGraphImage?
|
||||
var bestImage: HTMLOpenGraphImage?
|
||||
|
||||
for image in openGraphImages {
|
||||
if image.width / image.height > 2 {
|
||||
@@ -54,7 +54,7 @@ extension RSHTMLMetadata {
|
||||
return nil
|
||||
}
|
||||
|
||||
var bestImage: RSHTMLMetadataAppleTouchIcon?
|
||||
var bestImage: HTMLMetadataAppleTouchIcon?
|
||||
|
||||
for image in icons {
|
||||
if image.size.width / image.size.height > 2 {
|
||||
Reference in new issue
Block a user