Compare commits
No commits in common. "395f5965bcf928f9f7b5d3cc25d59520f77c21fb" and "d396a800f63d8c793bbdb9c078f96412ac895395" have entirely different histories.
395f5965bc
...
d396a800f6
|
@ -26,7 +26,6 @@ public struct AttributedStringConverter<Callbacks: AttributedStringCallbacks> {
|
||||||
|
|
||||||
private var actionStack: [ElementAction] = []
|
private var actionStack: [ElementAction] = []
|
||||||
private var styleStack: [Style] = []
|
private var styleStack: [Style] = []
|
||||||
private var previouslyFinishedBlockElement = false
|
|
||||||
// The current run of text w/o styles changing
|
// The current run of text w/o styles changing
|
||||||
private var currentRun: String = ""
|
private var currentRun: String = ""
|
||||||
|
|
||||||
|
@ -145,7 +144,6 @@ public struct AttributedStringConverter<Callbacks: AttributedStringCallbacks> {
|
||||||
|
|
||||||
private mutating func startBlockElement() {
|
private mutating func startBlockElement() {
|
||||||
if str.length != 0 || !currentRun.isEmpty {
|
if str.length != 0 || !currentRun.isEmpty {
|
||||||
previouslyFinishedBlockElement = false
|
|
||||||
currentRun.append("\n\n")
|
currentRun.append("\n\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -172,19 +170,15 @@ public struct AttributedStringConverter<Callbacks: AttributedStringCallbacks> {
|
||||||
case "pre":
|
case "pre":
|
||||||
finishRun()
|
finishRun()
|
||||||
removeLastStyle(.monospace)
|
removeLastStyle(.monospace)
|
||||||
finishBlockElement()
|
|
||||||
case "blockquote":
|
case "blockquote":
|
||||||
finishRun()
|
finishRun()
|
||||||
removeLastStyle(.blockquote)
|
removeLastStyle(.blockquote)
|
||||||
finishBlockElement()
|
|
||||||
case "ol":
|
case "ol":
|
||||||
finishRun()
|
finishRun()
|
||||||
removeLastStyle(.orderedList)
|
removeLastStyle(.orderedList)
|
||||||
finishBlockElement()
|
|
||||||
case "ul":
|
case "ul":
|
||||||
finishRun()
|
finishRun()
|
||||||
removeLastStyle(.unorderedList)
|
removeLastStyle(.unorderedList)
|
||||||
finishBlockElement()
|
|
||||||
case "li":
|
case "li":
|
||||||
finishRun()
|
finishRun()
|
||||||
default:
|
default:
|
||||||
|
@ -192,13 +186,6 @@ 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.
|
// 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.
|
// We can't just use the last one because we need to handle mis-nested tags.
|
||||||
private mutating func removeLastStyle(_ type: Style.StyleType) {
|
private mutating func removeLastStyle(_ type: Style.StyleType) {
|
||||||
|
@ -248,11 +235,6 @@ public struct AttributedStringConverter<Callbacks: AttributedStringCallbacks> {
|
||||||
currentRun = replacement
|
currentRun = replacement
|
||||||
}
|
}
|
||||||
|
|
||||||
if previouslyFinishedBlockElement {
|
|
||||||
previouslyFinishedBlockElement = false
|
|
||||||
currentRun.insert(contentsOf: "\n\n", at: currentRun.startIndex)
|
|
||||||
}
|
|
||||||
|
|
||||||
var attributes = [NSAttributedString.Key: Any]()
|
var attributes = [NSAttributedString.Key: Any]()
|
||||||
var currentFontTraits: FontTrait = []
|
var currentFontTraits: FontTrait = []
|
||||||
for style in styleStack {
|
for style in styleStack {
|
||||||
|
|
|
@ -12,9 +12,6 @@ final class AttributedStringConverterTests: XCTestCase {
|
||||||
|
|
||||||
#if os(iOS)
|
#if os(iOS)
|
||||||
private let font = UIFont.systemFont(ofSize: 13)
|
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)
|
private let monospaceFont = UIFont.monospacedSystemFont(ofSize: 13, weight: .regular)
|
||||||
#elseif os(macOS)
|
#elseif os(macOS)
|
||||||
private let font = NSFont.systemFont(ofSize: 13)
|
private let font = NSFont.systemFont(ofSize: 13)
|
||||||
|
@ -170,36 +167,6 @@ 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() {
|
func testSelfClosing() {
|
||||||
XCTAssertEqual(convert("<b />asdf"), NSAttributedString(string: "asdf", attributes: [
|
XCTAssertEqual(convert("<b />asdf"), NSAttributedString(string: "asdf", attributes: [
|
||||||
.font: font,
|
.font: font,
|
||||||
|
|
Loading…
Reference in New Issue