Permalink
Browse files

change array push/concat to string concat. closes gh-11

  • Loading branch information...
1 parent 0975001 commit 969019c980a58ed22bcaa6e8b03729e3df3a4833 @paularmstrong committed Sep 27, 2011
Showing with 80 additions and 79 deletions.
  1. +6 −6 docs/custom-tags.md
  2. +7 −7 examples/custom_tags/custom_tags.js
  3. +2 −2 index.js
  4. +1 −1 lib/helpers.js
  5. +14 −13 lib/parser.js
  6. +49 −49 lib/tags.js
  7. +1 −1 tests/tags.test.js
View
@@ -48,14 +48,14 @@ To parse the inner content of a tag for outputting, use `parser.compile.call(thi
output.push(helpers.setVar('__myArg', myArg));
- output.push('<h1>');
- output.push('__output.push(__myArg)');
- output.push('</h1>');
- output.push('<p>');
+ output.push('__output += "<h1>";');
+ output.push('__output += __myArg;');
+ output.push('__output += "</h1>";');
+ output.push('__output += "<p>";');
output.push(parser.compile.call(this, indent + ' '));
- output.push('</p>');
+ output.push('__output += "</p>";');
- return output.join('\n' + indent);
+ return output.join('');
};
exports.mytag.ends = true;
@@ -15,13 +15,13 @@ exports.input = function (indent) {
helpers.setVar('__type', type),
helpers.setVar('__label', label),
helpers.setVar('__value', value),
- ' __output.push(\'<div class="input \' + __type + \'">\')',
- ' __output.push(\'\\n\')',
- ' __output.push(\'<label for="\' + __name + \'">\' + __label + \'</label>\');',
- ' __output.push(\'<input type="\' + __type + \'" name="\' + __name + \'" id="\' + __name + \'" value="\' + __value + \'">\');',
- ' __output.push(\'\\n\')',
- ' __output.push("</div>")',
- ' __output.push(\'\\n\')',
+ ' __output += \'<div class="input \' + __type + \'">\';',
+ ' __output += \'\\n\';',
+ ' __output += \'<label for="\' + __name + \'">\' + __label + \'</label>\';',
+ ' __output += \'<input type="\' + __type + \'" name="\' + __name + \'" id="\' + __name + \'" value="\' + __value + \'">\';',
+ ' __output += \'\\n\';',
+ ' __output += "</div>";',
+ ' __output += \'\\n\';',
'})();'
];
View
@@ -64,7 +64,7 @@ function createTemplate(data, id) {
'__parents = __parents ? __parents.slice() : [];',
// Prevents circular includes (which will crash node without warning)
'var j = __parents.length,',
- ' __output = [],',
+ ' __output = "",',
' __this = this;',
// Note: this loop averages much faster than indexOf across all cases
'while (j--) {',
@@ -75,7 +75,7 @@ function createTemplate(data, id) {
// Add this template as a parent to all includes in its scope
'__parents.push(this.id);',
code,
- 'return __output.join("");',
+ 'return __output;',
].join(''));
template.render = function (context, parents) {
View
@@ -177,7 +177,7 @@ exports.wrapFilters = function (variable, filters, context, escape) {
};
exports.setVar = function (varName, argument) {
- var out = 'var ' + varName + ';' +
+ var out = 'var ' + varName + ' = "";' +
'if (' + check(argument.name) + ') {' +
' ' + varName + ' = ' + exports.wrapFilters(exports.escape(argument.name), argument.filters, null, argument.escape) + ';' +
'} else if (' + check(argument.name, '__context') + ') {' +
View
@@ -191,7 +191,7 @@ exports.parse = function (data, tags, autoescape) {
};
exports.compile = function compile(indent, parentBlock) {
- var code = [''],
+ var code = '',
tokens = [],
parent,
filepath,
@@ -242,7 +242,8 @@ exports.compile = function compile(indent, parentBlock) {
// If this is not a template then just iterate through its tokens
this.tokens.forEach(function (token, index) {
if (typeof token === 'string') {
- return code.push('__output.push("' + token.replace(/\n/g, '\\n').replace(/\r/g, '\\r').replace(/"/g, '\\"') + '");');
+ code += '__output += "' + token.replace(/\n/g, '\\n').replace(/\r/g, '\\r').replace(/"/g, '\\"') + '";';
+ return code;
}
if (typeof token !== 'object') {
@@ -251,21 +252,21 @@ exports.compile = function compile(indent, parentBlock) {
if (token.type === VAR_TOKEN) {
var name = token.name.replace(/\W/g, '_'), args;
- code.push('if (typeof ' + name + ' === "function") {');
+ code += 'if (typeof ' + name + ' === "function") {';
if (token.args && token.args.length) {
_.each(token.args, function (value, key) {
if (helpers.isValidName(value)) {
- code.push(helpers.setVar('__' + parseVariable(value).name.replace(/\W/g, '_'), parseVariable(value)));
+ code += helpers.setVar('__' + parseVariable(value).name.replace(/\W/g, '_'), parseVariable(value));
token.args[key] = '__' + parseVariable(value).name.replace(/\W/g, '_');
}
});
args = '[' + token.args.join(',') + ']';
}
- code.push('__output.push(' + name + '.apply(null, ' + args + '));');
- code.push('} else {');
- code.push(helpers.setVar('__' + name, token));
- code.push('__output.push(__' + name + ');');
- code.push('}');
+ code += '__output += ' + name + '.apply(null, ' + args + ');';
+ code += '} else {';
+ code += helpers.setVar('__' + name, token);
+ code += '__output += __' + name + ';';
+ code += '}';
}
if (token.type !== LOGIC_TOKEN) {
@@ -277,14 +278,14 @@ exports.compile = function compile(indent, parentBlock) {
throw new Error('Block tag found nested in another block tag on line ' + token.line + '.');
}
- code.push(this.blocks[token.args[0]]); // Blocks are already compiled in the precompile part
+ code += this.blocks[token.args[0]]; // Blocks are already compiled in the precompile part
} else if (token.name === 'parent') {
- code.push(indent + ' ' + parentBlock);
+ code += indent + ' ' + parentBlock;
} else {
- code.push(token.compile(indent + ' '));
+ code += token.compile(indent + ' ');
}
}, this);
- return code.join('\n' + indent);
+ return code;
};
View
@@ -40,10 +40,10 @@ exports.include = function (indent) {
' var __template = ' + helpers.escape(template, '__context') + ';',
' }',
' if (typeof __template === "string") {',
- ' __output.push(__this.fromFile(__template).render(__context, __parents));',
+ ' __output += __this.fromFile(__template).render(__context, __parents);',
' }',
' else if (typeof __template === "object" && __template.render) {',
- ' __output.push(__template.render(__context, __parents));',
+ ' __output += __template.render(__context, __parents);',
' }',
'})();'
].join('\n' + indent);
@@ -88,37 +88,37 @@ exports['if'] = function (indent) {
operator = this.args[1],
rightOperand = parser.parseVariable(this.args[2]),
negation = checkIfArgs(leftOperand, operator, rightOperand),
- out;
+ out = '';
indent = indent || '';
- out = ['(function () {'];
- out.push(helpers.setVar('__op1', leftOperand));
+ out = '(function () {';
+ out += helpers.setVar('__op1', leftOperand);
if (rightOperand.name === '') {
- out.push(' if (' + (negation ? '!' : '!!') + '__op1) {');
- out.push(parser.compile.call(this, indent + ' '));
- out.push(' }');
+ out += ' if (' + (negation ? '!' : '!!') + '__op1) {';
+ out += parser.compile.call(this, indent + ' ');
+ out += ' }';
} else {
- out.push(helpers.setVar('__op2', rightOperand));
+ out += helpers.setVar('__op2', rightOperand);
if (typeof operator !== 'undefined') {
if (operator === 'in') {
- out.push('if (');
- out.push(' (Array.isArray(__op2) && __op2.indexOf(__op1) > -1) ||');
- out.push(' (typeof __op2 === "string" && __op2.indexOf(__op1) > -1) ||');
- out.push(' (!Array.isArray(__op2) && typeof __op2 === "object" && __op1 in __op2)');
- out.push(') {');
- out.push(parser.compile.call(this, indent + ' '));
- out.push('}');
+ out += 'if (';
+ out += ' (Array.isArray(__op2) && __op2.indexOf(__op1) > -1) ||';
+ out += ' (typeof __op2 === "string" && __op2.indexOf(__op1) > -1) ||';
+ out += ' (!Array.isArray(__op2) && typeof __op2 === "object" && __op1 in __op2)';
+ out += ') {';
+ out += parser.compile.call(this, indent + ' ');
+ out += '}';
} else {
- out.push('if (__op1 ' + helpers.escape(operator) + ' __op2) {');
- out.push(parser.compile.call(this, indent + ' '));
- out.push('}');
+ out += 'if (__op1 ' + helpers.escape(operator) + ' __op2) {';
+ out += parser.compile.call(this, indent + ' ');
+ out += '}';
}
}
}
- out.push('})();');
- return out.join('\n' + indent);
+ out += '})();';
+ return out;
};
exports['if'].ends = true;
@@ -132,7 +132,7 @@ exports.else = function (indent) {
operator,
rightOperand,
negation,
- out;
+ out = '';
if (ifarg) {
leftOperand = parser.parseVariable(this.args[0]);
@@ -142,34 +142,34 @@ exports.else = function (indent) {
out = [];
if (rightOperand.name === '') {
- out.push('} else if (' + (negation ? '!' : '!!') + '(function() {' + helpers.setVar('__op1', leftOperand) + ' return __op1; })()) {');
+ out += '} else if (' + (negation ? '!' : '!!') + '(function() {' + helpers.setVar('__op1', leftOperand) + ' return __op1; })()) {';
} else {
- out.push(helpers.setVar('__op2', rightOperand));
+ out += helpers.setVar('__op2', rightOperand);
if (operator) {
if (operator === 'in') {
- out.push('} else if (');
- out.push(' (function() {');
- out.push(' ' + helpers.setVar('__op1', leftOperand));
- out.push(' ' + helpers.setVar('__op2', rightOperand));
- out.push(' return (Array.isArray(__op2) && __op2.indexOf(__op1) > -1) ||');
- out.push(' (typeof __op2 === "string" && __op2.indexOf(__op1) > -1) ||');
- out.push(' (!Array.isArray(__op2) && typeof __op2 === "object" && __op1 in __op2);');
- out.push(' })()');
- out.push(') {');
+ out += '} else if (';
+ out += ' (function() {';
+ out += ' ' + helpers.setVar('__op1', leftOperand);
+ out += ' ' + helpers.setVar('__op2', rightOperand);
+ out += ' return (Array.isArray(__op2) && __op2.indexOf(__op1) > -1) ||';
+ out += ' (typeof __op2 === "string" && __op2.indexOf(__op1) > -1) ||';
+ out += ' (!Array.isArray(__op2) && typeof __op2 === "object" && __op1 in __op2);';
+ out += ' })()';
+ out += ') {';
} else {
- out.push('} else if (');
- out.push(' (function() {');
- out.push(' ' + helpers.setVar('__op1', leftOperand));
- out.push(' ' + helpers.setVar('__op2', rightOperand));
- out.push(' return __op1 ' + helpers.escape(operator) + ' __op2;');
- out.push(' })()');
- out.push(') {');
+ out += '} else if (';
+ out += ' (function() {';
+ out += ' ' + helpers.setVar('__op1', leftOperand);
+ out += ' ' + helpers.setVar('__op2', rightOperand);
+ out += ' return __op1 ' + helpers.escape(operator) + ' __op2;';
+ out += ' })()';
+ out += ') {';
}
}
}
- return out.join('\n', indent);
+ return out;
}
return indent + '} else {';
@@ -276,24 +276,24 @@ exports.set = function (indent) {
}
value = parser.parseVariable(this.args[0]);
- return 'var ' + varname + ' = (function () { var __output = []; ' + parser.compile.call({ tokens: [value] }, indent) + ' return __output.join(""); })();';
+ return 'var ' + varname + ' = (function () { var __output = ""; ' + parser.compile.call({ tokens: [value] }, indent) + ' return __output; })();';
};
exports.macro = function (indent) {
var macro = this.args.shift(),
args = '',
- out = [];
+ out = '';
if (this.args.length) {
args = JSON.stringify(this.args).replace(/^\[|\'|\"|\]$/g, '');
}
- out.push('var ' + macro + ' = function (' + args + ') {');
- out.push(' var __output = [];');
- out.push(parser.compile.call(this, indent + ' '));
- out.push(' return __output.join("")');
- out.push('};');
+ out += 'var ' + macro + ' = function (' + args + ') {';
+ out += ' var __output = "";';
+ out += parser.compile.call(this, indent + ' ');
+ out += ' return __output;';
+ out += '};';
- return out.join('\n' + indent);
+ return out;
};
exports.macro.ends = true;
View
@@ -4,7 +4,7 @@ var testCase = require('nodeunit').testCase,
exports['custom tags'] = function (test) {
var tags = {
foo: function (indent) {
- return '__output.push("hi!");';
+ return '__output += "hi!";';
}
},
tmpl8;

0 comments on commit 969019c

Please sign in to comment.