Browse files

only allow one layer of with in templates

  • Loading branch information...
1 parent 377e8f8 commit 011b5a9a0c7d42425fa75c3195819faf9636d685 @tenorviol committed Oct 2, 2011
Showing with 28 additions and 18 deletions.
  1. +1 −1 example/index.xjs
  2. +27 −17 lib/xjs.js
View
2 example/index.xjs
@@ -1,5 +1,5 @@
<?
- var dir = local.dir ? dir : __dirname;
+ var dir = globals.dir ? dir : __dirname;
?>
<html>
<body>
View
44 lib/xjs.js
@@ -50,37 +50,47 @@ function parse(source, options) {
function interpret(source) {
var tree = parser.parse(source);
var js = [
- 'module.exports = '
+ 'module.exports = function (out, globals) {',
+ ' var end = false;',
+ ' if (Object.getPrototypeOf(out) !== module.xjs.XjsStream.prototype) {',
+ ' out = new module.xjs.XjsStream(out);',
+ ' end = true;',
+ ' }',
+ ' if (globals && typeof globals.length === "number" && typeof globals !== "string") {',
+ ' var callee = arguments.callee;',
+ ' for (var i in globals) {',
+ ' callee.call(this, out, globals[i]);',
+ ' }',
+ ' return;',
+ ' }',
+ ' globals = globals || {};',
+ ' with (globals) {'
];
- interpretTree(js, tree, '');
+ interpretChildren(js, tree, ' ');
+ js.push(
+ ' }',
+ ' if (end) {',
+ ' out.end();',
+ ' }',
+ '}'
+ );
js = js.join('\n');
return js;
}
-function interpretTree(js, tree, tab) {
+function interpretTemplate(js, tree, tab) {
js.push(
tab + 'function (out, local) {',
- tab + ' var end = false;',
- tab + ' if (Object.getPrototypeOf(out) !== module.xjs.XjsStream.prototype) {',
- tab + ' out = new module.xjs.XjsStream(out);',
- tab + ' end = true;',
- tab + ' }',
tab + ' if (local && typeof local.length === "number" && typeof local !== "string") {',
tab + ' var callee = arguments.callee;',
tab + ' for (var i in local) {',
tab + ' callee.call(this, out, local[i]);',
tab + ' }',
tab + ' return;',
- tab + ' }',
- tab + ' local = local || {};',
- tab + ' with(local) {'
+ tab + ' }'
);
- interpretChildren(js, tree, tab + ' ');
+ interpretChildren(js, tree, tab + ' ');
js.push(
- tab + ' }',
- tab + ' if (end) {',
- tab + ' out.end();',
- tab + ' }',
tab + '}'
);
return js.join('\n');
@@ -130,7 +140,7 @@ function interpretScript(js, node, tab) {
if (typeof script === 'string') {
js.push(script);
} else {
- interpretTree(js, [script], tab);
+ interpretTemplate(js, [script], tab);
}
});
}

0 comments on commit 011b5a9

Please sign in to comment.