Permalink
Browse files

fixed `jade.render()`

  • Loading branch information...
1 parent a29568c commit 54f034136d5cbfe78e2b059a190bc318c6faebc2 @tj tj committed Aug 26, 2011
Showing with 88 additions and 100 deletions.
  1. +3 −10 lib/jade.js
  2. +85 −90 test/jade.test.js
View
13 lib/jade.js
@@ -192,24 +192,17 @@ exports.render = function(str, options){
if (cache[filename]) {
fn = cache[filename];
} else {
- fn = cache[filename] = new Function('locals', parse(str, options));
+ fn = cache[filename] = exports.compile(str, options);
}
} else {
throw new Error('filename is required when using the cache option');
}
} else {
- fn = new Function('locals', parse(str, options));
+ fn = exports.compile(str, options);
}
// Render the template
- try {
- var locals = options.locals || {}
- , meta = { lineno: 1 };
- locals.__ = meta;
- return fn.call(options.scope, locals);
- } catch (err) {
- runtime.rethrow(err, str, filename, meta.lineno);
- }
+ return fn.call(options.scope, options.locals || {});
};
/**
View
175 test/jade.test.js
@@ -52,41 +52,41 @@ module.exports = {
'div',
'img'
].join('\r\n');
-
+
var html = [
'<p></p>',
'<div></div>',
'<img/>'
].join('');
-
+
assert.equal(html, render(str));
var str = [
'p',
'div',
'img'
].join('\r');
-
+
var html = [
'<p></p>',
'<div></div>',
'<img/>'
].join('');
-
+
assert.equal(html, render(str));
var str = [
'p',
'div',
'img'
].join('\r\n');
-
+
var html = [
'<p></p>',
'<div></div>',
'<img>'
].join('');
-
+
assert.equal(html, render(str, { doctype:'html' }));
},
@@ -95,7 +95,7 @@ module.exports = {
assert.equal("<p>'foo'\n</p>", render("p\n | 'foo'"));
assert.equal('<a href="/foo"></a>', render("- var path = 'foo';\na(href='/' + path)"));
},
-
+
'test block-expansion': function(assert){
assert.equal("<li><a>foo</a></li><li><a>bar</a></li><li><a>baz</a></li>", render("li: a foo\nli: a bar\nli: a baz"));
assert.equal("<li class=\"first\"><a>foo</a></li><li><a>bar</a></li><li><a>baz</a></li>", render("li.first: a foo\nli: a bar\nli: a baz"));
@@ -107,13 +107,13 @@ module.exports = {
'div',
'img'
].join('\n');
-
+
var html = [
'<p></p>',
'<div></div>',
'<img/>'
].join('');
-
+
assert.equal(html, render(str), 'Test basic tags');
assert.equal('<fb:foo-bar></fb:foo-bar>', render('fb:foo-bar'), 'Test hyphens');
assert.equal('<div class="something"></div>', render('div.something'), 'Test classes');
@@ -147,7 +147,7 @@ module.exports = {
' li d',
' li e',
].join('\n');
-
+
var html = [
'<ul>',
'<li>a</li>',
@@ -156,7 +156,7 @@ module.exports = {
'<li>e</li>',
'</ul>'
].join('');
-
+
assert.equal(html, render(str));
var str = [
@@ -203,7 +203,7 @@ module.exports = {
' li d',
' li e',
].join('\n');
-
+
var html = [
'<ul>',
'<li>a</li>',
@@ -212,7 +212,7 @@ module.exports = {
'<li>e</li>',
'</ul>'
].join('');
-
+
assert.equal(html, render(str));
},
@@ -231,7 +231,7 @@ module.exports = {
'\t\t\tli d',
'\tli e',
].join('\n');
-
+
var html = [
'<ul>',
'<li>a</li>',
@@ -240,7 +240,7 @@ module.exports = {
'<li>e</li>',
'</ul>'
].join('');
-
+
assert.equal(html, render(str));
},
@@ -263,7 +263,7 @@ module.exports = {
' li d',
' li e',
].join('\n');
-
+
var html = [
'<ul>',
'<li>a</li>',
@@ -272,7 +272,7 @@ module.exports = {
'<li>e</li>',
'</ul>'
].join('');
-
+
assert.equal(html, render(str));
var str = [
@@ -285,7 +285,7 @@ module.exports = {
' ',
' body'
].join('\n');
-
+
var html = [
'<html>',
'<head>',
@@ -294,7 +294,7 @@ module.exports = {
'<body></body>',
'</html>'
].join('');
-
+
assert.equal(html, render(str));
assert.equal('<foo></foo>something<bar></bar>', render('foo\n= "something"\nbar'));
assert.equal('<foo></foo>something<bar></bar>else', render('foo\n= "something"\nbar\n= "else"'));
@@ -355,7 +355,7 @@ module.exports = {
assert.equal('foo &lt;script&gt; bar\n', render('| foo #{code} bar', { locals: { code: '<script>' }}));
assert.equal('foo <script> bar\n', render('| foo !{code} bar', { locals: { code: '<script>' }}));
},
-
+
'test flexible indentation': function(assert){
assert.equal('<html><body><h1>Wahoo</h1><p>test</p></body></html>', render('html\n body\n h1 Wahoo\n p test'));
},
@@ -414,20 +414,20 @@ module.exports = {
assert.equal('<div style="color: white"></div>', render("div(style='color: white')"));
assert.equal('<p class="foo"></p>', render("p('class'='foo')"), 'Test keys with single quotes');
assert.equal('<p class="foo"></p>', render("p(\"class\"= 'foo')"), 'Test keys with double quotes');
-
+
assert.equal('<p data-lang="en"></p>', render('p(data-lang = "en")'));
assert.equal('<p data-dynamic="true"></p>', render('p("data-dynamic"= "true")'));
assert.equal('<p data-dynamic="true" class="name"></p>', render('p("class"= "name", "data-dynamic"= "true")'));
assert.equal('<p data-dynamic="true"></p>', render('p(\'data-dynamic\'= "true")'));
assert.equal('<p data-dynamic="true" class="name"></p>', render('p(\'class\'= "name", \'data-dynamic\'= "true")'));
assert.equal('<p data-dynamic="true" yay="yay" class="name"></p>', render('p(\'class\'= "name", \'data-dynamic\'= "true", yay)'));
-
+
assert.equal('<input checked="checked" type="checkbox"/>', render('input(checked, type="checkbox")'));
-
+
assert.equal('<a data-foo="{ foo: \'bar\', bar= \'baz\' }"></a>', render('a(data-foo = "{ foo: \'bar\', bar= \'baz\' }")'));
-
+
assert.equal('<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>', render('meta(http-equiv="X-UA-Compatible", content="IE=edge,chrome=1")'));
-
+
assert.equal('<div style="background: url(/images/test.png)">Foo</div>', render("div(style= 'background: url(/images/test.png)') Foo"));
assert.equal('<div style="background = url(/images/test.png)">Foo</div>', render("div(style= 'background = url(/images/test.png)') Foo"));
assert.equal('<div style="foo">Foo</div>', render("div(style= ['foo', 'bar'][0]) Foo"));
@@ -444,33 +444,33 @@ module.exports = {
assert.equal('<meta content="what\'s up? \'weee\'"/>', render('meta(content="what\'s up? \'weee\'")'));
},
-
+
'test colons option': function(assert){
assert.equal('<a href="/bar"></a>', render('a(href:"/bar")', { colons: true }));
},
-
+
'test class attr array': function(assert){
assert.equal('<body class="foo bar baz"></body>', render('body(class=["foo", "bar", "baz"])'));
},
-
+
'test attr interpolation': function(assert){
// Test single quote interpolation
assert.equal('<a href="/user/12">tj</a>'
, render("a(href='/user/#{id}') #{name}", { locals: { name: 'tj', id: 12 }}));
-
+
assert.equal('<a href="/user/12-tj">tj</a>'
, render("a(href='/user/#{id}-#{name}') #{name}", { locals: { name: 'tj', id: 12 }}));
-
+
assert.equal('<a href="/user/&lt;script&gt;">tj</a>'
, render("a(href='/user/#{id}') #{name}", { locals: { name: 'tj', id: '<script>' }}));
-
+
// Test double quote interpolation
assert.equal('<a href="/user/13">ds</a>'
, render('a(href="/user/#{id}") #{name}', { locals: { name: 'ds', id: 13 }}));
-
+
assert.equal('<a href="/user/13-ds">ds</a>'
, render('a(href="/user/#{id}-#{name}") #{name}', { locals: { name: 'ds', id: 13 }}));
-
+
assert.equal('<a href="/user/&lt;script&gt;">ds</a>'
, render('a(href="/user/#{id}") #{name}', { locals: { name: 'ds', id: '<script>' }}));
},
@@ -502,7 +502,7 @@ module.exports = {
assert.equal('<p id="user-1"></p>', render('p(id = "user-" + 1)'));
assert.equal('<p class="user-1"></p>', render('p(class = "user-" + 1)'));
},
-
+
'test code buffering': function(assert){
assert.equal('<p></p>', render('p= null'));
assert.equal('<p></p>', render('p= undefined'));
@@ -516,12 +516,12 @@ module.exports = {
'//foo',
'p bar'
].join('\n');
-
+
var html = [
'<!--foo-->',
'<p>bar</p>'
].join('');
-
+
assert.equal(html, render(str));
// Arbitrary indentation
@@ -530,12 +530,12 @@ module.exports = {
' //foo',
'p bar'
].join('\n');
-
+
var html = [
'<!--foo-->',
'<p>bar</p>'
].join('');
-
+
assert.equal(html, render(str));
// Between tags
@@ -545,17 +545,17 @@ module.exports = {
'// bar ',
'p baz'
].join('\n');
-
+
var html = [
'<p>foo</p>',
'<!-- bar -->',
'<p>baz</p>'
].join('');
-
+
assert.equal(html, render(str));
-
+
// Quotes
-
+
var str = "<!-- script(src: '/js/validate.js') -->",
js = "// script(src: '/js/validate.js') ";
assert.equal(str, render(js));
@@ -566,24 +566,24 @@ module.exports = {
'//- foo',
'p bar'
].join('\n');
-
+
var html = [
'<p>bar</p>'
].join('');
-
+
assert.equal(html, render(str));
var str = [
'p foo',
'//- bar ',
'p baz'
].join('\n');
-
+
var html = [
'<p>foo</p>',
'<p>baz</p>'
].join('');
-
+
assert.equal(html, render(str));
},
@@ -597,42 +597,42 @@ module.exports = {
assert.equal('test', render('- var foo = "test"\n=foo'));
assert.equal('foo\n<em>test</em>bar\n', render('- var foo = "test"\n| foo\nem= foo\n| bar'));
assert.equal('test<h2>something</h2>', render('!= "test"\nh2 something'));
-
+
var str = [
'- var foo = "<script>";',
'= foo',
'!= foo'
].join('\n');
-
+
var html = [
'&lt;script&gt;',
'<script>'
].join('');
-
+
assert.equal(html, render(str));
var str = [
'- var foo = "<script>";',
'- if (foo)',
' p= foo'
].join('\n');
-
+
var html = [
'<p>&lt;script&gt;</p>'
].join('');
-
+
assert.equal(html, render(str));
var str = [
'- var foo = "<script>";',
'- if (foo)',
' p!= foo'
].join('\n');
-
+
var html = [
'<p><script></p>'
].join('');
-
+
assert.equal(html, render(str));
var str = [
@@ -642,11 +642,11 @@ module.exports = {
'- else',
' p.noFoo no foo'
].join('\n');
-
+
var html = [
'<p class="noFoo">no foo</p>'
].join('');
-
+
assert.equal(html, render(str));
var str = [
@@ -658,24 +658,24 @@ module.exports = {
'- else',
' p.noFoo no foo'
].join('\n');
-
+
var html = [
'<p>kinda foo</p>'
].join('');
-
+
assert.equal(html, render(str));
-
+
var str = [
'p foo',
'= "bar"',
].join('\n');
-
+
var html = [
'<p>foo</p>bar'
].join('');
-
+
assert.equal(html, render(str));
-
+
var str = [
'title foo',
'- if (true)',
@@ -687,21 +687,21 @@ module.exports = {
].join('');
assert.equal(html, render(str));
-
+
var str = [
'foo',
' bar= "bar"',
' baz= "baz"',
].join('\n');
-
+
var html = [
'<foo>',
'<bar>bar',
'<baz>baz</baz>',
'</bar>',
'</foo>'
].join('');
-
+
assert.equal(html, render(str));
},
@@ -720,20 +720,20 @@ module.exports = {
].join('');
assert.equal(html, render(str));
-
+
// Any enumerable (length property)
var str = [
'- var jQuery = { length: 3, 0: 1, 1: 2, 2: 3 };',
'- each item in jQuery',
' li= item'
].join('\n');
-
+
var html = [
'<li>1</li>',
'<li>2</li>',
'<li>3</li>'
].join('');
-
+
assert.equal(html, render(str));
// Empty array
@@ -744,7 +744,7 @@ module.exports = {
].join('\n');
assert.equal('', render(str));
-
+
// Object
var str = [
'- var obj = { foo: "bar", baz: "raz" };',
@@ -798,9 +798,9 @@ module.exports = {
var html = [
'<li>name tj</li>'
].join('');
-
+
assert.equal(html, render(str));
-
+
var str = [
'- var users = ["tobi", "loki", "jane"]',
'each user in users',
@@ -812,9 +812,9 @@ module.exports = {
'<li>loki</li>',
'<li>jane</li>',
].join('');
-
+
assert.equal(html, render(str));
-
+
var str = [
'- var users = ["tobi", "loki", "jane"]',
'for user in users',
@@ -826,38 +826,38 @@ module.exports = {
'<li>loki</li>',
'<li>jane</li>',
].join('');
-
+
assert.equal(html, render(str));
},
-
+
'test if': function(assert){
var str = [
'- var users = ["tobi", "loki", "jane"]',
'if users.length',
' p users: #{users.length}',
].join('\n');
-
+
assert.equal('<p>users: 3</p>', render(str));
},
-
+
'test unless': function(assert){
var str = [
'- var users = ["tobi", "loki", "jane"]',
'unless users.length',
' p no users',
].join('\n');
-
+
assert.equal('', render(str));
-
+
var str = [
'- var users = []',
'unless users.length',
' p no users',
].join('\n');
-
+
assert.equal('<p>no users</p>', render(str));
},
-
+
'test else': function(assert){
var str = [
'- var users = []',
@@ -866,10 +866,10 @@ module.exports = {
'else',
' p users: none',
].join('\n');
-
+
assert.equal('<p>users: none</p>', render(str));
},
-
+
'test else if': function(assert){
var str = [
'- var users = ["tobi", "jane", "loki"]',
@@ -881,10 +881,10 @@ module.exports = {
' else',
' p #{user}',
].join('\n');
-
+
assert.equal('<p>awesome tobi</p><p>lame jane</p><p>loki</p>', render(str));
},
-
+
'test renderFile() fs exception': function(assert, beforeExit){
var called;
jade.renderFile('foo', function(err, str){
@@ -915,7 +915,7 @@ module.exports = {
++called;
assert.equal(null, err);
assert.equal('<html><body><h1>Jade</h1></body></html>', str);
-
+
jade.renderFile(__dirname + '/fixtures/layout.jade', { cache: true }, function(err, str){
++called;
assert.equal(null, err);
@@ -949,11 +949,6 @@ module.exports = {
assert.equal('<p>bar</p>', fn({ foo: 'bar' }));
},
- 'test .compile() scope': function(assert){
- var fn = jade.compile('p= this.foo');
- assert.equal('<p>bar</p>', fn.call({ foo: 'bar' }));
- },
-
'test .compile() no debug': function(assert){
var fn = jade.compile('p foo\np #{bar}', {compileDebug: false});
assert.equal('<p>foo</p><p>baz</p>', fn({bar: 'baz'}));

0 comments on commit 54f0341

Please sign in to comment.