Fix link ranges being stored incorrectly

This commit is contained in:
Shadowfacts 2018-08-21 21:43:43 -04:00
parent 65530dbfd0
commit f28e73442b
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 12 additions and 5 deletions

View File

@ -56,7 +56,8 @@ class StatusTableViewCell: UITableViewCell {
// print(status.content)
// print("---")
let text = attributedTextForNode(body)
let (text, links) = attributedTextForNode(body)
self.links = links
contentLabel.attributedText = text
contentLabel.isUserInteractionEnabled = true
contentLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTapOnContentLabel(_:))))
@ -74,14 +75,20 @@ class StatusTableViewCell: UITableViewCell {
textContainer.maximumNumberOfLines = contentLabel.numberOfLines
}
func attributedTextForNode(_ node: Node) -> NSAttributedString {
func attributedTextForNode(_ node: Node) -> (NSAttributedString, [NSRange: URL]) {
switch node {
case let node as TextNode:
return NSAttributedString(string: node.text())
return (NSAttributedString(string: node.text()), [:])
case let node as Element:
var links = [NSRange: URL]()
let attributed = NSMutableAttributedString()
node.getChildNodes().forEach { child in
attributed.append(attributedTextForNode(child))
let (text, childLinks) = attributedTextForNode(child)
childLinks.forEach { range, url in
let newRange = NSRange(location: range.location + attributed.length, length: range.length)
links[newRange] = url
}
attributed.append(text)
}
switch node.tagName() {
@ -102,7 +109,7 @@ class StatusTableViewCell: UITableViewCell {
break
}
return attributed
return (attributed, links)
default:
fatalError("Unexpected node type: \(type(of: node))")
}