Skip to content

Commit

Permalink
Fix text rendering regression for indented documents (#65)
Browse files Browse the repository at this point in the history
Plot 0.9.0 introduced a minor regression for documents that are rendered with indentation,
in that plain text nodes would always be rendered on a new line. This patch fixes that by
differentiating between plain text output and element-based output, and only the latter
causes a new line to be inserted, preserving the previous rendering behavior.

Components that are made up of just plain text also get this behavior, and the unit test
that verifies indented documents has been expanded to also cover these cases.
  • Loading branch information
JohnSundell authored May 19, 2021
1 parent cd60159 commit c0bdc94
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Sources/Plot/Internal/ElementRenderingBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
25 changes: 20 additions & 5 deletions Sources/Plot/Internal/Renderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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<T>(_ attribute: Attribute<T>) {
Expand Down Expand Up @@ -119,20 +125,29 @@ 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<Any>.raw(text)))
}
}

if let elementBuffer = elementBuffer {
elementBuffer.add(text)
elementBuffer.add(text, isPlainText: isPlainText)
} else {
if indentation != nil && !result.isEmpty {
result.append("\n")
Expand Down
18 changes: 13 additions & 5 deletions Tests/PlotTests/DocumentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
])
]
Expand All @@ -38,10 +45,11 @@ final class DocumentTests: XCTestCase {
<one>
<two>
<three/>
</two>
<four></four>
</two>four five
<six>seven</six>
<eight>nine</eight>
</one>
<five key="value"/>
<ten key="value"/>
""")
}

Expand Down

0 comments on commit c0bdc94

Please sign in to comment.