velocityjs
Advanced tools
Comparing version 0.3.13 to 0.3.14
@@ -7,2 +7,3 @@ var path = require('path'); | ||
var Velocity = require('../src/velocity'); | ||
var utils = require('../src/utils'); | ||
var Parser = Velocity.Parser; | ||
@@ -38,4 +39,5 @@ var Structure = Velocity.Helper.Structure; | ||
var str = fs.readFileSync(currentPath + '/' + file).toString(); | ||
var asts = Parser.parse(str); | ||
var asts = Parser._parse(str); | ||
var requires = getAllLoad(asts); | ||
asts = utils.makeLevel(asts); | ||
@@ -67,5 +69,5 @@ template = template.replace('{content}', JSON.stringify(asts, null, 2)); | ||
if (ast.type === 'parse') { | ||
if (ast.type === 'macro_call' && ast.id === 'parse') { | ||
var id = ast.id; | ||
var id = ast.args[0]; | ||
if (id.type !== 'string') { | ||
@@ -72,0 +74,0 @@ throw Error('#parse arguments must be string'); |
{ | ||
"name": "velocityjs", | ||
"description": "Velocity Template Language(VTL) for JavaScript", | ||
"version": "0.3.13", | ||
"version": "0.3.14", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "velocity template" |
@@ -23,2 +23,4 @@ module.exports = function(Velocity, utils){ | ||
ret = this._render(block.slice(1)); | ||
} else { | ||
ret = this._render(block); | ||
} | ||
@@ -25,0 +27,0 @@ |
module.exports = function(Velocity, utils){ | ||
var BLOCK_TYPES = ['if', 'foreach', 'macro', 'noescape', 'define']; | ||
/** | ||
@@ -66,4 +66,2 @@ * compile | ||
var str = ''; | ||
var block = []; | ||
var index = 0; | ||
asts = asts || this.asts; | ||
@@ -85,46 +83,25 @@ | ||
utils.forEach(asts, function(ast){ | ||
var type = ast.type; | ||
//foreach if macro时,index加一 | ||
if (utils.indexOf(type, BLOCK_TYPES) > -1) index ++; | ||
if (type === 'comment') return; | ||
if (index) { | ||
type === 'end' && index--; | ||
if (index) { | ||
block.push(ast); | ||
return; | ||
} | ||
} | ||
switch(type) { | ||
switch(ast.type) { | ||
case 'references': | ||
str += this.getReferences(ast, true); | ||
str += this.getReferences(ast, true); | ||
break; | ||
case 'set': | ||
this.setValue(ast); | ||
this.setValue(ast); | ||
break; | ||
case 'break': | ||
this.setBreak = true; | ||
this.setBreak = true; | ||
break; | ||
case 'macro_call': | ||
str += this.getMacro(ast); | ||
str += this.getMacro(ast); | ||
break; | ||
case 'end': | ||
//使用slide获取block的拷贝 | ||
str += this.getBlock(block.slice()); | ||
block = []; | ||
break; | ||
case 'comment': | ||
break; | ||
default: | ||
if (utils.isArray(ast)) { | ||
str += this.getBlock(ast); | ||
} else { | ||
str += ast; | ||
} | ||
str += typeof ast == 'string' ? ast : this.getBlock(ast); | ||
break; | ||
@@ -131,0 +108,0 @@ } |
@@ -45,5 +45,17 @@ module.exports = function(Velocity, utils){ | ||
if (ast.path && ret !== undefined) { | ||
utils.some(ast.path, function(property, i){ | ||
ret = this.getAttributes(property, ret); | ||
return ret === undefined; | ||
//第三个参数,返回后面的参数ast | ||
ret = this.getAttributes(property, ret, ast.path.slice(i + 1), ast); | ||
//提前结束计算,某些情况下,连缀运行,后面的运算影响前面的结果,这种 | ||
//情况需要特殊处理,比如$control.setTempalte('a.vm').setParameter('a', 'b') | ||
//第一个函数就返回结果,这时候,函数返回对象为 | ||
//{$stop: true, $return: 'string'} | ||
//$stop表示停滞,$return:返回结果 | ||
if (ret === undefined || ret.$stop === true) { | ||
ret = ret && ret.$stop ? ret.$return : ret; | ||
return true; | ||
} | ||
}, this); | ||
@@ -58,3 +70,2 @@ } | ||
* set方法需要单独处理,假设set只在references最后$page.setTitle('') | ||
* 对于set连缀的情况$page.setTitle('sd').setName('haha') | ||
*/ | ||
@@ -111,5 +122,12 @@ hasSetMethod: function(ast, context){ | ||
/** | ||
* $foo.bar 属性求值 | ||
* $foo.bar 属性求值,最后面两个参数在用户传递的函数中用到 | ||
* @param {object} property 属性描述,一个对象,主要包括id,type等定义 | ||
* @param {object} baseRef 当前执行链结果,比如$a.b.c,第一次baseRef是$a, | ||
* 第二次是$a.b返回值 | ||
* @param {array} others 后面的属性,比如$a.b.c,求$a.b时,其余的是[c]所对 | ||
* 应的描述 | ||
* @param {object} total 整个ast描述 | ||
* @private | ||
*/ | ||
getAttributes: function(property, baseRef){ | ||
getAttributes: function(property, baseRef, others, total){ | ||
/** | ||
@@ -122,3 +140,3 @@ * type对应着velocity.yy中的attribute,三种类型: method, index, property | ||
if (type === 'method'){ | ||
ret = this.getPropMethod(property, baseRef); | ||
ret = this.getPropMethod(property, baseRef, others, total); | ||
} else if (type === 'property') { | ||
@@ -134,2 +152,3 @@ ret = baseRef[id]; | ||
* $foo.bar[1] index求值 | ||
* @private | ||
*/ | ||
@@ -153,3 +172,3 @@ getPropIndex: function(property, baseRef){ | ||
*/ | ||
getPropMethod: function(property, baseRef){ | ||
getPropMethod: function(property, baseRef, others, total){ | ||
@@ -207,2 +226,9 @@ var id = property.id; | ||
var that = this; | ||
baseRef.$sys = { | ||
others: others, | ||
vm: this, | ||
total: total | ||
}; | ||
baseRef.eval = function() { | ||
@@ -213,2 +239,4 @@ return that.eval.apply(that, arguments); | ||
delete baseRef.$sys; | ||
} else { | ||
@@ -215,0 +243,0 @@ ret = undefined; |
@@ -7,6 +7,6 @@ var Parser = require('./parse/'); | ||
Compile.Parser = Parser; | ||
var _parse = Parser.parse; | ||
Parser._parse = Parser.parse; | ||
Parser.parse = function (str) { | ||
var asts = _parse(str); | ||
var asts = Parser._parse(str); | ||
return utils.makeLevel(asts); | ||
@@ -13,0 +13,0 @@ }; |
@@ -441,3 +441,3 @@ var Velocity = require('../src/velocity'); | ||
it('use eavl with local variable', function(){ | ||
it('use eval with local variable', function(){ | ||
//这一句非常重要,在node端无需处理,web端必须如此声明 | ||
@@ -479,2 +479,45 @@ Compile.Parser = Parser; | ||
describe('self defined function', function() { | ||
it('$stop test', function(){ | ||
var control = { | ||
setTemplate: function(str){ | ||
var $sys = this.$sys; | ||
if ($sys.others.length) { | ||
this.__temp = {}; | ||
var ast = $sys.total; | ||
ast.path = $sys.others; | ||
$sys.vm.getReferences(ast); | ||
} | ||
return { | ||
$return: this.eval(str, this.__temp), | ||
$stop: true | ||
}; | ||
}, | ||
setParameter: function(key, value){ | ||
this.__temp[key] = value; | ||
return this; | ||
} | ||
}; | ||
var str = 'hello $who, welcome to $where'; | ||
var vm = '$control.setTemplate($str).setParameter("who", "Blob").setParameter("where", "China")'; | ||
assert.equal(render(vm, {str: str, control: control}), 'hello Blob, welcome to China'); | ||
}); | ||
}); | ||
}); |
413371
3559