Fix crash when comments present in html
This commit is contained in:
parent
e5363b2e21
commit
29b594207c
|
@ -32,17 +32,20 @@ struct HTMLConverter {
|
||||||
let doc = try! SwiftSoup.parseBodyFragment(html)
|
let doc = try! SwiftSoup.parseBodyFragment(html)
|
||||||
let body = doc.body()!
|
let body = doc.body()!
|
||||||
|
|
||||||
let attributedText = attributedTextForHTMLNode(body)
|
if let attributedText = attributedTextForHTMLNode(body) {
|
||||||
let mutAttrString = NSMutableAttributedString(attributedString: attributedText)
|
let mutAttrString = NSMutableAttributedString(attributedString: attributedText)
|
||||||
mutAttrString.trimTrailingCharactersInSet(.whitespacesAndNewlines)
|
mutAttrString.trimTrailingCharactersInSet(.whitespacesAndNewlines)
|
||||||
mutAttrString.collapseWhitespace()
|
mutAttrString.collapseWhitespace()
|
||||||
|
|
||||||
mutAttrString.addAttribute(.paragraphStyle, value: paragraphStyle, range: mutAttrString.fullRange)
|
mutAttrString.addAttribute(.paragraphStyle, value: paragraphStyle, range: mutAttrString.fullRange)
|
||||||
|
|
||||||
return mutAttrString
|
return mutAttrString
|
||||||
|
} else {
|
||||||
|
return NSAttributedString()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func attributedTextForHTMLNode(_ node: Node, usePreformattedText: Bool = false) -> NSAttributedString {
|
private func attributedTextForHTMLNode(_ node: Node, usePreformattedText: Bool = false) -> NSAttributedString? {
|
||||||
switch node {
|
switch node {
|
||||||
case let node as TextNode:
|
case let node as TextNode:
|
||||||
let text: String
|
let text: String
|
||||||
|
@ -65,7 +68,9 @@ struct HTMLConverter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
attributed.append(attributedTextForHTMLNode(child, usePreformattedText: usePreformattedText || node.tagName() == "pre"))
|
if let childText = attributedTextForHTMLNode(child, usePreformattedText: usePreformattedText || node.tagName() == "pre") {
|
||||||
|
attributed.append(childText)
|
||||||
|
}
|
||||||
|
|
||||||
if appendEllipsis {
|
if appendEllipsis {
|
||||||
attributed.append(NSAttributedString("…"))
|
attributed.append(NSAttributedString("…"))
|
||||||
|
@ -134,10 +139,8 @@ struct HTMLConverter {
|
||||||
}
|
}
|
||||||
|
|
||||||
return attributed
|
return attributed
|
||||||
case is DataNode:
|
|
||||||
return NSAttributedString()
|
|
||||||
default:
|
default:
|
||||||
fatalError("Unexpected node type \(type(of: node))")
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue