From e01664565fa9eaa9e6aeb6a7438f5d2c7d666879 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 8 Jul 2024 21:12:44 -0700 Subject: [PATCH] Fix error when whitespace occurs at end of

I had the state machine correct and made a mistake when converting it to code :/
---
 Sources/HTMLStreamer/BlockState.swift                 | 11 ++++++++++-
 .../AttributedStringConverterTests.swift              |  8 ++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/Sources/HTMLStreamer/BlockState.swift b/Sources/HTMLStreamer/BlockState.swift
index c409adc..b8fb124 100644
--- a/Sources/HTMLStreamer/BlockState.swift
+++ b/Sources/HTMLStreamer/BlockState.swift
@@ -19,7 +19,15 @@ import Foundation
  */
 
 struct BlockStateMachine {
+    #if false
+    var blockState: BlockState = .start {
+        didSet {
+            print("\(oldValue) -> \(blockState)")
+        }
+    }
+    #else
     var blockState: BlockState = .start
+    #endif
     let blockBreak: String
     let lineBreak: String
     let listIndentForContentOutsideItem: String
@@ -70,7 +78,8 @@ extension BlockStateMachine {
             blockState = .preformattedEmptyBlock(depth: depth)
         case .afterPreStartTagWithLeadingWhitespace(let depth):
             blockState = .preformattedEmptyBlockWithLeadingWhitespace(depth: depth)
-        case .preformattedNonEmptyBlockWithTrailingWhitespace(depth: _):
+        case .preformattedNonEmptyBlockWithTrailingWhitespace(let depth):
+            blockState = .preformattedEmptyBlockWithLeadingWhitespace(depth: depth)
             temporaryBuffer.append(blockBreak)
         case .preformattedEmptyBlockWithLeadingWhitespace(depth: _):
             break
diff --git a/Tests/HTMLStreamerTests/AttributedStringConverterTests.swift b/Tests/HTMLStreamerTests/AttributedStringConverterTests.swift
index 0fef3ce..87f1856 100644
--- a/Tests/HTMLStreamerTests/AttributedStringConverterTests.swift
+++ b/Tests/HTMLStreamerTests/AttributedStringConverterTests.swift
@@ -687,5 +687,13 @@ final class AttributedStringConverterTests: XCTestCase {
         XCTAssertEqual(convert("

a


b

c

"), result2) } + func testWhitespaceAtEndOfPre() { + let result = NSAttributedString(string: "a", attributes: [ + .font: monospaceFont, + .paragraphStyle: NSParagraphStyle.default, + .foregroundColor: color, + ]) + XCTAssertEqual(convert("
a 
"), result) + } }