Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added try/catch to runtime.js for browser support #734

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 66 additions & 25 deletions jade.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,9 @@ Compiler.prototype = {
*/

setDoctype: function(name){
var doctype = doctypes[(name || 'default').toLowerCase()];
doctype = doctype || '<!DOCTYPE ' + name + '>';
this.doctype = doctype;
this.terse = '5' == name || 'html' == name;
name = (name && name.toLowerCase()) || 'default';
this.doctype = doctypes[name] || '<!DOCTYPE ' + name + '>';
this.terse = this.doctype.toLowerCase() == '<!doctype html>';
this.xml = 0 == this.doctype.indexOf('<?xml');
},

Expand Down Expand Up @@ -173,7 +172,7 @@ Compiler.prototype = {
* @param {Boolean} newline
* @api public
*/

prettyIndent: function(offset, newline){
offset = offset || 0;
newline = newline ? '\\n' : '';
Expand Down Expand Up @@ -281,24 +280,24 @@ Compiler.prototype = {
var len = block.nodes.length
, escape = this.escape
, pp = this.pp

// Block keyword has a special meaning in mixins
if (this.parentIndents && block.mode) {
if (pp) this.buf.push("__indent.push('" + Array(this.indents + 1).join(' ') + "');")
this.buf.push('block && block();');
if (pp) this.buf.push("__indent.pop();")
return;
}

// Pretty print multi-line text
if (pp && len > 1 && !escape && block.nodes[0].isText && block.nodes[1].isText)
this.prettyIndent(1, true);

for (var i = 0; i < len; ++i) {
// Pretty print text
if (pp && i > 0 && !escape && block.nodes[i].isText && block.nodes[i-1].isText)
this.prettyIndent(1, false);

this.visit(block.nodes[i]);
// Multiple text nodes are separated by newlines
if (block.nodes[i+1] && block.nodes[i].isText && block.nodes[i+1].isText)
Expand Down Expand Up @@ -342,27 +341,27 @@ Compiler.prototype = {
if (mixin.call) {
if (pp) this.buf.push("__indent.push('" + Array(this.indents + 1).join(' ') + "');")
if (block || attrs.length) {

this.buf.push(name + '.call({');

if (block) {
this.buf.push('block: function(){');

// Render block with no indents, dynamically added when rendered
this.parentIndents++;
var _indents = this.indents;
this.indents = 0;
this.visit(mixin.block);
this.indents = _indents;
this.parentIndents--;

if (attrs.length) {
this.buf.push('},');
} else {
this.buf.push('}');
}
}

if (attrs.length) {
var val = this.attrs(attrs);
if (val.inherits) {
Expand All @@ -372,13 +371,13 @@ Compiler.prototype = {
this.buf.push('attributes: {' + val.buf + '}, escaped: ' + val.escaped);
}
}

if (args) {
this.buf.push('}, ' + args + ');');
} else {
this.buf.push('});');
}

} else {
this.buf.push(name + '(' + args + ');');
}
Expand Down Expand Up @@ -566,14 +565,27 @@ Compiler.prototype = {
this.buf.push(''
+ '// iterate ' + each.obj + '\n'
+ ';(function(){\n'
+ ' if (\'number\' == typeof ' + each.obj + '.length) {\n'
+ ' if (\'number\' == typeof ' + each.obj + '.length) {\n');

if (each.alternative) {
this.buf.push(' if (' + each.obj + '.length) {');
}

this.buf.push(''
+ ' for (var ' + each.key + ' = 0, $$l = ' + each.obj + '.length; ' + each.key + ' < $$l; ' + each.key + '++) {\n'
+ ' var ' + each.val + ' = ' + each.obj + '[' + each.key + '];\n');

this.visit(each.block);

this.buf.push(' }\n');

if (each.alternative) {
this.buf.push(' } else {');
this.visit(each.alternative);
this.buf.push(' }');
}

this.buf.push(''
+ ' }\n'
+ ' } else {\n'
+ ' for (var ' + each.key + ' in ' + each.obj + ') {\n'
+ ' if (' + each.obj + '.hasOwnProperty(' + each.key + ')){'
Expand Down Expand Up @@ -656,16 +668,16 @@ function isConstant(val){
// Check strings/literals
if (/^ *("([^"\\]*(\\.[^"\\]*)*)"|'([^'\\]*(\\.[^'\\]*)*)'|true|false|null|undefined) *$/i.test(val))
return true;

// Check numbers
if (!isNaN(Number(val)))
return true;

// Check arrays
var matches;
if (matches = /^ *\[(.*)\] *$/.exec(val))
return matches[1].split(',').every(isConstant);

return false;
}

Expand All @@ -684,6 +696,7 @@ function escape(html){
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;');
}

}); // module: compiler.js

require.register("doctypes.js", function(module, exports, require){
Expand Down Expand Up @@ -859,7 +872,7 @@ var Parser = require('./parser')
* Library version.
*/

exports.version = '0.26.1';
exports.version = '0.27.2';

/**
* Expose self closing tags.
Expand Down Expand Up @@ -956,6 +969,20 @@ function parse(str, options){
}
}

/**
* Strip any UTF-8 BOM off of the start of `str`, if it exists.
*
* @param {String} str
* @return {String}
* @api private
*/

function stripBOM(str){
return 0xFEFF == str.charCodeAt(0)
? str.substring(1)
: str;
}

/**
* Compile a `Function` representation of the given jade `str`.
*
Expand All @@ -979,17 +1006,19 @@ exports.compile = function(str, options){
: 'undefined'
, fn;

str = stripBOM(String(str));

if (options.compileDebug !== false) {
fn = [
'var __jade = [{ lineno: 1, filename: ' + filename + ' }];'
, 'try {'
, parse(String(str), options)
, parse(str, options)
, '} catch (err) {'
, ' rethrow(err, __jade[0].filename, __jade[0].lineno);'
, '}'
].join('\n');
} else {
fn = parse(String(str), options);
fn = parse(str, options);
}

if (client) {
Expand Down Expand Up @@ -1085,6 +1114,8 @@ require.register("lexer.js", function(module, exports, require){
* MIT Licensed
*/

var utils = require('./utils');

/**
* Initialize `Lexer` with the given `str`.
*
Expand Down Expand Up @@ -3004,6 +3035,10 @@ Parser.prototype = {
, node = new nodes.Each(tok.code, tok.val, tok.key);
node.line = this.line();
node.block = this.block();
if (this.peek().type == 'code' && this.peek().val == 'else') {
this.advance();
node.alternative = this.block();
}
return node;
},

Expand Down Expand Up @@ -3484,8 +3519,14 @@ exports.escape = function escape(html){
exports.rethrow = function rethrow(err, filename, lineno){
if (!filename) throw err;

try {
var str = require('fs').readFileSync(filename, 'utf8');
}
catch (e) {
throw err;
}

var context = 3
, str = require('fs').readFileSync(filename, 'utf8')
, lines = str.split('\n')
, start = Math.max(lineno - context, 0)
, end = Math.min(lines.length, lineno + context);
Expand Down
4 changes: 2 additions & 2 deletions jade.min.js

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion lib/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,14 @@ exports.escape = function escape(html){
exports.rethrow = function rethrow(err, filename, lineno){
if (!filename) throw err;

try {
var str = require('fs').readFileSync(filename, 'utf8');
}
catch (e) {
throw err;
}

var context = 3
, str = require('fs').readFileSync(filename, 'utf8')
, lines = str.split('\n')
, start = Math.max(lineno - context, 0)
, end = Math.min(lines.length, lineno + context);
Expand Down
10 changes: 8 additions & 2 deletions runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,14 @@ exports.escape = function escape(html){
exports.rethrow = function rethrow(err, filename, lineno){
if (!filename) throw err;

try {
var str = require('fs').readFileSync(filename, 'utf8');
}
catch (e) {
throw err;
}

var context = 3
, str = require('fs').readFileSync(filename, 'utf8')
, lines = str.split('\n')
, start = Math.max(lineno - context, 0)
, end = Math.min(lines.length, lineno + context);
Expand All @@ -176,4 +182,4 @@ exports.rethrow = function rethrow(err, filename, lineno){

return exports;

})({});
})({});
2 changes: 1 addition & 1 deletion runtime.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.