diff --git a/Sources/Plot/Internal/ElementRenderingBuffer.swift b/Sources/Plot/Internal/ElementRenderingBuffer.swift index 19bcd4f..6dc2ad1 100644 --- a/Sources/Plot/Internal/ElementRenderingBuffer.swift +++ b/Sources/Plot/Internal/ElementRenderingBuffer.swift @@ -35,8 +35,8 @@ internal final class ElementRenderingBuffer { } } - func add(_ text: String) { - if indentation != nil { + func add(_ text: String, isPlainText: Bool) { + if !isPlainText, indentation != nil { body.append("\n") } diff --git a/Sources/Plot/Internal/Renderer.swift b/Sources/Plot/Internal/Renderer.swift index abe193f..26fc906 100644 --- a/Sources/Plot/Internal/Renderer.swift +++ b/Sources/Plot/Internal/Renderer.swift @@ -12,6 +12,7 @@ internal struct Renderer { private var environment: Environment private var elementWrapper: ElementWrapper? private var elementBuffer: ElementRenderingBuffer? + private var containsElement = false } extension Renderer { @@ -30,7 +31,7 @@ extension Renderer { } mutating func renderRawText(_ text: String) { - renderRawText(text, wrapIfNeeded: true) + renderRawText(text, isPlainText: true, wrapIfNeeded: true) } mutating func renderText(_ text: String) { @@ -72,8 +73,13 @@ extension Renderer { } deferredAttributes.forEach(buffer.add) - renderRawText(buffer.flush(), wrapIfNeeded: false) elementBuffer?.containsChildElements = true + containsElement = true + + renderRawText(buffer.flush(), + isPlainText: false, + wrapIfNeeded: false + ) } mutating func renderAttribute(_ attribute: Attribute) { @@ -119,12 +125,21 @@ extension Renderer { ) } - renderRawText(renderer.result, wrapIfNeeded: false) + renderRawText(renderer.result, + isPlainText: !renderer.containsElement, + wrapIfNeeded: false + ) + + containsElement = renderer.containsElement } } private extension Renderer { - mutating func renderRawText(_ text: String, wrapIfNeeded: Bool) { + mutating func renderRawText( + _ text: String, + isPlainText: Bool, + wrapIfNeeded: Bool + ) { if wrapIfNeeded { if let wrapper = elementWrapper { return renderComponent(wrapper.body(Node.raw(text))) @@ -132,7 +147,7 @@ private extension Renderer { } if let elementBuffer = elementBuffer { - elementBuffer.add(text) + elementBuffer.add(text, isPlainText: isPlainText) } else { if indentation != nil && !result.isEmpty { result.append("\n") diff --git a/Tests/PlotTests/DocumentTests.swift b/Tests/PlotTests/DocumentTests.swift index 824ddcd..d1dd351 100644 --- a/Tests/PlotTests/DocumentTests.swift +++ b/Tests/PlotTests/DocumentTests.swift @@ -26,9 +26,16 @@ final class DocumentTests: XCTestCase { .element(named: "two", nodes: [ .selfClosedElement(named: "three") ]), - .element(named: "four") + .text("four "), + .component(Text("five")), + .component(Element.named("six", nodes: [ + .text("seven") + ])), + .element(named: "eight", nodes: [ + .text("nine") + ]) ]), - .selfClosed(named: "five", attributes: [ + .selfClosed(named: "ten", attributes: [ Attribute(name: "key", value: "value") ]) ] @@ -38,10 +45,11 @@ final class DocumentTests: XCTestCase { - - + four five + seven + nine - + """) }