Skip to content

Commit

Permalink
Fix issue with indented partials (#19)
Browse files Browse the repository at this point in the history
* Fix issue with indented partials

* Add test

* Add empty transform for string
  • Loading branch information
adam-fowler authored Jan 14, 2023
1 parent 185004f commit fa56176
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
8 changes: 5 additions & 3 deletions Sources/HummingbirdMustache/Template+Render.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ extension HBMustacheTemplate {

if let indentation = context.indentation, indentation != "" {
for token in tokens {
if string.last == "\n" {
let renderedString = self.renderToken(token, context: &context)
if renderedString != "", string.last == "\n" {
string += indentation
}
string += self.renderToken(token, context: &context)
string += renderedString
}
} else {
for token in tokens {
string += self.renderToken(token, context: &context)
let result = self.renderToken(token, context: &context)
string += result
}
}
return string
Expand Down
2 changes: 2 additions & 0 deletions Sources/HummingbirdMustache/Transform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public extension StringProtocol {
/// - Returns: Result
func transform(_ name: String) -> Any? {
switch name {
case "empty":
return isEmpty
case "capitalized":
return capitalized
case "lowercased":
Expand Down
35 changes: 35 additions & 0 deletions Tests/HummingbirdMustacheTests/PartialTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,41 @@ final class PartialTests: XCTestCase {
""")
}

/// Test where last line of partial generates no content. It should not add a
/// tab either
func testPartialEmptyLineTabbing() throws {
let library = HBMustacheLibrary()
let template = try HBMustacheTemplate(string: """
<h2>Names</h2>
{{#names}}
{{> user}}
{{/names}}
Text after
""")
let template2 = try HBMustacheTemplate(string: """
{{^empty(.)}}
<strong>{{.}}</strong>
{{/empty(.)}}
{{#empty(.)}}
<strong>empty</strong>
{{/empty(.)}}
""")
library.register(template, named: "base")
library.register(template2, named: "user")

let object: [String: Any] = ["names": ["john", "adam", "claire"]]
XCTAssertEqual(library.render(object, withTemplate: "base"), """
<h2>Names</h2>
<strong>john</strong>
<strong>adam</strong>
<strong>claire</strong>
Text after
""")
}

/// Testing dynamic partials
func testDynamicPartials() throws {
let library = HBMustacheLibrary()
Expand Down
18 changes: 17 additions & 1 deletion Tests/HummingbirdMustacheTests/TemplateRendererTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ final class TemplateRendererTests: XCTestCase {
XCTAssertEqual(template.render(Test(string: nil)), "test ")
}

func testOptionalSequence() throws {
func testOptionalSection() throws {
struct Test {
let string: String?
}
Expand All @@ -94,6 +94,22 @@ final class TemplateRendererTests: XCTestCase {
XCTAssertEqual(template2.render(Test(string: nil)), "test *")
}

func testOptionalSequence() throws {
struct Test {
let string: String?
}
let template = try HBMustacheTemplate(string: "test {{#.}}{{string}}{{/.}}")
XCTAssertEqual(template.render([Test(string: "string")]), "test string")
}

func testOptionalSequenceSection() throws {
struct Test {
let string: String?
}
let template = try HBMustacheTemplate(string: "test {{#.}}{{#string}}*{{.}}{{/string}}{{/.}}")
XCTAssertEqual(template.render([Test(string: "string")]), "test *string")
}

func testStructureInStructure() throws {
struct SubTest {
let string: String?
Expand Down

0 comments on commit fa56176

Please sign in to comment.