Permalink
Browse files

Move tag helper generator out of loop so that it's defined only once.

  • Loading branch information...
savetheclocktower authored and edspencer committed Jan 22, 2010
1 parent b2b0ca8 commit db6ab758abe489e60a9623e1cc466ada5288873b
Showing with 33 additions and 34 deletions.
  1. +33 −34 Jaml-all.js
@@ -283,46 +283,45 @@ Jaml.Template.prototype = {
*/
(function() {
var tags = Jaml.Template.prototype.tags;

for (var i = tags.length - 1; i >= 0; i--){
var tagName = tags[i];

/**
* This function is created for each tag name and assigned to Template's
* prototype below
*/
var fn = function(tagName) {
return function(attrs) {
var node = new Jaml.Node(tagName);

var firstArgIsAttributes = (typeof attrs == 'object')
&& !(attrs instanceof Jaml.Node)
&& !(attrs instanceof Jaml.TextNode);

if (firstArgIsAttributes) node.setAttributes(attrs);
/**
* This function is created for each tag name and assigned to Template's
* prototype below.
*/
function makeTagHelper(tagName) {
return function(attrs) {
var node = new Jaml.Node(tagName);

var firstArgIsAttributes = (typeof attrs == 'object')
&& !(attrs instanceof Jaml.Node)
&& !(attrs instanceof Jaml.TextNode);

var startIndex = firstArgIsAttributes ? 1 : 0;
if (firstArgIsAttributes) node.setAttributes(attrs);

for (var i=startIndex; i < arguments.length; i++) {
var arg = arguments[i];
var startIndex = firstArgIsAttributes ? 1 : 0;

if (typeof arg == "string" || arg == undefined) {
arg = new Jaml.TextNode(arg || "");
}

if (arg instanceof Jaml.Node || arg instanceof Jaml.TextNode) {
arg.parent = node;
}
for (var i=startIndex; i < arguments.length; i++) {
var arg = arguments[i];

node.addChild(arg);
};

this.nodes.push(node);
if (typeof arg == "string" || arg == undefined) {
arg = new Jaml.TextNode(arg || "");
}

return node;
if (arg instanceof Jaml.Node || arg instanceof Jaml.TextNode) {
arg.parent = node;
}

node.addChild(arg);
};

this.nodes.push(node);

return node;
};

Jaml.Template.prototype[tagName] = fn(tagName);
}

for (var i = tags.length - 1; i >= 0; i--){
var tagName = tags[i];
Jaml.Template.prototype[tagName] = makeTagHelper(tagName);
};
})();
})();

0 comments on commit db6ab75

Please sign in to comment.