Stop context stack climbing with "." prefix
This commit is contained in:
@@ -138,12 +138,17 @@ extension HBMustacheTemplate {
|
|||||||
|
|
||||||
// work out which object to access. "." means the current object, if the variable name is ""
|
// work out which object to access. "." means the current object, if the variable name is ""
|
||||||
// and we have a transform to run on the variable then we need the context object, otherwise
|
// and we have a transform to run on the variable then we need the context object, otherwise
|
||||||
// the name is split by "." and we use mirror to get the correct child object
|
// the name is split by "." and we use mirror to get the correct child object. If we cannot find
|
||||||
|
// the root object we look up the context stack until we can find one with a matching name. The
|
||||||
|
// stack climbing can be disabled by prefixing the variable name with a "."
|
||||||
let child: Any?
|
let child: Any?
|
||||||
if name == "." {
|
if name == "." {
|
||||||
child = context.stack.last!
|
child = context.stack.last!
|
||||||
} else if name == "", transform != nil {
|
} else if name == "", transform != nil {
|
||||||
child = context.sequenceContext
|
child = context.sequenceContext
|
||||||
|
} else if name.first == "." {
|
||||||
|
let nameSplit = name.split(separator: ".").map { String($0) }
|
||||||
|
child = _getChild(named: nameSplit[...], from: context.stack.last!)
|
||||||
} else {
|
} else {
|
||||||
let nameSplit = name.split(separator: ".").map { String($0) }
|
let nameSplit = name.split(separator: ".").map { String($0) }
|
||||||
child = _getChildInStack(named: nameSplit[...], from: context.stack)
|
child = _getChildInStack(named: nameSplit[...], from: context.stack)
|
||||||
|
|||||||
@@ -99,6 +99,16 @@ final class TemplateRendererTests: XCTestCase {
|
|||||||
XCTAssertEqual(template2.render("<>"), "<>")
|
XCTAssertEqual(template2.render("<>"), "<>")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testStopClimbingStack() throws {
|
||||||
|
let template1 = try HBMustacheTemplate(string: "{{#test}}{{name}}{{/test}}")
|
||||||
|
let template2 = try HBMustacheTemplate(string: "{{#test}}{{.name}}{{/test}}")
|
||||||
|
let object: [String: Any] = ["test": [:], "name": "John"]
|
||||||
|
let object2: [String: Any] = ["test": ["name": "Jane"], "name": "John"]
|
||||||
|
XCTAssertEqual(template1.render(object), "John")
|
||||||
|
XCTAssertEqual(template2.render(object), "")
|
||||||
|
XCTAssertEqual(template2.render(object2), "Jane")
|
||||||
|
}
|
||||||
|
|
||||||
/// variables
|
/// variables
|
||||||
func testMustacheManualExample1() throws {
|
func testMustacheManualExample1() throws {
|
||||||
let template = try HBMustacheTemplate(string: """
|
let template = try HBMustacheTemplate(string: """
|
||||||
|
|||||||
Reference in New Issue
Block a user