Permalink
Browse files

Fix the array reference access with GH-148

  • Loading branch information...
1 parent 2f4462d commit 9da56388b2084af925b99d4cc632b808dbb4de77 @jairodemorais jairodemorais committed Sep 17, 2012
Showing with 72 additions and 23 deletions.
  1. +4 −1 lib/compiler.js
  2. +46 −20 lib/parser.js
  3. +7 −1 src/dust.pegjs
  4. +15 −1 test/jasmine-test/spec/coreTests.js
View
@@ -309,7 +309,10 @@ dust.nodes = {
list = [];
for (var i=0,len=keys.length; i<len; i++) {
- list.push("\"" + keys[i] + "\"");
+ if (Array.isArray(keys[i]))
+ list.push(dust.compileNode(context, keys[i]));
+ else
+ list.push("\"" + keys[i] + "\"");
}
return "ctx.getPath(" + current + ",[" + list.join(',') + "])";
},
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -132,7 +132,7 @@ key "key"
{ return h + t.join('') }
array "array"
- = i:("[" a:([0-9]+) "]" {return a.join('')}) nk: array_part? { if(nk) { nk.unshift(i); } else {nk = [i] } return nk; }
+ = i:( lb a:( n:([0-9]+) {return n.join('')} / identifier) rb {return a; }) nk: array_part? { if(nk) { nk.unshift(i); } else {nk = [i] } return nk; }
array_part "array_part"
= d:("." k:key {return k})+ a:(array)? { if (a) { return d.concat(a); } else { return d; } }
@@ -186,6 +186,12 @@ ld
rd
= "}"
+lb
+ = "["
+
+rb
+ = "]"
+
eol
= "\n" //line feed
/ "\r\n" //carriage + line feed
@@ -997,7 +997,21 @@ var coreTests = [
source: "{^xhr-n}tag not found!{:else}tag found!{/xhr-n}",
context: {"xhr": false},
expected: "tag not found!",
- message: "should test child template with dash in the reference"
+ message: "should test child template with dash"
+ },
+ {
+ name: "using idx in array reference Accessing",
+ source: "{#list4} {name} {number[$idx]} {$idx}{/list4}",
+ context: { list4: [ {name:"Dog", number: [1,2,3]}, {name:"Cat", number: [4,5,6]}] },
+ expected: " Dog 1 0 Cat 5 1",
+ message: "should test the array reference access with idx"
+ },
+ {
+ name: "using idx in array reference Accessing",
+ source: "{#list3}{.[$idx].biz}{/list3}",
+ context: { "list3": [[ { "biz" : "123" } ], [ { "biz" : "345" }, { "biz" : "456" } ]]},
+ expected: "123456",
+ message: "should test the array reference access with idx and current context"
}
];

0 comments on commit 9da5638

Please sign in to comment.