Compare commits

...

2 Commits

2 changed files with 51 additions and 0 deletions

View File

@ -26,6 +26,7 @@ public struct AttributedStringConverter<Callbacks: AttributedStringCallbacks> {
private var actionStack: [ElementAction] = []
private var styleStack: [Style] = []
private var previouslyFinishedBlockElement = false
// The current run of text w/o styles changing
private var currentRun: String = ""
@ -144,6 +145,7 @@ public struct AttributedStringConverter<Callbacks: AttributedStringCallbacks> {
private mutating func startBlockElement() {
if str.length != 0 || !currentRun.isEmpty {
previouslyFinishedBlockElement = false
currentRun.append("\n\n")
}
}
@ -170,15 +172,19 @@ public struct AttributedStringConverter<Callbacks: AttributedStringCallbacks> {
case "pre":
finishRun()
removeLastStyle(.monospace)
finishBlockElement()
case "blockquote":
finishRun()
removeLastStyle(.blockquote)
finishBlockElement()
case "ol":
finishRun()
removeLastStyle(.orderedList)
finishBlockElement()
case "ul":
finishRun()
removeLastStyle(.unorderedList)
finishBlockElement()
case "li":
finishRun()
default:
@ -186,6 +192,13 @@ public struct AttributedStringConverter<Callbacks: AttributedStringCallbacks> {
}
}
private mutating func finishBlockElement() {
if str.length != 0 {
previouslyFinishedBlockElement = true
// currentRun.append("\n\n")
}
}
// Finds the last currently-open style of the given type.
// We can't just use the last one because we need to handle mis-nested tags.
private mutating func removeLastStyle(_ type: Style.StyleType) {
@ -235,6 +248,11 @@ public struct AttributedStringConverter<Callbacks: AttributedStringCallbacks> {
currentRun = replacement
}
if previouslyFinishedBlockElement {
previouslyFinishedBlockElement = false
currentRun.insert(contentsOf: "\n\n", at: currentRun.startIndex)
}
var attributes = [NSAttributedString.Key: Any]()
var currentFontTraits: FontTrait = []
for style in styleStack {

View File

@ -12,6 +12,9 @@ final class AttributedStringConverterTests: XCTestCase {
#if os(iOS)
private let font = UIFont.systemFont(ofSize: 13)
private lazy var italicFont = UIFont(descriptor: font.fontDescriptor.withSymbolicTraits([.traitItalic])!, size: 13)
private lazy var boldFont = UIFont(descriptor: font.fontDescriptor.withSymbolicTraits([.traitBold])!, size: 13)
private lazy var boldItalicFont = UIFont(descriptor: font.fontDescriptor.withSymbolicTraits([.traitBold, .traitItalic])!, size: 13)
private let monospaceFont = UIFont.monospacedSystemFont(ofSize: 13, weight: .regular)
#elseif os(macOS)
private let font = NSFont.systemFont(ofSize: 13)
@ -167,6 +170,36 @@ final class AttributedStringConverterTests: XCTestCase {
]))
}
func testTextAfterBlockquote() {
let result = NSMutableAttributedString()
result.append(NSAttributedString(string: "wee", attributes: [
.font: italicFont,
.paragraphStyle: blockquoteParagraphStyle,
]))
result.append(NSAttributedString(string: "\n\nafter", attributes: [
.font: font,
.paragraphStyle: NSParagraphStyle.default,
]))
XCTAssertEqual(convert("<blockquote>wee</blockquote>after"), result)
}
func testMultipleBlockElements() {
let result = NSMutableAttributedString()
result.append(NSAttributedString(string: "a", attributes: [
.font: italicFont,
.paragraphStyle: blockquoteParagraphStyle,
]))
result.append(NSAttributedString(string: "\n\n", attributes: [
.font: font,
.paragraphStyle: NSParagraphStyle.default,
]))
result.append(NSAttributedString(string: "b", attributes: [
.font: italicFont,
.paragraphStyle: blockquoteParagraphStyle,
]))
XCTAssertEqual(convert("<blockquote>a</blockquote><blockquote>b</blockquote>"), result)
}
func testSelfClosing() {
XCTAssertEqual(convert("<b />asdf"), NSAttributedString(string: "asdf", attributes: [
.font: font,