Handle break tags at the beginning of paragraphs

This commit is contained in:
Shadowfacts 2024-05-22 17:48:29 -04:00
parent 35ed55edf2
commit 5fbf806e73
3 changed files with 30 additions and 3 deletions

View File

@ -35,7 +35,8 @@ digraph blockstate {
nonEmptyBlock -> beginListItem [label = "<li>"]; nonEmptyBlock -> beginListItem [label = "<li>"];
nonEmptyBlock -> endListItem [label = "</li>"]; nonEmptyBlock -> endListItem [label = "</li>"];
emptyBlock -> nonEmptyBlock [label = "non-whitespace (block break)"]; emptyBlock -> nonEmptyBlock [label = "non-whitespace (block break)"];
emptyBlock -> emptyBlock [label = "whitespace (skip)\n<br>\n</pre>\nstart/end block"]; emptyBlock -> emptyBlock [label = "whitespace (skip)\n</pre>\nstart/end block"];
emptyBlock -> lineBreakTag [label = "<br> (append to tmp, block break)"];
emptyBlock -> afterPreStartTag [label = "<pre> (depth = 1)"]; emptyBlock -> afterPreStartTag [label = "<pre> (depth = 1)"];
emptyBlock -> beginListItem [label = "<li>"]; emptyBlock -> beginListItem [label = "<li>"];
emptyBlock -> endListItem [label = "</li>"]; emptyBlock -> endListItem [label = "</li>"];

View File

@ -19,7 +19,11 @@ import Foundation
*/ */
struct BlockStateMachine { struct BlockStateMachine {
var blockState: BlockState = .start var blockState: BlockState = .start {
didSet {
print("new state: \(blockState)")
}
}
let blockBreak: String let blockBreak: String
let lineBreak: String let lineBreak: String
let listIndentForContentOutsideItem: String let listIndentForContentOutsideItem: String
@ -30,6 +34,7 @@ struct BlockStateMachine {
extension BlockStateMachine { extension BlockStateMachine {
mutating func startOrEndBlock() { mutating func startOrEndBlock() {
print("start/end block")
switch blockState { switch blockState {
case .start: case .start:
break break
@ -78,6 +83,7 @@ extension BlockStateMachine {
} }
mutating func continueBlock(char: UnicodeScalar) -> Bool { mutating func continueBlock(char: UnicodeScalar) -> Bool {
print("continue block: \(char.debugDescription)")
let isNewline = char == "\n" let isNewline = char == "\n"
let isWhitespace = isNewline || isWhitespace(char) let isWhitespace = isNewline || isWhitespace(char)
switch blockState { switch blockState {
@ -292,11 +298,14 @@ extension BlockStateMachine {
} }
mutating func breakTag() { mutating func breakTag() {
print("break tag")
switch blockState { switch blockState {
case .start: case .start:
break break
case .emptyBlock: case .emptyBlock:
append(lineBreak) blockState = .lineBreakTag
append(blockBreak)
temporaryBuffer.append(lineBreak)
case .nonEmptyBlock: case .nonEmptyBlock:
blockState = .lineBreakTag blockState = .lineBreakTag
temporaryBuffer.append(lineBreak) temporaryBuffer.append(lineBreak)

View File

@ -662,4 +662,21 @@ final class AttributedStringConverterTests: XCTestCase {
XCTAssertEqual(convert(html, callbacks: Replace.self), result) XCTAssertEqual(convert(html, callbacks: Replace.self), result)
} }
func testLineBreakAtBeginningOfBlockElement() {
let result = NSAttributedString(string: "a\n\n\n\nb", attributes: [
.font: font,
.paragraphStyle: NSParagraphStyle.default,
.foregroundColor: color,
])
XCTAssertEqual(convert("<p>a</p><p><br></p><p>b</p>"), result)
let result2 = NSAttributedString(string: "a\n\n\nb\n\nc", attributes: [
.font: font,
.paragraphStyle: NSParagraphStyle.default,
.foregroundColor: color,
])
XCTAssertEqual(convert("<p>a</p><p><br>b</p><p>c</p>"), result2)
}
} }