velocityjs
Advanced tools
Comparing version 0.3.8 to 0.3.9
{ | ||
"name": "velocityjs", | ||
"description": "Velocity Template Language(VTL) for JavaScript", | ||
"version": "0.3.8", | ||
"version": "0.3.9", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "velocity template" |
@@ -205,4 +205,4 @@ module.exports = function(Velocity, utils){ | ||
this.local[contextId] = {}; | ||
this.conditions.pop(); | ||
this.condition = ''; | ||
this.conditions.shift(); | ||
this.condition = this.conditions[0] || ''; | ||
@@ -209,0 +209,0 @@ return ret; |
@@ -200,3 +200,9 @@ module.exports = function(Velocity, utils){ | ||
if (ret && ret.call) { | ||
ret = ret.apply(this, args); | ||
var that = this; | ||
baseRef.eval = function() { | ||
return that.eval.apply(that, arguments); | ||
} | ||
ret = ret.apply(baseRef, args); | ||
} else { | ||
@@ -203,0 +209,0 @@ ret = undefined; |
@@ -24,3 +24,4 @@ module.exports = function(Velocity, utils){ | ||
if (ast.path) { | ||
// 排除get key value size等通用方法 | ||
if (ast.path && !(~['get', 'key', 'value', 'size'].indexOf(ast.path[0].id))) { | ||
local.objectKeys.push(ast.path[0].id); | ||
@@ -27,0 +28,0 @@ ret.ignore = true; |
@@ -61,2 +61,15 @@ var Velocity = require('../src/velocity'); | ||
it('this context keep correct, see #16', function(){ | ||
var data = 'a = $a.get()'; | ||
function b(c) { | ||
this.c = c; | ||
} | ||
b.prototype.get = function() { | ||
var t = this.eval("; hello $name", {name: 'hanwen'}); | ||
return this.c + t; | ||
}; | ||
assert.equal('a = 1; hello hanwen', render(data, {a: new b(1)})); | ||
}); | ||
}); | ||
@@ -330,2 +343,7 @@ | ||
it('issue #15', function(){ | ||
var vm = '#macro(a $b $list)#foreach($a in $list)${a}#end $b #end #a("hello", [1, 2])'; | ||
assert.equal(' 12 hello ', render(vm)); | ||
}); | ||
it('# meet with css property', function(){ | ||
@@ -332,0 +350,0 @@ var vm = '#margin-top:2px;'; |
@@ -7,27 +7,57 @@ /** | ||
var Jsonify = require('../../src/velocity').Helper.Jsonify; | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
describe('Jsonify', function(){ | ||
function getContext(vm){ | ||
var jsonify = new Jsonify(Parser.parse(vm)); | ||
function getContext(vm, context, macros){ | ||
var jsonify = new Jsonify(Parser.parse(vm), context, macros); | ||
return jsonify.toVTL(); | ||
} | ||
function getJson(obj){ | ||
return JSON.stringify(obj, false, 2); | ||
function getVmFile(name){ | ||
name = path.join(__dirname, name + '.vm'); | ||
var jsonFile = name.replace(/\.vm$/, '-json.vm'); | ||
var readFile = fs.readFileSync; | ||
return [(readFile(name)).toString(), (readFile(jsonFile)).toString()]; | ||
} | ||
function trimStr(str){ | ||
return str.replace(/\s+/g, ''); | ||
} | ||
it('simple references', function(){ | ||
var vm = '$foo.bar'; | ||
assert.equal(getContext(vm), getJson({foo: {bar: '#jsonifyGetString($foo.bar)'} })); | ||
var strs = getVmFile('simple'); | ||
var str1 = trimStr(getContext(strs[0])); | ||
var str2 = trimStr(strs[1]); | ||
assert.equal(str1, str2); | ||
}); | ||
it('method call', function(){ | ||
var vm = '$foo.bar($user.name)'; | ||
assert.equal(getContext(vm), getJson({foo: {bar: 'Function(){}'} })); | ||
it('simple macros', function(){ | ||
var strs = getVmFile('simple-macros'); | ||
var str1 = trimStr(getContext(strs[0])); | ||
var str2 = trimStr(strs[1]); | ||
assert.equal(str1, str2); | ||
}); | ||
it('method call, return map', function(){ | ||
var vm = '$foo.bar($user.name).bar'; | ||
//console.log(getContext(vm)); | ||
it('simple foreach', function(){ | ||
var strs = getVmFile('foreach'); | ||
var str1 = trimStr(getContext(strs[0])); | ||
var str2 = trimStr(strs[1]); | ||
assert.equal(str1, str2); | ||
}); | ||
it('rewrite macros and inner api', function(){ | ||
function loadVFile(txt){ | ||
var file = __dirname + '/'+ txt; | ||
if (fs.existsSync(file)) { | ||
this.eval(fs.readFileSync(file).toString()); | ||
} | ||
} | ||
var strs = getVmFile('macros'); | ||
var str1 = trimStr(getContext(strs[0], {ctrl: {load: loadVFile}}, {parse: loadVFile})); | ||
var str2 = trimStr(strs[1]); | ||
assert.equal(str1, str2); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
407925
45
3483
7