Fix issue with indented partials (#19)
* Fix issue with indented partials * Add test * Add empty transform for string
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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":
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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?
|
||||
}
|
||||
@@ -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?
|
||||
|
||||
Reference in New Issue
Block a user