Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Move tag helper generator out of loop so that it's defined only once.
  • Loading branch information
savetheclocktower authored and edspencer committed Jan 23, 2010
1 parent b2b0ca8 commit db6ab75
Showing 1 changed file with 33 additions and 34 deletions.
67 changes: 33 additions & 34 deletions Jaml-all.js
Expand Up @@ -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.