Permalink
Browse files

'null' now renders to the empty string.

Fixes for 87 and 89:
Fixes for nested parens in expressions, such as ((a) + b)
  • Loading branch information...
1 parent efdb2cb commit 2eb692aa8b8338bb9f6ea76a7ea34605cd209051 @BorisMoore committed Mar 20, 2012
Showing with 31 additions and 25 deletions.
  1. +30 −24 jsrender.js
  2. +1 −1 test/unit/jsrender-tests-no-jquery.js
View
@@ -6,7 +6,7 @@
* Copyright 2012, Boris Moore
* Released under the MIT License.
*/
-// informal pre beta commit counter: 0
+// informal pre beta commit counter: 1
this.jsviews || this.jQuery && jQuery.views || (function( window, undefined ) {
@@ -19,11 +19,11 @@ var versionNumber = "v1.0pre",
FALSE = false, TRUE = true,
jQuery = window.jQuery,
- rPath = /^(?:(true|false|null|\d[\d.]*)|([\w$]+|~([\w$]+)|#(view|([\w$]+))?)([\w$.]*?)(?:[.[]([\w$]+)\]?)?|(['"]).*\8)$/g,
- // val object helper view viewProperty pathTokens leafToken string
+ rPath = /^(?:true|false|\d[\d.]*|(null)|([\w$]+|~([\w$]+)|#(view|([\w$]+))?)([\w$.]*?)(?:[.[]([\w$]+)\]?)?|(['"]).*\8)$/g,
+ // nil object helper view viewProperty pathTokens leafToken string
- rParams = /(?:([([])\s*)?(?:([#~]?[\w$.]+)?\s*((\+\+|--)|\+|-|&&|\|\||===|!==|==|!=|<=|>=|[<>%*!:?\/]|(=))\s*|([#~]?[\w$.]+)([([])?)|(,\s*)|(\(?)\\?(?:(')|("))|(?:\s*([)\]])([([]?))|(\s+)/g,
- // lftPrn path operator err eq path2 prn comma lftPrn2 apos quot rtPrn prn2 space
+ rParams = /(\()(?=|\s*\()|(?:([([])\s*)?(?:([#~]?[\w$.]+)?\s*((\+\+|--)|\+|-|&&|\|\||===|!==|==|!=|<=|>=|[<>%*!:?\/]|(=))\s*|([#~]?[\w$.]+)([([])?)|(,\s*)|(\(?)\\?(?:(')|("))|(?:\s*([)\]])([([]?))|(\s+)/g,
+ // lftPrn lftPrn2 path operator err eq path2 prn comma lftPrn2 apos quot rtPrn prn2 space
// (left paren? followed by (path? followed by operator) or (path followed by paren?)) or comma or apos or quot or right paren or space
rNewLine = /\r?\n/g,
@@ -362,7 +362,7 @@ function syntaxError() {
function tmplFn( markup, tmpl, bind ) {
// Compile markup to AST (abtract syntax tree) then build the template function code from the AST nodes
// Used for compiling templates, and also by JsViews to build functions for data link expressions
- var newNode, node, i, l, code, hasTag, hasEncoder, hasHelperPath, getsValue, hasConverter, hasViewPath, tag, converter, params, hash, nestedTmpl, allowCode,
+ var newNode, node, i, l, code, hasTag, hasEncoder, getsValue, hasConverter, hasViewPath, tag, converter, params, hash, nestedTmpl, allowCode,
tmplOptions = tmpl ? {
allowCode: allowCode = tmpl.allowCode,
debug: tmpl.debug
@@ -483,8 +483,7 @@ function tmplFn( markup, tmpl, bind ) {
tmplFn( markup, nestedTmpl);
nested.push( nestedTmpl );
}
- hasHelperPath = hasHelperPath || params.indexOf( 'view.hlp("' ) > -1;
- hasViewPath = hasViewPath || params.indexOf( "view" ) > -1;
+ hasViewPath = hasViewPath || hash.indexOf( "view" ) > -1;
code += (tag === ":"
? (converter === "html"
? (hasEncoder = TRUE, "e(" + params)
@@ -514,7 +513,7 @@ function tmplFn( markup, tmpl, bind ) {
// Include only the var references that are needed in the code
if ( tmpl ) {
tmpl.fn = code;
- tmpl.useVw = hasConverter || hasHelperPath || hasViewPath || hasTag;
+ tmpl.useVw = hasConverter || hasViewPath || hasTag;
}
return code;
}
@@ -526,19 +525,21 @@ function parseParams( params, bind ) {
quoted = FALSE, // boolean for string content in double quotes
aposed = FALSE; // or in single quotes
- function parseTokens( all, lftPrn, path, operator, err, eq, path2, prn, comma, lftPrn2, apos, quot, rtPrn, prn2, space ) {
+ function parseTokens( all, lftPrn0, lftPrn, path, operator, err, eq, path2, prn, comma, lftPrn2, apos, quot, rtPrn, prn2, space ) {
// rParams = /(?:([([])\s*)?(?:([#~]?[\w$.]+)?\s*((\+\+|--)|\+|-|&&|\|\||===|!==|==|!=|<=|>=|[<>%*!:?\/]|(=))\s*|([#~]?[\w$.^]+)([([])?)|(,\s*)|(\(?)\\?(?:(')|("))|(?:\s*([)\]])([([]?))|(\s+)/g,
- // lftPrn path operator err eq path2 prn comma lftPrn2 apos quot rtPrn prn2 space
+ // lftPrn path operator err eq path2 prn comma lftPrn3 apos quot rtPrn prn2 space
// (left paren? followed by (path? followed by operator) or (path followed by paren?)) or comma or apos or quot or right paren or space
+ operator = operator || "";
+ lftPrn = lftPrn || lftPrn0 || lftPrn2;
path = path || path2;
prn = prn || prn2 || "";
operator = operator || "";
- function parsePath( all, val, object, helper, view, viewProperty, pathTokens, leafToken ) {
- // rPath = /^(?:(true|false|null|\d[\d.]*)|([\w$]+|~([\w$]+)|#(view|data|([\w$]+))?)([\w$.]*[.[])?([\w$]*\]?)?|['"].*\9)$/g,
- // val object helper view viewProperty pathTokens leafToken string quot
+ function parsePath( all, nil, object, helper, view, viewProperty, pathTokens, leafToken ) {
+ // rPath = /^(?:true|false|\d[\d.]*|(null)|([\w$]+|~([\w$]+)|#(view|([\w$]+))?)([\w$.]*?)(?:[.[]([\w$]+)\]?)?|(['"]).*\8)$/g,
+ // nil object helper view viewProperty pathTokens leafToken string
if ( object ) {
- val = (helper
+ var ret = (helper
? 'view.hlp("' + helper + '")'
: view
? "view"
@@ -553,26 +554,28 @@ function parseParams( params, bind ) {
: (leafToken = helper ? "" : view ? viewProperty || "" : object, ""));
if ( bind && prn !== "(" ) {
- val = "b(" + val + ',"' + leafToken + '")';
+ ret = "b(" + ret + ',"' + leafToken + '")';
}
- val += leafToken ? "." + leafToken : "";
+ return ret + (leafToken ? "." + leafToken : "");
}
- return val || all;
+ return nil ? "u" : all;
}
if ( err ) {
syntaxError();
} else {
- return ((lftPrn = lftPrn || lftPrn2)
- ? (parenDepth++, lftPrn)
- : "")
- + (aposed
+ return (aposed
// within single-quoted string
? (aposed = !apos, (aposed ? all : '"'))
: quoted
// within double-quoted string
? (quoted = !quot, (quoted ? all : '"'))
- : space
+ :
+ (
+ (lftPrn
+ ? (parenDepth++, lftPrn)
+ : "")
+ + (space
? (parenDepth
? ""
: named
@@ -600,7 +603,10 @@ function parseParams( params, bind ) {
)
: comma
? (fnCall[ parenDepth ] || syntaxError(), ",") // We don't allow top-level literal arrays or objects
- : (aposed = apos, quoted = quot, '"')
+ : lftPrn0
+ ? ""
+ : (aposed = apos, quoted = quot, '"')
+ ))
);
}
}
@@ -104,7 +104,7 @@ test("types", function() {
equal( jsviews.templates( "{{:'abc'}}" ).render({}), "abc", "'abc'" );
equal( jsviews.templates( "{{:true}}" ).render({}), "true", "true" );
equal( jsviews.templates( "{{:false}}" ).render({}), "false", "false" );
- equal( jsviews.templates( "{{:null}}" ).render({}), "null", "null" );
+ equal( jsviews.templates( "{{:null}}" ).render({}), "", 'null -> ""' );
equal( jsviews.templates( "{{:199}}" ).render({}), "199", "199" );
equal( jsviews.templates( "{{: 199.9 }}" ).render({}), "199.9", "| 199.9 |" );
equal( jsviews.templates( "{{:-33.33}}" ).render({}), "-33.33", "-33.33" );

0 comments on commit 2eb692a

Please sign in to comment.