Skip to content

Commit 4d252d1

Browse files
weiranclaude
andcommitted
Fix crash when tapping Hacker News comment permalinks
- Replace fragile string manipulation with robust URLComponents parsing in CommentTableViewCell - Add defensive programming to HtmlParser to handle comment permalink HTML structure - Prevents crash on line 45 of HtmlParser when parsing comment pages - Fixes issue #294: app now gracefully handles comment permalinks 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 1082dc1 commit 4d252d1

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

App/Comments/CommentTableViewCell.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,9 @@ extension CommentTableViewCell: UITextViewDelegate {
101101
if
102102
let host = URL.host,
103103
host.contains("news.ycombinator.com"),
104-
let range = URL.absoluteString.range(of: "id="),
105-
let id = Int(
106-
URL.absoluteString[range.upperBound...]
107-
.trimmingCharacters(in: .whitespaces)
108-
) {
104+
let components = URLComponents(url: URL, resolvingAgainstBaseURL: true),
105+
let idString = components.queryItems?.first(where: { $0.name == "id" })?.value,
106+
let id = Int(idString) {
109107
commentDelegate.internalLinkTapped(postId: id, url: URL, sender: textView)
110108
} else {
111109
commentDelegate.linkTapped(URL, sender: textView)

Shared Frameworks/HackersKit/HtmlParser.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ enum HtmlParser {
4242
throw Exception.Error(type: .SelectorParseException, Message: "Couldn't parse post ID")
4343
}
4444
let urlString = try postElement.select(".titleline").select("a").attr("href")
45-
let title = try postElement.select(".titleline").select("a").first()!.text()
45+
guard let titleElement = try postElement.select(".titleline").select("a").first() else {
46+
throw Exception.Error(type: .SelectorParseException, Message: "Couldn't find title element")
47+
}
48+
let title = try titleElement.text()
4649
guard let url = URL(string: urlString) else {
4750
throw Exception.Error(type: .SelectorParseException, Message: "Couldn't parse post URL")
4851
}

0 commit comments

Comments
 (0)