velocityjs
Advanced tools
Comparing version 0.3.16 to 0.4.0
{ | ||
"name": "velocityjs", | ||
"description": "Velocity Template Language(VTL) for JavaScript", | ||
"version": "0.3.16", | ||
"version": "0.4.0", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "velocity template" |
@@ -13,2 +13,3 @@ module.exports = function(Velocity, utils){ | ||
this.silence = false; | ||
this.unescape = {}; | ||
@@ -15,0 +16,0 @@ utils.forEach(this.asts, this._init, this); |
@@ -14,3 +14,52 @@ module.exports = function(Velocity, utils){ | ||
/** | ||
* unicode转码 | ||
*/ | ||
function convert(str){ | ||
if (typeof str !== 'string') return str; | ||
var result = "" | ||
var escape = false | ||
for(i = 0 ; i < str.length ; i++) { | ||
c = str.charAt(i); | ||
if((' ' <= c && c <= '~') || (c == '\r') || (c == '\n')) { | ||
if(c == '&') { | ||
cstr = "&" | ||
escape = true | ||
} else if(c == '<') { | ||
cstr = "<" | ||
escape = true | ||
} else if(c == '>') { | ||
cstr = ">" | ||
escape = true | ||
} else { | ||
cstr = c.toString() | ||
} | ||
} else { | ||
cstr = "&#" + c.charCodeAt().toString() + ";" | ||
} | ||
result = result + cstr | ||
} | ||
return escape ? result : str | ||
} | ||
utils.mixin(Velocity.prototype, { | ||
//增加某些函数,不需要执行html转义 | ||
addIgnoreEscpape: function(key){ | ||
if (!utils.isArray(key)) key = [key] | ||
utils.forEach(key, function(key){ | ||
this.unescape[key] = true | ||
}, this) | ||
}, | ||
/** | ||
@@ -24,2 +73,6 @@ * 引用求值 | ||
if (ast.prue) { | ||
if (ast.id in this.unescape) ast.prue = false | ||
} | ||
var isSilent = this.silence || ast.leader === "$!"; | ||
@@ -32,4 +85,5 @@ var isfn = ast.args !== undefined; | ||
var text = Velocity.Helper.getRefText(ast) | ||
if (text in context) { | ||
return context[text]; | ||
return ast.prue ? convert(context[text]) : context[text]; | ||
} | ||
@@ -71,3 +125,8 @@ | ||
if (isVal && ret === undefined) ret = isSilent? '' : Velocity.Helper.getRefText(ast); | ||
if (isVal && ret === undefined) { | ||
ret = isSilent? '' : Velocity.Helper.getRefText(ast); | ||
} | ||
ret = ast.prue ? convert(ret) : ret | ||
return ret; | ||
@@ -74,0 +133,0 @@ }, |
@@ -93,3 +93,3 @@ /* parser generated by jison 0.4.10 */ | ||
break; | ||
case 5: this.$ = $$[$0]; | ||
case 5: $$[$0]['prue'] = true; this.$ = $$[$0]; | ||
break; | ||
@@ -96,0 +96,0 @@ case 6: this.$ = $$[$0]; |
@@ -84,2 +84,14 @@ var Velocity = require('../src/velocity') | ||
it('escape default', function(){ | ||
var vm = '$name $name2 $cn $cn1' | ||
var data = { | ||
name: 'hello world', | ||
name2: '<i>a', | ||
cn: '中文', | ||
cn1: '<i>中文' | ||
} | ||
var ret = 'hello world <i>a 中文 <i>中文' | ||
assert.equal(ret, render(vm, data)) | ||
}) | ||
}) | ||
@@ -86,0 +98,0 @@ |
@@ -1,3 +0,3 @@ | ||
var Parser = require('../src/velocity').Parser; | ||
var assert = require("assert"); | ||
var Parser = require('../src/velocity').Parser | ||
var assert = require("assert") | ||
@@ -9,31 +9,31 @@ describe('Parser', function(){ | ||
it('simple references', function(){ | ||
var vm = 'hello world: $foo'; | ||
var ret = Parser.parse(vm); | ||
var vm = 'hello world: $foo' | ||
var ret = Parser.parse(vm) | ||
assert.ok(ret instanceof Array); | ||
assert.equal(2 , ret.length); | ||
assert.equal('hello world: ' , ret[0]); | ||
assert.equal('foo' , ret[1].id); | ||
assert.ok(ret instanceof Array) | ||
assert.equal(2, ret.length) | ||
assert.equal('hello world: ', ret[0]) | ||
assert.equal('foo', ret[1].id) | ||
}); | ||
}) | ||
it('valid variable references', function(){ | ||
var vm = '$mud-Slinger_1'; | ||
assert.equal('mud-Slinger_1', Parser.parse(vm)[0].id); | ||
}); | ||
var vm = '$mud-Slinger_1' | ||
assert.equal('mud-Slinger_1', Parser.parse(vm)[0].id) | ||
}) | ||
it('wraped references', function(){ | ||
var vm = '${mudSlinger}'; | ||
var ast = Parser.parse(vm)[0]; | ||
assert.equal(true , ast.isWraped); | ||
assert.equal('mudSlinger' , ast.id); | ||
}); | ||
var vm = '${mudSlinger}' | ||
var ast = Parser.parse(vm)[0] | ||
assert.equal(true, ast.isWraped) | ||
assert.equal('mudSlinger', ast.id) | ||
}) | ||
it('function call references', function(){ | ||
var ast = Parser.parse('$foo()')[0]; | ||
assert.equal(false, ast.args); | ||
assert.equal('references', ast.type); | ||
}); | ||
var ast = Parser.parse('$foo()')[0] | ||
assert.equal(false, ast.args) | ||
assert.equal('references', ast.type) | ||
}) | ||
}); | ||
}) | ||
@@ -43,13 +43,14 @@ describe('Properties', function(){ | ||
it('simple property', function(){ | ||
var vm = '$customer.Address'; | ||
var asts = Parser.parse(vm); | ||
var vm = '$customer.Address' | ||
var asts = Parser.parse(vm) | ||
assert.deepEqual(asts[0], { | ||
id : "customer", | ||
type : "references", | ||
path : [{"type": "property","id": "Address"}], | ||
id : "customer", | ||
prue : true, | ||
type : "references", | ||
path : [{"type": "property","id": "Address"}], | ||
leader : '$' | ||
}); | ||
}); | ||
}) | ||
}) | ||
}); | ||
}) | ||
@@ -59,4 +60,4 @@ describe('Methods ', function(){ | ||
it('with no arguments', function(){ | ||
var vm = '$foo.bar()'; | ||
var ast = Parser.parse(vm)[0]; | ||
var vm = '$foo.bar()' | ||
var ast = Parser.parse(vm)[0] | ||
@@ -67,8 +68,8 @@ assert.deepEqual(ast['path'], [{ | ||
args : false | ||
}]); | ||
}); | ||
}]) | ||
}) | ||
it('with arguments integer', function(){ | ||
var vm = '$foo.bar(10)'; | ||
var ast = Parser.parse(vm)[0]; | ||
var vm = '$foo.bar(10)' | ||
var ast = Parser.parse(vm)[0] | ||
@@ -82,8 +83,11 @@ assert.deepEqual(ast['path'], [{ | ||
}] | ||
}]); | ||
}); | ||
}]) | ||
}) | ||
it('with arguments references', function(){ | ||
var vm = '$foo.bar($bar)'; | ||
var ast = Parser.parse(vm)[0]; | ||
var vm = '$foo.bar($bar)' | ||
var ast = Parser.parse(vm)[0] | ||
assert.equal(ast.prue, true) | ||
assert.deepEqual(ast.path[0].args, [{ | ||
@@ -93,6 +97,6 @@ type : "references", | ||
id : "bar" | ||
}]); | ||
}); | ||
}]) | ||
}) | ||
}); | ||
}) | ||
@@ -102,28 +106,28 @@ describe('Index', function(){ | ||
it('all kind of indexs', function(){ | ||
var vm = '$foo[0] $foo[$i] $foo["bar"]'; | ||
var asts = Parser.parse(vm); | ||
var vm = '$foo[0] $foo[$i] $foo["bar"]' | ||
var asts = Parser.parse(vm) | ||
assert.equal(5, asts.length); | ||
assert.equal(5, asts.length) | ||
//asts[0].path[0] => $foo[0] | ||
//{type: 'index', id: {type: 'integer', value: '0'}} | ||
assert.equal('index' , asts[0].path[0].type); | ||
assert.equal('integer' , asts[0].path[0].id.type); | ||
assert.equal('0' , asts[0].path[0].id.value); | ||
assert.equal('index', asts[0].path[0].type) | ||
assert.equal('integer', asts[0].path[0].id.type) | ||
assert.equal('0', asts[0].path[0].id.value) | ||
//asts[2].path[0] => $foo[$i] | ||
//{type: 'references', id: {type:'references', id: 'i', leader: '$'}} | ||
assert.equal('index' , asts[2].path[0].type); | ||
assert.equal('references' , asts[2].path[0].id.type); | ||
assert.equal('i' , asts[2].path[0].id.id); | ||
assert.equal('index', asts[2].path[0].type) | ||
assert.equal('references', asts[2].path[0].id.type) | ||
assert.equal('i', asts[2].path[0].id.id) | ||
//asts[4].path[0] => $foo["bar"] | ||
//{type: 'index', id: {type: 'string', value: 'bar', isEval: true}} | ||
assert.equal('index' , asts[4].path[0].type); | ||
assert.equal('string' , asts[4].path[0].id.type); | ||
assert.equal('bar' , asts[4].path[0].id.value); | ||
assert.equal('index', asts[4].path[0].type) | ||
assert.equal('string', asts[4].path[0].id.type) | ||
assert.equal('bar', asts[4].path[0].id.value) | ||
}); | ||
}) | ||
}); | ||
}) | ||
@@ -133,51 +137,51 @@ describe('complex references', function(){ | ||
it('property + index + property', function(){ | ||
var vm = '$foo.bar[1].junk'; | ||
var ast = Parser.parse(vm)[0]; | ||
var vm = '$foo.bar[1].junk' | ||
var ast = Parser.parse(vm)[0] | ||
assert.equal('foo' , ast.id); | ||
assert.equal(3 , ast.path.length); | ||
assert.equal('foo', ast.id) | ||
assert.equal(3, ast.path.length) | ||
var paths = ast.path; | ||
var paths = ast.path | ||
assert.equal('property' , paths[0].type); | ||
assert.equal('index' , paths[1].type); | ||
assert.equal('property' , paths[2].type); | ||
assert.equal('property', paths[0].type) | ||
assert.equal('index', paths[1].type) | ||
assert.equal('property', paths[2].type) | ||
}); | ||
}) | ||
it('method + index', function(){ | ||
var vm = '$foo.callMethod()[1]'; | ||
var ast = Parser.parse(vm)[0]; | ||
var vm = '$foo.callMethod()[1]' | ||
var ast = Parser.parse(vm)[0] | ||
assert.equal(2, ast.path.length); | ||
assert.equal(2, ast.path.length) | ||
assert.equal('method' , ast.path[0].type); | ||
assert.equal('callMethod' , ast.path[0].id); | ||
assert.equal('method', ast.path[0].type) | ||
assert.equal('callMethod', ast.path[0].id) | ||
assert.equal('index' , ast.path[1].type); | ||
assert.equal('1' , ast.path[1].id.value); | ||
assert.equal('integer' , ast.path[1].id.type); | ||
assert.equal('index', ast.path[1].type) | ||
assert.equal('1', ast.path[1].id.value) | ||
assert.equal('integer', ast.path[1].id.type) | ||
}); | ||
}) | ||
it('property should not start with alphabet', function(){ | ||
var asts = Parser.parse('$foo.124'); | ||
var ast2 = Parser.parse('$foo.-24')[0]; | ||
var asts = Parser.parse('$foo.124') | ||
var ast2 = Parser.parse('$foo.-24')[0] | ||
assert.equal(3 , asts.length); | ||
assert.equal('foo' , asts[0].id); | ||
assert.equal(undefined , asts[0].path); | ||
assert.equal(3, asts.length) | ||
assert.equal('foo', asts[0].id) | ||
assert.equal(undefined, asts[0].path) | ||
assert.equal(undefined , ast2.path); | ||
assert.equal(undefined, ast2.path) | ||
}); | ||
}) | ||
it('index should end with close bracket', function(){ | ||
assert.throws(function(){ | ||
Parser.parse("$foo.bar['a'12]"); | ||
}, /Parse error/); | ||
}); | ||
Parser.parse("$foo.bar['a'12]") | ||
}, /Parse error/) | ||
}) | ||
}); | ||
}) | ||
@@ -187,21 +191,32 @@ describe('Directives', function(){ | ||
it('#macro', function(){ | ||
var vm = '#macro( d $a $b)#if($b)$a#end#end #d($foo $bar)'; | ||
var asts = Parser.parse(vm); | ||
assert.equal(asts.length, 3); | ||
assert.equal(asts[0][1].length, 2); | ||
}); | ||
var vm = '#macro( d $a $b)#if($b)$a#end#end #d($foo $bar)' | ||
var asts = Parser.parse(vm) | ||
}); | ||
var ifAst = asts[0][1] | ||
assert.equal(ifAst[0].condition.type, 'references') | ||
assert.equal(ifAst[0].condition.id, 'b') | ||
assert.equal(ifAst[0].condition.prue, undefined) | ||
assert.equal(ifAst[1].type, 'references') | ||
assert.equal(ifAst[1].id, 'a') | ||
assert.equal(ifAst[1].prue, true) | ||
assert.equal(asts.length, 3) | ||
assert.equal(ifAst.length, 2) | ||
}) | ||
}) | ||
describe('comment identify', function(){ | ||
it('one line comment', function(){ | ||
var asts = Parser.parse('#set( $monkey.Number = 123)##number literal'); | ||
var asts = Parser.parse('#set( $monkey.Number = 123)##number literal') | ||
assert.equal(2 , asts.length); | ||
assert.equal('comment' , asts[1].type); | ||
}); | ||
assert.equal(2, asts.length) | ||
assert.equal('comment', asts[1].type) | ||
}) | ||
}); | ||
}) | ||
}); | ||
}) |
Sorry, the diff of this file is not supported yet
423099
3855