|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703 |
- 'use strict';
-
- var nodes = require('./nodes');
- var filters = require('./filters');
- var doctypes = require('./doctypes');
- var runtime = require('./runtime');
- var utils = require('./utils');
- var selfClosing = require('void-elements');
- var parseJSExpression = require('character-parser').parseMax;
- var constantinople = require('constantinople');
-
- function isConstant(src) {
- return constantinople(src, {jade: runtime, 'jade_interp': undefined});
- }
- function toConstant(src) {
- return constantinople.toConstant(src, {jade: runtime, 'jade_interp': undefined});
- }
- function errorAtNode(node, error) {
- error.line = node.line;
- error.filename = node.filename;
- return error;
- }
-
- /**
- * Initialize `Compiler` with the given `node`.
- *
- * @param {Node} node
- * @param {Object} options
- * @api public
- */
-
- var Compiler = module.exports = function Compiler(node, options) {
- this.options = options = options || {};
- this.node = node;
- this.hasCompiledDoctype = false;
- this.hasCompiledTag = false;
- this.pp = options.pretty || false;
- this.debug = false !== options.compileDebug;
- this.indents = 0;
- this.parentIndents = 0;
- this.terse = false;
- this.mixins = {};
- this.dynamicMixins = false;
- if (options.doctype) this.setDoctype(options.doctype);
- };
-
- /**
- * Compiler prototype.
- */
-
- Compiler.prototype = {
-
- /**
- * Compile parse tree to JavaScript.
- *
- * @api public
- */
-
- compile: function(){
- this.buf = [];
- if (this.pp) this.buf.push("var jade_indent = [];");
- this.lastBufferedIdx = -1;
- this.visit(this.node);
- if (!this.dynamicMixins) {
- // if there are no dynamic mixins we can remove any un-used mixins
- var mixinNames = Object.keys(this.mixins);
- for (var i = 0; i < mixinNames.length; i++) {
- var mixin = this.mixins[mixinNames[i]];
- if (!mixin.used) {
- for (var x = 0; x < mixin.instances.length; x++) {
- for (var y = mixin.instances[x].start; y < mixin.instances[x].end; y++) {
- this.buf[y] = '';
- }
- }
- }
- }
- }
- return this.buf.join('\n');
- },
-
- /**
- * Sets the default doctype `name`. Sets terse mode to `true` when
- * html 5 is used, causing self-closing tags to end with ">" vs "/>",
- * and boolean attributes are not mirrored.
- *
- * @param {string} name
- * @api public
- */
-
- setDoctype: function(name){
- this.doctype = doctypes[name.toLowerCase()] || '<!DOCTYPE ' + name + '>';
- this.terse = this.doctype.toLowerCase() == '<!doctype html>';
- this.xml = 0 == this.doctype.indexOf('<?xml');
- },
-
- /**
- * Buffer the given `str` exactly as is or with interpolation
- *
- * @param {String} str
- * @param {Boolean} interpolate
- * @api public
- */
-
- buffer: function (str, interpolate) {
- var self = this;
- if (interpolate) {
- var match = /(\\)?([#!]){((?:.|\n)*)$/.exec(str);
- if (match) {
- this.buffer(str.substr(0, match.index), false);
- if (match[1]) { // escape
- this.buffer(match[2] + '{', false);
- this.buffer(match[3], true);
- return;
- } else {
- var rest = match[3];
- var range = parseJSExpression(rest);
- var code = ('!' == match[2] ? '' : 'jade.escape') + "((jade_interp = " + range.src + ") == null ? '' : jade_interp)";
- this.bufferExpression(code);
- this.buffer(rest.substr(range.end + 1), true);
- return;
- }
- }
- }
-
- str = JSON.stringify(str);
- str = str.substr(1, str.length - 2);
-
- if (this.lastBufferedIdx == this.buf.length) {
- if (this.lastBufferedType === 'code') this.lastBuffered += ' + "';
- this.lastBufferedType = 'text';
- this.lastBuffered += str;
- this.buf[this.lastBufferedIdx - 1] = 'buf.push(' + this.bufferStartChar + this.lastBuffered + '");'
- } else {
- this.buf.push('buf.push("' + str + '");');
- this.lastBufferedType = 'text';
- this.bufferStartChar = '"';
- this.lastBuffered = str;
- this.lastBufferedIdx = this.buf.length;
- }
- },
-
- /**
- * Buffer the given `src` so it is evaluated at run time
- *
- * @param {String} src
- * @api public
- */
-
- bufferExpression: function (src) {
- if (isConstant(src)) {
- return this.buffer(toConstant(src) + '', false)
- }
- if (this.lastBufferedIdx == this.buf.length) {
- if (this.lastBufferedType === 'text') this.lastBuffered += '"';
- this.lastBufferedType = 'code';
- this.lastBuffered += ' + (' + src + ')';
- this.buf[this.lastBufferedIdx - 1] = 'buf.push(' + this.bufferStartChar + this.lastBuffered + ');'
- } else {
- this.buf.push('buf.push(' + src + ');');
- this.lastBufferedType = 'code';
- this.bufferStartChar = '';
- this.lastBuffered = '(' + src + ')';
- this.lastBufferedIdx = this.buf.length;
- }
- },
-
- /**
- * Buffer an indent based on the current `indent`
- * property and an additional `offset`.
- *
- * @param {Number} offset
- * @param {Boolean} newline
- * @api public
- */
-
- prettyIndent: function(offset, newline){
- offset = offset || 0;
- newline = newline ? '\n' : '';
- this.buffer(newline + Array(this.indents + offset).join(' '));
- if (this.parentIndents)
- this.buf.push("buf.push.apply(buf, jade_indent);");
- },
-
- /**
- * Visit `node`.
- *
- * @param {Node} node
- * @api public
- */
-
- visit: function(node){
- var debug = this.debug;
-
- if (debug) {
- this.buf.push('jade_debug.unshift({ lineno: ' + node.line
- + ', filename: ' + (node.filename
- ? JSON.stringify(node.filename)
- : 'jade_debug[0].filename')
- + ' });');
- }
-
- // Massive hack to fix our context
- // stack for - else[ if] etc
- if (false === node.debug && this.debug) {
- this.buf.pop();
- this.buf.pop();
- }
-
- this.visitNode(node);
-
- if (debug) this.buf.push('jade_debug.shift();');
- },
-
- /**
- * Visit `node`.
- *
- * @param {Node} node
- * @api public
- */
-
- visitNode: function(node){
- return this['visit' + node.type](node);
- },
-
- /**
- * Visit case `node`.
- *
- * @param {Literal} node
- * @api public
- */
-
- visitCase: function(node){
- var _ = this.withinCase;
- this.withinCase = true;
- this.buf.push('switch (' + node.expr + '){');
- this.visit(node.block);
- this.buf.push('}');
- this.withinCase = _;
- },
-
- /**
- * Visit when `node`.
- *
- * @param {Literal} node
- * @api public
- */
-
- visitWhen: function(node){
- if ('default' == node.expr) {
- this.buf.push('default:');
- } else {
- this.buf.push('case ' + node.expr + ':');
- }
- if (node.block) {
- this.visit(node.block);
- this.buf.push(' break;');
- }
- },
-
- /**
- * Visit literal `node`.
- *
- * @param {Literal} node
- * @api public
- */
-
- visitLiteral: function(node){
- this.buffer(node.str);
- },
-
- /**
- * Visit all nodes in `block`.
- *
- * @param {Block} block
- * @api public
- */
-
- visitBlock: function(block){
- var len = block.nodes.length
- , escape = this.escape
- , pp = this.pp
-
- // 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)
- this.buffer('\n');
- }
- },
-
- /**
- * Visit a mixin's `block` keyword.
- *
- * @param {MixinBlock} block
- * @api public
- */
-
- visitMixinBlock: function(block){
- if (this.pp) this.buf.push("jade_indent.push('" + Array(this.indents + 1).join(' ') + "');");
- this.buf.push('block && block();');
- if (this.pp) this.buf.push("jade_indent.pop();");
- },
-
- /**
- * Visit `doctype`. Sets terse mode to `true` when html 5
- * is used, causing self-closing tags to end with ">" vs "/>",
- * and boolean attributes are not mirrored.
- *
- * @param {Doctype} doctype
- * @api public
- */
-
- visitDoctype: function(doctype){
- if (doctype && (doctype.val || !this.doctype)) {
- this.setDoctype(doctype.val || 'default');
- }
-
- if (this.doctype) this.buffer(this.doctype);
- this.hasCompiledDoctype = true;
- },
-
- /**
- * Visit `mixin`, generating a function that
- * may be called within the template.
- *
- * @param {Mixin} mixin
- * @api public
- */
-
- visitMixin: function(mixin){
- var name = 'jade_mixins[';
- var args = mixin.args || '';
- var block = mixin.block;
- var attrs = mixin.attrs;
- var attrsBlocks = mixin.attributeBlocks;
- var pp = this.pp;
- var dynamic = mixin.name[0]==='#';
- var key = mixin.name;
- if (dynamic) this.dynamicMixins = true;
- name += (dynamic ? mixin.name.substr(2,mixin.name.length-3):'"'+mixin.name+'"')+']';
-
- this.mixins[key] = this.mixins[key] || {used: false, instances: []};
- if (mixin.call) {
- this.mixins[key].used = true;
- if (pp) this.buf.push("jade_indent.push('" + Array(this.indents + 1).join(' ') + "');")
- if (block || attrs.length || attrsBlocks.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 || attrsBlocks.length) {
- this.buf.push('},');
- } else {
- this.buf.push('}');
- }
- }
-
- if (attrsBlocks.length) {
- if (attrs.length) {
- var val = this.attrs(attrs);
- attrsBlocks.unshift(val);
- }
- this.buf.push('attributes: jade.merge([' + attrsBlocks.join(',') + '])');
- } else if (attrs.length) {
- var val = this.attrs(attrs);
- this.buf.push('attributes: ' + val);
- }
-
- if (args) {
- this.buf.push('}, ' + args + ');');
- } else {
- this.buf.push('});');
- }
-
- } else {
- this.buf.push(name + '(' + args + ');');
- }
- if (pp) this.buf.push("jade_indent.pop();")
- } else {
- var mixin_start = this.buf.length;
- this.buf.push(name + ' = function(' + args + '){');
- this.buf.push('var block = (this && this.block), attributes = (this && this.attributes) || {};');
- this.parentIndents++;
- this.visit(block);
- this.parentIndents--;
- this.buf.push('};');
- var mixin_end = this.buf.length;
- this.mixins[key].instances.push({start: mixin_start, end: mixin_end});
- }
- },
-
- /**
- * Visit `tag` buffering tag markup, generating
- * attributes, visiting the `tag`'s code and block.
- *
- * @param {Tag} tag
- * @api public
- */
-
- visitTag: function(tag){
- this.indents++;
- var name = tag.name
- , pp = this.pp
- , self = this;
-
- function bufferName() {
- if (tag.buffer) self.bufferExpression(name);
- else self.buffer(name);
- }
-
- if ('pre' == tag.name) this.escape = true;
-
- if (!this.hasCompiledTag) {
- if (!this.hasCompiledDoctype && 'html' == name) {
- this.visitDoctype();
- }
- this.hasCompiledTag = true;
- }
-
- // pretty print
- if (pp && !tag.isInline())
- this.prettyIndent(0, true);
-
- if (tag.selfClosing || (!this.xml && selfClosing.indexOf(tag.name) !== -1)) {
- this.buffer('<');
- bufferName();
- this.visitAttributes(tag.attrs, tag.attributeBlocks);
- this.terse
- ? this.buffer('>')
- : this.buffer('/>');
- // if it is non-empty throw an error
- if (tag.block &&
- !(tag.block.type === 'Block' && tag.block.nodes.length === 0) &&
- tag.block.nodes.some(function (tag) {
- return tag.type !== 'Text' || !/^\s*$/.test(tag.val)
- })) {
- throw errorAtNode(tag, new Error(name + ' is self closing and should not have content.'));
- }
- } else {
- // Optimize attributes buffering
- this.buffer('<');
- bufferName();
- this.visitAttributes(tag.attrs, tag.attributeBlocks);
- this.buffer('>');
- if (tag.code) this.visitCode(tag.code);
- this.visit(tag.block);
-
- // pretty print
- if (pp && !tag.isInline() && 'pre' != tag.name && !tag.canInline())
- this.prettyIndent(0, true);
-
- this.buffer('</');
- bufferName();
- this.buffer('>');
- }
-
- if ('pre' == tag.name) this.escape = false;
-
- this.indents--;
- },
-
- /**
- * Visit `filter`, throwing when the filter does not exist.
- *
- * @param {Filter} filter
- * @api public
- */
-
- visitFilter: function(filter){
- var text = filter.block.nodes.map(
- function(node){ return node.val; }
- ).join('\n');
- filter.attrs.filename = this.options.filename;
- try {
- this.buffer(filters(filter.name, text, filter.attrs), true);
- } catch (err) {
- throw errorAtNode(filter, err);
- }
- },
-
- /**
- * Visit `text` node.
- *
- * @param {Text} text
- * @api public
- */
-
- visitText: function(text){
- this.buffer(text.val, true);
- },
-
- /**
- * Visit a `comment`, only buffering when the buffer flag is set.
- *
- * @param {Comment} comment
- * @api public
- */
-
- visitComment: function(comment){
- if (!comment.buffer) return;
- if (this.pp) this.prettyIndent(1, true);
- this.buffer('<!--' + comment.val + '-->');
- },
-
- /**
- * Visit a `BlockComment`.
- *
- * @param {Comment} comment
- * @api public
- */
-
- visitBlockComment: function(comment){
- if (!comment.buffer) return;
- if (this.pp) this.prettyIndent(1, true);
- this.buffer('<!--' + comment.val);
- this.visit(comment.block);
- if (this.pp) this.prettyIndent(1, true);
- this.buffer('-->');
- },
-
- /**
- * Visit `code`, respecting buffer / escape flags.
- * If the code is followed by a block, wrap it in
- * a self-calling function.
- *
- * @param {Code} code
- * @api public
- */
-
- visitCode: function(code){
- // Wrap code blocks with {}.
- // we only wrap unbuffered code blocks ATM
- // since they are usually flow control
-
- // Buffer code
- if (code.buffer) {
- var val = code.val.trimLeft();
- val = 'null == (jade_interp = '+val+') ? "" : jade_interp';
- if (code.escape) val = 'jade.escape(' + val + ')';
- this.bufferExpression(val);
- } else {
- this.buf.push(code.val);
- }
-
- // Block support
- if (code.block) {
- if (!code.buffer) this.buf.push('{');
- this.visit(code.block);
- if (!code.buffer) this.buf.push('}');
- }
- },
-
- /**
- * Visit `each` block.
- *
- * @param {Each} each
- * @api public
- */
-
- visitEach: function(each){
- this.buf.push(''
- + '// iterate ' + each.obj + '\n'
- + ';(function(){\n'
- + ' var $$obj = ' + each.obj + ';\n'
- + ' if (\'number\' == typeof $$obj.length) {\n');
-
- if (each.alternative) {
- this.buf.push(' if ($$obj.length) {');
- }
-
- this.buf.push(''
- + ' for (var ' + each.key + ' = 0, $$l = $$obj.length; ' + each.key + ' < $$l; ' + each.key + '++) {\n'
- + ' var ' + each.val + ' = $$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(''
- + ' } else {\n'
- + ' var $$l = 0;\n'
- + ' for (var ' + each.key + ' in $$obj) {\n'
- + ' $$l++;'
- + ' var ' + each.val + ' = $$obj[' + each.key + '];\n');
-
- this.visit(each.block);
-
- this.buf.push(' }\n');
- if (each.alternative) {
- this.buf.push(' if ($$l === 0) {');
- this.visit(each.alternative);
- this.buf.push(' }');
- }
- this.buf.push(' }\n}).call(this);\n');
- },
-
- /**
- * Visit `attrs`.
- *
- * @param {Array} attrs
- * @api public
- */
-
- visitAttributes: function(attrs, attributeBlocks){
- if (attributeBlocks.length) {
- if (attrs.length) {
- var val = this.attrs(attrs);
- attributeBlocks.unshift(val);
- }
- this.bufferExpression('jade.attrs(jade.merge([' + attributeBlocks.join(',') + ']), ' + JSON.stringify(this.terse) + ')');
- } else if (attrs.length) {
- this.attrs(attrs, true);
- }
- },
-
- /**
- * Compile attributes.
- */
-
- attrs: function(attrs, buffer){
- var buf = [];
- var classes = [];
- var classEscaping = [];
-
- attrs.forEach(function(attr){
- var key = attr.name;
- var escaped = attr.escaped;
-
- if (key === 'class') {
- classes.push(attr.val);
- classEscaping.push(attr.escaped);
- } else if (isConstant(attr.val)) {
- if (buffer) {
- this.buffer(runtime.attr(key, toConstant(attr.val), escaped, this.terse));
- } else {
- var val = toConstant(attr.val);
- if (escaped && !(key.indexOf('data') === 0 && typeof val !== 'string')) {
- val = runtime.escape(val);
- }
- buf.push(JSON.stringify(key) + ': ' + JSON.stringify(val));
- }
- } else {
- if (buffer) {
- this.bufferExpression('jade.attr("' + key + '", ' + attr.val + ', ' + JSON.stringify(escaped) + ', ' + JSON.stringify(this.terse) + ')');
- } else {
- var val = attr.val;
- if (escaped && !(key.indexOf('data') === 0)) {
- val = 'jade.escape(' + val + ')';
- } else if (escaped) {
- val = '(typeof (jade_interp = ' + val + ') == "string" ? jade.escape(jade_interp) : jade_interp)';
- }
- buf.push(JSON.stringify(key) + ': ' + val);
- }
- }
- }.bind(this));
- if (buffer) {
- if (classes.every(isConstant)) {
- this.buffer(runtime.cls(classes.map(toConstant), classEscaping));
- } else {
- this.bufferExpression('jade.cls([' + classes.join(',') + '], ' + JSON.stringify(classEscaping) + ')');
- }
- } else if (classes.length) {
- if (classes.every(isConstant)) {
- classes = JSON.stringify(runtime.joinClasses(classes.map(toConstant).map(runtime.joinClasses).map(function (cls, i) {
- return classEscaping[i] ? runtime.escape(cls) : cls;
- })));
- } else {
- classes = '(jade_interp = ' + JSON.stringify(classEscaping) + ',' +
- ' jade.joinClasses([' + classes.join(',') + '].map(jade.joinClasses).map(function (cls, i) {' +
- ' return jade_interp[i] ? jade.escape(cls) : cls' +
- ' }))' +
- ')';
- }
- if (classes.length)
- buf.push('"class": ' + classes);
- }
- return '{' + buf.join(',') + '}';
- }
- };
|