From 637066f6260c71dda3c94970616e673c44ab11ca Mon Sep 17 00:00:00 2001 From: Jairo de Morais Date: Thu, 28 Jun 2012 13:01:39 -0300 Subject: [PATCH] fix issue https://github.com/linkedin/dustjs/issues/68 --- dist/dust-core-0.6.0.js | 19 +++++++++++++++---- dist/dust-full-0.6.0.js | 25 +++++++++++++++++++++---- lib/parser.js | 6 ++++++ src/dust.pegjs | 2 +- test/jasmine-test/spec/examples.js | 7 +++++++ 5 files changed, 50 insertions(+), 9 deletions(-) diff --git a/dist/dust-core-0.6.0.js b/dist/dust-core-0.6.0.js index dec1ebaf..a1ccd38d 100644 --- a/dist/dust-core-0.6.0.js +++ b/dist/dust-core-0.6.0.js @@ -541,6 +541,16 @@ if (typeof exports !== "undefined") { } (function(dust){ +/* make a safe version of console if it is not available + * currently supporting: + * _console.log + * */ +var _console = (typeof console !== 'undefined')? console: { + log: function(){ + /* a noop*/ + } +}; + function isSelect(context) { var value = context.current(); return typeof value === "object" && value.isSelect === true; @@ -600,7 +610,10 @@ var helpers = { idx: function(chunk, context, bodies) { return bodies.block(chunk, context.push(context.stack.index)); }, - + contextDump: function(chunk, context, bodies) { + _console.log(JSON.stringify(context.stack)); + return chunk; + }, "if": function( chunk, context, bodies, params ){ if( params && params.cond ){ var cond = params.cond; @@ -626,9 +639,7 @@ var helpers = { } // no condition else { - if( typeof window !== 'undefined' && window.console ){ - window.console.log( "No expression given!" ); - } + _console.log( "No expression given!" ); } return chunk; }, diff --git a/dist/dust-full-0.6.0.js b/dist/dust-full-0.6.0.js index 94c7f0ba..dc33f63a 100644 --- a/dist/dust-full-0.6.0.js +++ b/dist/dust-full-0.6.0.js @@ -541,6 +541,16 @@ if (typeof exports !== "undefined") { } (function(dust){ +/* make a safe version of console if it is not available + * currently supporting: + * _console.log + * */ +var _console = (typeof console !== 'undefined')? console: { + log: function(){ + /* a noop*/ + } +}; + function isSelect(context) { var value = context.current(); return typeof value === "object" && value.isSelect === true; @@ -600,7 +610,10 @@ var helpers = { idx: function(chunk, context, bodies) { return bodies.block(chunk, context.push(context.stack.index)); }, - + contextDump: function(chunk, context, bodies) { + _console.log(JSON.stringify(context.stack)); + return chunk; + }, "if": function( chunk, context, bodies, params ){ if( params && params.cond ){ var cond = params.cond; @@ -626,9 +639,7 @@ var helpers = { } // no condition else { - if( typeof window !== 'undefined' && window.console ){ - window.console.log( "No expression given!" ); - } + _console.log( "No expression given!" ); } return chunk; }, @@ -2187,11 +2198,17 @@ var parser = (function(){ result0 = result0 !== null ? result0 : ""; if (result0 !== null) { result2 = parse_nestedKey(); + if (result2 === null) { + result2 = parse_array(); + } if (result2 !== null) { result1 = []; while (result2 !== null) { result1.push(result2); result2 = parse_nestedKey(); + if (result2 === null) { + result2 = parse_array(); + } } } else { result1 = null; diff --git a/lib/parser.js b/lib/parser.js index 6d3db72b..ae9feb15 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -1193,11 +1193,17 @@ var parser = (function(){ result0 = result0 !== null ? result0 : ""; if (result0 !== null) { result2 = parse_nestedKey(); + if (result2 === null) { + result2 = parse_array(); + } if (result2 !== null) { result1 = []; while (result2 !== null) { result1.push(result2); result2 = parse_nestedKey(); + if (result2 === null) { + result2 = parse_array(); + } } } else { result1 = null; diff --git a/src/dust.pegjs b/src/dust.pegjs index a380975f..863bc715 100644 --- a/src/dust.pegjs +++ b/src/dust.pegjs @@ -121,7 +121,7 @@ integer "integer" Match anything that match with key plus one or more characters that match with key again but preceded by a dot "." ---------------------------------------------------------------------------------------------------------------------------------------*/ path "path" - = k:key? d:(nestedKey)+ { + = k:key? d:(nestedKey / array)+ { d = d[0]; if (k && d) { d.unshift(k); diff --git a/test/jasmine-test/spec/examples.js b/test/jasmine-test/spec/examples.js index e1042d80..5a818412 100644 --- a/test/jasmine-test/spec/examples.js +++ b/test/jasmine-test/spec/examples.js @@ -505,6 +505,13 @@ var dustExamples = [ expected: "hello!", message: "should return a specific array element by index. Most Complex case, the array contains nested objects." }, + { + name: "Accessing array by index", + source: '{do[0]}', + context: { "do": ["lala", "lele"] }, + expected: "lala", + message: "should return a specific array element by index. Root context object as array." + }, { name: "params: integer", source: "{#helper foo=10 /}",