Permalink
Browse files

Merge pull request #59 from jairodemorais/relaxWs

ws relaxed to allow eol
  • Loading branch information...
2 parents 05bddbe + 9b9a754 commit 5ee10f9e61d3c05b7262e9c54fa3ddf9e8d0d717 @vybs vybs committed Jun 11, 2012
Showing with 184 additions and 15 deletions.
  1. +82 −7 dist/dust-core-0.4.0.js
  2. +86 −7 dist/dust-full-0.4.0.js
  3. +3 −0 lib/parser.js
  4. +1 −1 src/dust.pegjs
  5. +12 −0 test/jasmine-test/spec/examples.js
@@ -540,7 +540,54 @@ if (typeof exports !== "undefined") {
module.exports = dust;
}
(function(dust){
+
+function isSelect(context) {
+ var value = context.current();
+ return typeof value === "object" && value.isSelect === true;
+}
+
+function filter(chunk, context, bodies, params, filter) {
+ var params = params || {},
+ actual, expected;
+ if (params.key) {
+ actual = context.get(params.key);
+ } else if (isSelect(context)) {
+ actual = context.current().value;
+ if (context.current().isResolved) {
+ filter = function() { return false; };
+ }
+ } else {
+ throw "No key specified for filter and no key found in context from select statement";
+ }
+
+ expected = params.value;
+ if (filter(expected, coerce(actual, params.type, context))) {
+ if (isSelect(context)) {
+ context.current().isResolved = true;
+ }
+ return chunk.render(bodies.block, context);
+ } else if (bodies['else']) {
+ return chunk.render(bodies['else'], context);
+ }
+
+ return chunk.write('');
+}
+
+function coerce (value, type, context) {
+ if (value) {
+ switch (type || typeof(value)) {
+ case 'number': return +value;
+ case 'string': return String(value);
+ case 'boolean': return Boolean(value);
+ case 'date': return new Date(value);
+ case 'context': return context.get(value);
+ }
+ }
+
+ return value;
+}
+
var helpers = {
sep: function(chunk, context, bodies) {
@@ -553,11 +600,11 @@ var helpers = {
idx: function(chunk, context, bodies) {
return bodies.block(chunk, context.push(context.stack.index));
},
-
+
"if": function( chunk, context, bodies, params ){
- var cond = ( params.cond );
-
if( params && params.cond ){
+ var cond = params.cond;
+
// resolve dust references in the expression
if( typeof cond === "function" ){
cond = '';
@@ -572,21 +619,49 @@ var helpers = {
// eval expressions with no dust references
if( eval( cond ) ){
return chunk.render( bodies.block, context );
- }
+ }
if( bodies['else'] ){
return chunk.render( bodies['else'], context );
}
- }
+ }
// no condition
else {
- if( window.console ){
+ if( typeof window !== 'undefined' && window.console ){
window.console.log( "No expression given!" );
}
}
return chunk;
+ },
+
+ select: function(chunk, context, bodies, params) {
+ return chunk.render(bodies.block, context.push({ isSelect: true, isResolved: false, value: context.get(params.key) }));
+ },
+
+ eq: function(chunk, context, bodies, params) {
+ return filter(chunk, context, bodies, params, function(expected, actual) { return actual === expected; });
+ },
+
+ lt: function(chunk, context, bodies, params) {
+ return filter(chunk, context, bodies, params, function(expected, actual) { return actual < expected; });
+ },
+
+ lte: function(chunk, context, bodies, params) {
+ return filter(chunk, context, bodies, params, function(expected, actual) { return actual <= expected; });
+ },
+
+ gt: function(chunk, context, bodies, params) {
+ return filter(chunk, context, bodies, params, function(expected, actual) { return actual > expected; });
+ },
+
+ gte: function(chunk, context, bodies, params) {
+ return filter(chunk, context, bodies, params, function(expected, actual) { return actual >= expected; });
+ },
+
+ "else": function(chunk, context, bodies, params) {
+ return filter(chunk, context, bodies, params, function(expected, actual) { return true; });
}
};
dust.helpers = helpers;
-})(typeof exports !== 'undefined' ? exports : getGlobal());
+})(typeof exports !== 'undefined' ? exports : getGlobal());
@@ -540,7 +540,54 @@ if (typeof exports !== "undefined") {
module.exports = dust;
}
(function(dust){
+
+function isSelect(context) {
+ var value = context.current();
+ return typeof value === "object" && value.isSelect === true;
+}
+
+function filter(chunk, context, bodies, params, filter) {
+ var params = params || {},
+ actual, expected;
+ if (params.key) {
+ actual = context.get(params.key);
+ } else if (isSelect(context)) {
+ actual = context.current().value;
+ if (context.current().isResolved) {
+ filter = function() { return false; };
+ }
+ } else {
+ throw "No key specified for filter and no key found in context from select statement";
+ }
+
+ expected = params.value;
+ if (filter(expected, coerce(actual, params.type, context))) {
+ if (isSelect(context)) {
+ context.current().isResolved = true;
+ }
+ return chunk.render(bodies.block, context);
+ } else if (bodies['else']) {
+ return chunk.render(bodies['else'], context);
+ }
+
+ return chunk.write('');
+}
+
+function coerce (value, type, context) {
+ if (value) {
+ switch (type || typeof(value)) {
+ case 'number': return +value;
+ case 'string': return String(value);
+ case 'boolean': return Boolean(value);
+ case 'date': return new Date(value);
+ case 'context': return context.get(value);
+ }
+ }
+
+ return value;
+}
+
var helpers = {
sep: function(chunk, context, bodies) {
@@ -553,11 +600,11 @@ var helpers = {
idx: function(chunk, context, bodies) {
return bodies.block(chunk, context.push(context.stack.index));
},
-
+
"if": function( chunk, context, bodies, params ){
- var cond = ( params.cond );
-
if( params && params.cond ){
+ var cond = params.cond;
+
// resolve dust references in the expression
if( typeof cond === "function" ){
cond = '';
@@ -572,24 +619,53 @@ var helpers = {
// eval expressions with no dust references
if( eval( cond ) ){
return chunk.render( bodies.block, context );
- }
+ }
if( bodies['else'] ){
return chunk.render( bodies['else'], context );
}
- }
+ }
// no condition
else {
- if( window.console ){
+ if( typeof window !== 'undefined' && window.console ){
window.console.log( "No expression given!" );
}
}
return chunk;
+ },
+
+ select: function(chunk, context, bodies, params) {
+ return chunk.render(bodies.block, context.push({ isSelect: true, isResolved: false, value: context.get(params.key) }));
+ },
+
+ eq: function(chunk, context, bodies, params) {
+ return filter(chunk, context, bodies, params, function(expected, actual) { return actual === expected; });
+ },
+
+ lt: function(chunk, context, bodies, params) {
+ return filter(chunk, context, bodies, params, function(expected, actual) { return actual < expected; });
+ },
+
+ lte: function(chunk, context, bodies, params) {
+ return filter(chunk, context, bodies, params, function(expected, actual) { return actual <= expected; });
+ },
+
+ gt: function(chunk, context, bodies, params) {
+ return filter(chunk, context, bodies, params, function(expected, actual) { return actual > expected; });
+ },
+
+ gte: function(chunk, context, bodies, params) {
+ return filter(chunk, context, bodies, params, function(expected, actual) { return actual >= expected; });
+ },
+
+ "else": function(chunk, context, bodies, params) {
+ return filter(chunk, context, bodies, params, function(expected, actual) { return true; });
}
};
dust.helpers = helpers;
-})(typeof exports !== 'undefined' ? exports : getGlobal());(function(dust) {
+})(typeof exports !== 'undefined' ? exports : getGlobal());
+(function(dust) {
dust.compile = function(source, name) {
try {
@@ -3368,6 +3444,9 @@ var parser = (function(){
matchFailed("[\\t\\x0B\\f \\xA0\\uFEFF]");
}
}
+ if (result0 === null) {
+ result0 = parse_eol();
+ }
return result0;
}
View
@@ -2450,6 +2450,9 @@ var parser = (function(){
matchFailed("[\\t\\x0B\\f \\xA0\\uFEFF]");
}
}
+ if (result0 === null) {
+ result0 = parse_eol();
+ }
return result0;
}
View
@@ -222,4 +222,4 @@ eol
Match one whitespace
---------------------------------------------------------------------------------------------------------------------------------------*/
ws
- = [\t\v\f \u00A0\uFEFF]
+ = [\t\v\f \u00A0\uFEFF] / eol
@@ -645,6 +645,18 @@ var dustExamples = [
context: { foo: "foo" },
expected: "",
message: "Select helper works correctly with no matching conditions"
+ },
+ {
+ name: "ws updated to allow eol",
+ source: ['{#authors ',
+ 'name="theAuthors"',
+ 'lastname="authorlastname" ',
+ 'maxtext=300}',
+ '{>"otherTemplate"/}',
+ '{/authors}',].join("\n"),
+ context: {},
+ expected: "",
+ message: "Should ignore carriage return or tab"
}
];

0 comments on commit 5ee10f9

Please sign in to comment.