Browse files

Removed __proto__ hack. It was fun while it lasted

  • Loading branch information...
1 parent a28c2c2 commit 532c86a3827357f9a6f06ca06a5999f971421c50 @raycmorgan committed Feb 2, 2012
Showing with 63 additions and 31 deletions.
  1. +45 −24 lib/mu/renderer.js
  2. +2 −0 test/examples/twice.html
  3. +5 −0 test/examples/twice.js
  4. +2 −0 test/examples/twice.txt
  5. +9 −7 test/run_examples_test.js
View
69 lib/mu/renderer.js
@@ -6,13 +6,17 @@ var parser = require('./parser'),
exports.render = render;
function render(tokens, context, partials, stream, callback) {
+ if (!Array.isArray(context)) {
+ context = [context];
+ }
+
+ return _render(tokens, context, partials, stream, callback);
+}
+
+function _render(tokens, context, partials, stream, callback) {
if (tokens[0] !== 'multi') {
throw new Error('Mu - WTF did you give me? I expected mustache tokens.');
}
-
- //if (!Array.isArray(context)) {
- // context = [context];
- //}
var i = 1;
@@ -93,36 +97,55 @@ function escape(string) {
}
function normalize(context, name, body) {
- var val = context[name];
+ var val = walkToFind(context, name);
if (typeof(val) === 'function') {
- val = context[name](body);
+ val = val.call(smashContext(context), body);
}
return val;
}
-function section(view, name, val, tokens, partials, stream, callback) {
- // var val = normalize(view, name, body);
-
- // if (typeof val === 'boolean') {
- // return val ? render(tokens, view, partials, stream, callback) : callback();
- // }
-
+function walkToFind(context, name) {
+ var i = context.length;
+ while (i--) {
+ if (name in context[i]) {
+ return context[i][name];
+ }
+ }
+
+ return undefined;
+}
+
+// TODO: if Proxy, make more efficient
+// TODO: cache?
+function smashContext(context) {
+ var obj = {};
+
+ for (var i = 0; i < context.length; i++) {
+ var level = context[i];
+
+ for (var k in level) {
+ obj[k] = level[k];
+ }
+ }
+
+ return obj;
+}
+
+function section(context, name, val, tokens, partials, stream, callback) {
if (val instanceof Array) {
var i = 0;
(function next() {
var item = val[i++];
if (item) {
- //view.push(item);
- var proto = insertProto(item, view);
- render(tokens, item, partials, stream, function () {
- proto.__proto__ = baseProto;
+ context.push(item);
+ _render(tokens, context, partials, stream, function () {
+ context.pop();
next();
});
- //view.pop();
} else {
callback();
}
@@ -133,16 +156,14 @@ function section(view, name, val, tokens, partials, stream, callback) {
}
if (typeof val === 'object') {
- //view.push(val);
- var proto = insertProto(val, view);
- render(tokens, val, partials, stream, callback);
- proto.__proto__ = baseProto;
- //view.pop();
+ context.push(val);
+ _render(tokens, context, partials, stream, callback);
+ context.pop();
return;
}
if (val) {
- return render(tokens, view, partials, stream, callback);
+ return _render(tokens, context, partials, stream, callback);
}
return callback();
View
2 test/examples/twice.html
@@ -0,0 +1,2 @@
+{{#person}}{{name}}{{/person}}
+{{#person}}{{name}}{{/person}}
View
5 test/examples/twice.js
@@ -0,0 +1,5 @@
+{
+ person: {
+ name: "tom"
+ }
+}
View
2 test/examples/twice.txt
@@ -0,0 +1,2 @@
+tom
+tom
View
16 test/run_examples_test.js
@@ -6,10 +6,10 @@ var assert = require('assert'),
mu.root = path.join(__dirname, 'examples');
[
- 'boolean',
- 'comments',
- 'complex',
- 'deep_partial',
+ 'boolean',
+ 'comments',
+ 'complex',
+ 'deep_partial',
// 'delimiters',
'error_not_found',
'escaped',
@@ -19,6 +19,7 @@ mu.root = path.join(__dirname, 'examples');
'recursion_with_same_names',
'reuse_of_enumerables',
'simple',
+ 'twice',
'two_in_a_row',
'unescaped',
].forEach(function (name) {
@@ -28,13 +29,14 @@ mu.root = path.join(__dirname, 'examples');
js = eval('(' + js + ')');
var buffer = '';
-
+
mu.compileAndRender(name + '.html', js)
.on('data', function (c) { buffer += c.toString(); })
.on('end', function () {
+ console.log("Testing: " + name);
assert.equal(buffer, text);
- console.log(name + ' passed');
- })
+ console.log(name + ' passed\n');
+ });
/*
var js = fs.readFileSync(path.join(mu.root, name + '.js')).toString(),

0 comments on commit 532c86a

Please sign in to comment.