Permalink
Browse files

Merge branch 'remove-inlining'

  • Loading branch information...
2 parents 465057e + 34966f6 commit 8b4562d5652b72b430c789127103dc287f37e157 @tj tj committed Aug 26, 2011
Showing with 140 additions and 318 deletions.
  1. +4 −18 Readme.md
  2. +5 −113 lib/jade.js
  3. +8 −0 lib/runtime.js
  4. +10 −4 test/filters.test.js
  5. +113 −183 test/jade.test.js
View
22 Readme.md
@@ -57,7 +57,7 @@ via npm:
$ make jade.min.js
- By default Jade inlines its utility functions within the template functions generated, such as `escape()` and `attrs()`. Jade also instruments templates with line number statements such as `__.lineno = 3` for debugging purposes. When used in a browser it's useful to minimize this boiler plate, you can do so by passing the option `{ compileDebug: false }`, and `{ inline: false }`. The following template
+ By default Jade instruments templates with line number statements such as `__.lineno = 3` for debugging purposes. When used in a browser it's useful to minimize this boiler plate, you can do so by passing the option `{ compileDebug: false }`, and `{ inline: false }`. The following template
p Hello #{name}
@@ -84,30 +84,19 @@ function anonymous(locals) {
```javascript
var jade = require('jade');
- // Render a string
- jade.render('string of jade', { options: 'here' });
-
- // Render a file
- jade.renderFile('path/to/some.jade', { options: 'here' }, function(err, html){
- // options are optional,
- // the callback can be the second arg
- });
-
// Compile a function
var fn = jade.compile('string of jade', options);
- fn.call(scope, locals);
+ fn(locals);
```
### Options
- - `scope` Evaluation scope (`this`)
- `self` Use a `self` namespace to hold the locals. _false by default_
- `locals` Local variable object
- `filename` Used in exceptions, and required when using includes
- `debug` Outputs tokens and function body generated
- `compiler` Compiler to replace jade's default
- `compileDebug` When `false` no debug instrumentation is compiled
- - `inline` When `false` the helpers are not inlined (ideal for client-side use)
## Syntax
@@ -169,8 +158,7 @@ well cool, but how about large bodies of text:
renders `<p>foo bar baz rawr.....</p>`
interpolation? yup! both types of text can utilize interpolation,
-if we passed `{ locals: { name: 'tj', email: 'tj@vision-media.ca' }}` to `render()`
-we can do the following:
+if we passed `{ name: 'tj', email: 'tj@vision-media.ca' }` to the compiled function we can do the following:
#user #{name} &lt;#{email}&gt;
@@ -655,9 +643,7 @@ and the following _layout.jade_:
both includes _includes/head_ and _includes/foot_ are
read relative to the `filename` option given to _layout.jade_,
-which should be an absolute path to this file, however Express
-and the `renderFile()` method do this for you. Include then parses
-these files, and injects the AST produced to render what you would expect:
+which should be an absolute path to this file, however Express does this for you. Include then parses these files, and injects the AST produced to render what you would expect:
```html
<html>
View
118 lib/jade.js
@@ -86,16 +86,7 @@ exports.runtime = runtime;
*/
function parse(str, options){
- var filename = options.filename
- , inline = false !== options.inline
- , inlined = '';
-
- if (inline) {
- inlined += runtime.attrs.toString() + '\n';
- inlined += runtime.escape.toString() + '\n';
- } else {
- inlined = 'var attrs = jade.attrs, escape = jade.escape;\n';
- }
+ var filename = options.filename;
try {
// Parse
@@ -113,7 +104,6 @@ function parse(str, options){
try {
return ''
- + inlined
+ 'var buf = [];\n'
+ (options.self
? 'var self = locals || {}, __ = __ || locals.__;\n' + js
@@ -135,7 +125,6 @@ function parse(str, options){
* Options:
*
* - `compileDebug` when `false` debugging code is stripped from the compiled template
- * - `inline` when `false` helpers are not inlined, and `jade.<helper>` is used
*
* @param {String} str
* @param {Options} options
@@ -146,24 +135,15 @@ function parse(str, options){
exports.compile = function(str, options){
var options = options || {}
, input = JSON.stringify(str)
- , inline = false !== options.inline
, filename = options.filename
? JSON.stringify(options.filename)
: 'undefined'
- , inlined = ''
, fn;
-
- if (inline) {
- inlined = runtime.rethrow.toString();
- } else {
- inlined = 'var rethrow = jade.rethrow;';
- }
if (options.compileDebug !== false) {
// Reduce closure madness by injecting some locals
fn = [
'var __ = { lineno: 1, input: ' + input + ', filename: ' + filename + ' };'
- , inlined
, 'try {'
, parse(String(str), options || {})
, '} catch (err) {'
@@ -173,97 +153,9 @@ exports.compile = function(str, options){
} else {
fn = parse(String(str), options || {});
}
-
- return new Function('locals', fn);
-};
-
-/**
- * Render the given `str` of jade.
- *
- * Options:
- *
- * - `scope` Evaluation scope (`this`)
- * - `locals` Local variable object
- * - `filename` Used in exceptions, and required by `cache`
- * - `cache` Cache intermediate JavaScript in memory keyed by `filename`
- * - `compiler` Compiler to replade jade's default
- * - `doctype` Specify the default doctype
- *
- * @param {String|Buffer} str
- * @param {Object} options
- * @return {String}
- * @api public
- */
-exports.render = function(str, options){
- var fn
- , options = options || {}
- , filename = options.filename;
-
- // Accept Buffers
- str = String(str);
-
- // Cache support
- if (options.cache) {
- if (filename) {
- if (cache[filename]) {
- fn = cache[filename];
- } else {
- fn = cache[filename] = new Function('locals', parse(str, options));
- }
- } else {
- throw new Error('filename is required when using the cache option');
- }
- } else {
- fn = new Function('locals', parse(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);
- }
+ fn = new Function('locals, attrs, escape, rethrow', fn);
+ return function(locals){
+ return fn(locals, runtime.attrs, runtime.escape, runtime.rethrow);
+ };
};
-
-/**
- * Render jade template at the given `path`.
- *
- * @param {String} path
- * @param {Object} options
- * @param {Function} fn
- * @api public
- */
-
-exports.renderFile = function(path, options, fn){
- var ret;
-
- if (typeof options === 'function') {
- fn = options;
- options = {};
- }
- options.filename = path;
-
- // Primed cache
- if (options.cache && cache[path]) {
- try {
- ret = exports.render('', options);
- } catch (err) {
- return fn(err);
- }
- fn(null, ret);
- } else {
- fs.readFile(path, 'utf8', function(err, str){
- if (err) return fn(err);
- try {
- ret = exports.render(str, options);
- } catch (err) {
- return fn(err);
- }
- fn(null, ret);
- });
- }
-};
View
8 lib/runtime.js
@@ -5,12 +5,20 @@
* MIT Licensed
*/
+/**
+ * Lame Array.isArray() polyfill for now.
+ */
+
if (!Array.isArray) {
Array.isArray = function(arr){
return '[object Array]' == toString.call(arr);
};
}
+/**
+ * Lame Object.keys() polyfill for now.
+ */
+
if (!Object.keys) {
Object.keys = function(obj){
var arr = [];
View
14 test/filters.test.js
@@ -3,10 +3,16 @@
* Module dependencies.
*/
-var jade = require('../'),
- Compiler = jade.Compiler,
- render = jade.render,
- nodes = jade.nodes;
+var jade = require('../')
+ , Compiler = jade.Compiler
+ , nodes = jade.nodes;
+
+// Shortcut
+
+var render = function(str, options){
+ var fn = jade.compile(str, options);
+ return fn(options);
+};
jade.filters.conditionals = function(block, compiler){
return new Visitor(block).compile();
View
296 test/jade.test.js
@@ -16,7 +16,10 @@ try {
// Shortcut
-var render = jade.render;
+var render = function(str, options){
+ var fn = jade.compile(str, options);
+ return fn(options);
+};
module.exports = {
'test .version': function(assert){
@@ -52,41 +55,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 +98,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 +110,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 +150,7 @@ module.exports = {
' li d',
' li e',
].join('\n');
-
+
var html = [
'<ul>',
'<li>a</li>',
@@ -156,7 +159,7 @@ module.exports = {
'<li>e</li>',
'</ul>'
].join('');
-
+
assert.equal(html, render(str));
var str = [
@@ -203,7 +206,7 @@ module.exports = {
' li d',
' li e',
].join('\n');
-
+
var html = [
'<ul>',
'<li>a</li>',
@@ -212,7 +215,7 @@ module.exports = {
'<li>e</li>',
'</ul>'
].join('');
-
+
assert.equal(html, render(str));
},
@@ -231,7 +234,7 @@ module.exports = {
'\t\t\tli d',
'\tli e',
].join('\n');
-
+
var html = [
'<ul>',
'<li>a</li>',
@@ -240,7 +243,7 @@ module.exports = {
'<li>e</li>',
'</ul>'
].join('');
-
+
assert.equal(html, render(str));
},
@@ -263,7 +266,7 @@ module.exports = {
' li d',
' li e',
].join('\n');
-
+
var html = [
'<ul>',
'<li>a</li>',
@@ -272,7 +275,7 @@ module.exports = {
'<li>e</li>',
'</ul>'
].join('');
-
+
assert.equal(html, render(str));
var str = [
@@ -285,7 +288,7 @@ module.exports = {
' ',
' body'
].join('\n');
-
+
var html = [
'<html>',
'<head>',
@@ -294,26 +297,12 @@ 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"'));
},
- 'test cache': function(assert){
- var err;
- try {
- render('foo', { cache: true });
- } catch (e) {
- err = e;
- }
- assert.equal('filename is required when using the cache option', err.message);
-
- assert.equal('<p></p>', render('p', { cache: true, filename: 'foo.jade' }));
- assert.equal('<p></p>', render('p', { cache: true, filename: 'foo.jade' }));
- assert.ok(typeof jade.cache['foo.jade'] === 'function', 'Test cache');
- },
-
'test text': function(assert){
assert.equal('foo\nbar\nbaz\n', render('| foo\n| bar\n| baz'));
assert.equal('foo \nbar \nbaz\n', render('| foo \n| bar \n| baz'));
@@ -348,14 +337,14 @@ module.exports = {
},
'test tag text interpolation': function(assert){
- assert.equal('yo, jade is cool\n', render('| yo, #{name} is cool\n', { locals: { name: 'jade' }}));
- assert.equal('<p>yo, jade is cool</p>', render('p yo, #{name} is cool', { locals: { name: 'jade' }}));
- assert.equal('yo, jade is cool\n', render('| yo, #{name || "jade"} is cool', { locals: { name: null }}));
- assert.equal('yo, \'jade\' is cool\n', render('| yo, #{name || "\'jade\'"} is cool', { locals: { name: null }}));
- 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>' }}));
+ assert.equal('yo, jade is cool\n', render('| yo, #{name} is cool\n', { name: 'jade' }));
+ assert.equal('<p>yo, jade is cool</p>', render('p yo, #{name} is cool', { name: 'jade' }));
+ assert.equal('yo, jade is cool\n', render('| yo, #{name || "jade"} is cool', { name: null }));
+ assert.equal('yo, \'jade\' is cool\n', render('| yo, #{name || "\'jade\'"} is cool', { name: null }));
+ assert.equal('foo &lt;script&gt; bar\n', render('| foo #{code} bar', { code: '<script>' }));
+ assert.equal('foo <script> bar\n', render('| foo !{code} bar', { 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 +403,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,65 +433,65 @@ 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 }}));
-
+ , render("a(href='/user/#{id}') #{name}", { 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 }}));
-
+ , render("a(href='/user/#{id}-#{name}') #{name}", { 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>' }}));
-
+ , render("a(href='/user/#{id}') #{name}", { 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 }}));
-
+ , render('a(href="/user/#{id}") #{name}', { 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 }}));
-
+ , render('a(href="/user/#{id}-#{name}") #{name}', { 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>' }}));
+ , render('a(href="/user/#{id}") #{name}', { name: 'ds', id: '<script>' }));
},
'test attr parens': function(assert){
assert.equal('<p foo="bar">baz</p>', render('p(foo=((("bar"))))= ((("baz")))'));
},
'test code attrs': function(assert){
- assert.equal('<p></p>', render('p(id= name)', { locals: { name: undefined }}));
- assert.equal('<p></p>', render('p(id= name)', { locals: { name: null }}));
- assert.equal('<p></p>', render('p(id= name)', { locals: { name: false }}));
- assert.equal('<p id=""></p>', render('p(id= name)', { locals: { name: '' }}));
- assert.equal('<p id="tj"></p>', render('p(id= name)', { locals: { name: 'tj' }}));
- assert.equal('<p id="default"></p>', render('p(id= name || "default")', { locals: { name: null }}));
- assert.equal('<p id="something"></p>', render("p(id= 'something')", { locals: { name: null }}));
- assert.equal('<p id="something"></p>', render("p(id = 'something')", { locals: { name: null }}));
+ assert.equal('<p></p>', render('p(id= name)', { name: undefined }));
+ assert.equal('<p></p>', render('p(id= name)', { name: null }));
+ assert.equal('<p></p>', render('p(id= name)', { name: false }));
+ assert.equal('<p id=""></p>', render('p(id= name)', { name: '' }));
+ assert.equal('<p id="tj"></p>', render('p(id= name)', { name: 'tj' }));
+ assert.equal('<p id="default"></p>', render('p(id= name || "default")', { name: null }));
+ assert.equal('<p id="something"></p>', render("p(id= 'something')", { name: null }));
+ assert.equal('<p id="something"></p>', render("p(id = 'something')", { name: null }));
assert.equal('<p id="foo"></p>', render("p(id= (true ? 'foo' : 'bar'))"));
assert.equal('<option value="">Foo</option>', render("option(value='') Foo"));
},
'test code attrs class': function(assert){
- assert.equal('<p class="tj"></p>', render('p(class= name)', { locals: { name: 'tj' }}));
- assert.equal('<p class="tj"></p>', render('p( class= name )', { locals: { name: 'tj' }}));
- assert.equal('<p class="default"></p>', render('p(class= name || "default")', { locals: { name: null }}));
- assert.equal('<p class="foo default"></p>', render('p.foo(class= name || "default")', { locals: { name: null }}));
- assert.equal('<p class="default foo"></p>', render('p(class= name || "default").foo', { locals: { name: null }}));
- assert.equal('<p id="default"></p>', render('p(id = name || "default")', { locals: { name: null }}));
+ assert.equal('<p class="tj"></p>', render('p(class= name)', { name: 'tj' }));
+ assert.equal('<p class="tj"></p>', render('p( class= name )', { name: 'tj' }));
+ assert.equal('<p class="default"></p>', render('p(class= name || "default")', { name: null }));
+ assert.equal('<p class="foo default"></p>', render('p.foo(class= name || "default")', { name: null }));
+ assert.equal('<p class="default foo"></p>', render('p(class= name || "default").foo', { name: null }));
+ assert.equal('<p id="default"></p>', render('p(id = name || "default")', { name: null }));
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 +505,12 @@ module.exports = {
'//foo',
'p bar'
].join('\n');
-
+
var html = [
'<!--foo-->',
'<p>bar</p>'
].join('');
-
+
assert.equal(html, render(str));
// Arbitrary indentation
@@ -530,12 +519,12 @@ module.exports = {
' //foo',
'p bar'
].join('\n');
-
+
var html = [
'<!--foo-->',
'<p>bar</p>'
].join('');
-
+
assert.equal(html, render(str));
// Between tags
@@ -545,17 +534,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 +555,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 +586,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 +631,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 +647,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 +676,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 +709,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 +733,7 @@ module.exports = {
].join('\n');
assert.equal('', render(str));
-
+
// Object
var str = [
'- var obj = { foo: "bar", baz: "raz" };',
@@ -798,9 +787,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 +801,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 +815,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 +855,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,64 +870,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){
- called = true;
- assert.equal(ENOENT, err.errno);
- assert.equal(undefined, str);
- });
- beforeExit(function(){
- assert.ok(called);
- });
- },
-
- 'test renderFile() with valid path': function(assert, beforeExit){
- var called;
- jade.renderFile(__dirname + '/fixtures/layout.jade', function(err, str){
- called = true;
- assert.equal(null, err);
- assert.equal('<html><body><h1>Jade</h1></body></html>', str);
- });
- beforeExit(function(){
- assert.ok(called);
- });
- },
-
- 'test renderFile() with options': function(assert, beforeExit){
- var called = 0;
- jade.renderFile(__dirname + '/fixtures/layout.jade', { cache: true }, function(err, str){
- ++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);
- assert.equal('<html><body><h1>Jade</h1></body></html>', str);
- });
- });
- beforeExit(function(){
- assert.equal(2, called);
- });
- },
-
- 'test renderFile() passing of exceptions': function(assert, beforeExit){
- var called = 0;
- jade.renderFile(__dirname + '/fixtures/invalid.jade', { cache: true }, function(err, str){
- ++called;
- assert.ok(typeof err.message === 'string', 'Test passing of exceptions to renderFile() callback');
- assert.equal(undefined, str);
- });
- beforeExit(function(){
- assert.equal(1, called);
- });
- },
-
'test .compile()': function(assert){
var fn = jade.compile('p foo');
assert.equal('<p>foo</p>', fn());
@@ -949,11 +884,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 8b4562d

Please sign in to comment.