Fix issue with indented partials (#19)

* Fix issue with indented partials

* Add test

* Add empty transform for string
This commit is contained in:
Adam Fowler
2023-01-14 08:50:51 +00:00
committed by GitHub
parent 185004fdce
commit fa56176223
4 changed files with 59 additions and 4 deletions

View File

@@ -27,14 +27,16 @@ extension HBMustacheTemplate {
if let indentation = context.indentation, indentation != "" { if let indentation = context.indentation, indentation != "" {
for token in tokens { for token in tokens {
if string.last == "\n" { let renderedString = self.renderToken(token, context: &context)
if renderedString != "", string.last == "\n" {
string += indentation string += indentation
} }
string += self.renderToken(token, context: &context) string += renderedString
} }
} else { } else {
for token in tokens { for token in tokens {
string += self.renderToken(token, context: &context) let result = self.renderToken(token, context: &context)
string += result
} }
} }
return string return string

View File

@@ -39,6 +39,8 @@ public extension StringProtocol {
/// - Returns: Result /// - Returns: Result
func transform(_ name: String) -> Any? { func transform(_ name: String) -> Any? {
switch name { switch name {
case "empty":
return isEmpty
case "capitalized": case "capitalized":
return capitalized return capitalized
case "lowercased": case "lowercased":

View File

@@ -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 /// Testing dynamic partials
func testDynamicPartials() throws { func testDynamicPartials() throws {
let library = HBMustacheLibrary() let library = HBMustacheLibrary()

View File

@@ -82,7 +82,7 @@ final class TemplateRendererTests: XCTestCase {
XCTAssertEqual(template.render(Test(string: nil)), "test ") XCTAssertEqual(template.render(Test(string: nil)), "test ")
} }
func testOptionalSequence() throws { func testOptionalSection() throws {
struct Test { struct Test {
let string: String? let string: String?
} }
@@ -94,6 +94,22 @@ final class TemplateRendererTests: XCTestCase {
XCTAssertEqual(template2.render(Test(string: nil)), "test *") 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 { func testStructureInStructure() throws {
struct SubTest { struct SubTest {
let string: String? let string: String?