From b139dd811d49d952ba0a7a35fc208187c5ffef1a Mon Sep 17 00:00:00 2001 From: Seth Kinast Date: Thu, 13 Nov 2014 17:11:28 -0800 Subject: [PATCH] Fix the use of a multi-level object key (e.g. foo.bar) as the key for an index lookup inside non-self-closing tags. Closes #521. --- lib/parser.js | 2 +- src/dust.pegjs | 2 +- test/jasmine-test/spec/coreTests.js | 12 ++++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/parser.js b/lib/parser.js index 687387c5..0b8befd4 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -107,7 +107,7 @@ peg$c42 = { type: "literal", value: "~", description: "\"~\"" }, peg$c43 = function(k) { return ["special", k].concat([['line', line()], ['col', column()]]) }, peg$c44 = { type: "other", description: "identifier" }, - peg$c45 = function(p) { var arr = ["path"].concat(p); arr.text = p[1].join('.'); return arr; }, + peg$c45 = function(p) { var arr = ["path"].concat(p); arr.text = p[1].join('.').replace(/,line,\d+,col,\d+/g,''); return arr; }, peg$c46 = function(k) { var arr = ["key", k]; arr.text = k; return arr; }, peg$c47 = { type: "other", description: "number" }, peg$c48 = function(n) { return ['literal', n]; }, diff --git a/src/dust.pegjs b/src/dust.pegjs index 5b9a0667..35cadc26 100644 --- a/src/dust.pegjs +++ b/src/dust.pegjs @@ -111,7 +111,7 @@ special "special" identifier is defined as matching a path or key ---------------------------------------------------------------------------------------------------------------------------------------*/ identifier "identifier" - = p:path { var arr = ["path"].concat(p); arr.text = p[1].join('.'); return arr; } + = p:path { var arr = ["path"].concat(p); arr.text = p[1].join('.').replace(/,line,\d+,col,\d+/g,''); return arr; } / k:key { var arr = ["key", k]; arr.text = k; return arr; } number "number" diff --git a/test/jasmine-test/spec/coreTests.js b/test/jasmine-test/spec/coreTests.js index 4bfb2c98..b36b850d 100755 --- a/test/jasmine-test/spec/coreTests.js +++ b/test/jasmine-test/spec/coreTests.js @@ -676,6 +676,18 @@ var coreTests = [ context: { "test": [[ 1,2,3 ]]}, expected: "1i:0l:3,2i:1l:3,3i:2l:3,", message: "should test double nested array and . reference: issue #340" + }, + { + name: "using a nested key as a reference for array index access", + source: "{#loop.array[key.foo].sub}{.}{/loop.array[key.foo].sub}", + context: { + "loop": { + "array": {"thing": {sub: 1, sap: 2}, "thing2": "bar"} + }, + "key": { "foo": "thing" } + }, + expected: "1", + message: "should test using a multilevel reference as a key in array access" } ] },