Permalink
Browse files

Simplify attribute complication

  • Loading branch information...
1 parent 4eac61b commit c945df16d2ac92ca91a92316ac5e544f7d8200dc @wycats wycats committed Mar 21, 2013
View
@@ -331,7 +331,7 @@ define("htmlbars/compiler/pass1",
} else if (node instanceof BlockElement) {
compiler.block(node);
} else {
- compiler[node.type + "Content"](node);
+ compiler[node.type](node);
}
}
}
@@ -378,21 +378,11 @@ define("htmlbars/compiler/pass1",
var name = attribute[0],
value = attribute[1];
- if (value.length > 1) {
- var program = compileAttr(value);
- this.children.push(program);
-
- this.opcode('blockAttr', name, this.children.length - 1);
- return;
- }
-
- value = value[0];
+ var program = compileAttr(value);
+ this.children.push(program);
- if (typeof value === 'string') {
- this.opcode('attribute', name, value);
- } else {
- this[value.type + "Attr"](name, value);
- }
+ this.opcode('attribute', name, this.children.length - 1);
+ return;
};
compiler1.nodeHelper = function(mustache) {
@@ -402,24 +392,7 @@ define("htmlbars/compiler/pass1",
this.opcode('nodeHelper', mustache.id.string, mustache.params.length);
};
- compiler1.mustacheAttr = function(attrName, mustache) {
- var type = classifyMustache(mustache, this.options);
-
- if (type === 'simple') {
- this.opcode('dynamicAttr', attrName, mustache.id.parts, mustache.escaped);
- } else if (type === 'ambiguous') {
- this.opcode('ambiguousAttr', attrName, mustache.id.string, mustache.escaped);
- } else {
- this.opcode('program', null);
- processParams(this, mustache.params);
- processHash(this, mustache.hash);
- this.opcode('helperAttr', attrName, mustache.id.string, mustache.params.length);
- }
-
- applyAttribute(this, attrName, mustache);
- };
-
- compiler1.mustacheContent = function(mustache) {
+ compiler1.mustache = function(mustache) {
var type = classifyMustache(mustache, this.options);
if (type === 'simple') {
@@ -500,14 +473,6 @@ define("htmlbars/compiler/pass1",
}
}
- function applyAttribute(compiler, attrName, mustache) {
- if (mustache.escaped) {
- compiler.opcode('applyAttribute', attrName);
- } else {
- throw new Error("Unescaped attributes are not allowed");
- }
- }
-
__exports__.Compiler1 = Compiler1;
});
@@ -619,11 +584,7 @@ define("htmlbars/compiler/pass2",
this.push("var " + elRef + " = el = " + call('document.createElement', string(tagName)));
};
- compiler2.attribute = function(name, value) {
- this.push(call('el.setAttribute', string(name), string(value)));
- };
-
- compiler2.blockAttr = function(name, child) {
+ compiler2.attribute = function(name, child) {
var invokeRererender = call('el.setAttribute', string(name), call('child' + child, 'context', hash(['rerender:rerender'])));
var rerender = 'function rerender() { ' + invokeRererender + '}';
var options = hash(['rerender:' + rerender, 'element:el', 'attrName:' + string(name)]);
@@ -655,23 +616,6 @@ define("htmlbars/compiler/pass2",
this.push(helper('helperContents', string(name), this.el(), 'context', prepared.args, hash(prepared.options)));
};
- compiler2.dynamicAttr = function(attrName, parts) {
- pushStack(this.stack, helper('resolveAttr', 'context', quotedArray(parts), this.el(), string(attrName)));
- };
-
- compiler2.ambiguousAttr = function(attrName, str) {
- pushStack(this.stack, helper('ambiguousAttr', this.el(), 'context', string(attrName), string(str)));
- };
-
- compiler2.helperAttr = function(attrName, name, size) {
- var prepared = prepareHelper(this.stack, size);
- pushStack(this.stack, helper('helperAttr', string(name), this.el(), string(attrName), 'context', prepared.args, hash(prepared.options)));
- };
-
- compiler2.applyAttribute = function(attrName) {
- this.push(helper('applyAttribute', this.el(), string(attrName), popStack(this.stack)));
- };
-
__exports__.Compiler2 = Compiler2;
});
@@ -1087,9 +1031,11 @@ define("htmlbars/runtime",
}, context)
},
- ambiguousAttr: function(element, context, attrName, string) {
+ ambiguousAttr: function(context, string, options) {
var helper, value, args;
+ return this.resolveInAttr(context, [string], options);
+
if (helper = helpers[string]) {
throw new Error("helperAttr is not implemented yet");
} else {
@@ -1105,11 +1051,6 @@ define("htmlbars/runtime",
return helper.apply(context, args);
},
- applyAttribute: function(element, attrName, value) {
- if (value === undefined) { return; }
- element.setAttribute(attrName, value);
- },
-
resolveAttr: function(context, parts, element, attrName, escaped) {
var helper = helpers.RESOLVE_ATTR;
@@ -55,7 +55,7 @@ define(
} else if (node instanceof BlockElement) {
compiler.block(node);
} else {
- compiler[node.type + "Content"](node);
+ compiler[node.type](node);
}
}
}
@@ -102,21 +102,11 @@ define(
var name = attribute[0],
value = attribute[1];
- if (value.length > 1) {
- var program = compileAttr(value);
- this.children.push(program);
-
- this.opcode('blockAttr', name, this.children.length - 1);
- return;
- }
-
- value = value[0];
+ var program = compileAttr(value);
+ this.children.push(program);
- if (typeof value === 'string') {
- this.opcode('attribute', name, value);
- } else {
- this[value.type + "Attr"](name, value);
- }
+ this.opcode('attribute', name, this.children.length - 1);
+ return;
};
compiler1.nodeHelper = function(mustache) {
@@ -126,24 +116,7 @@ define(
this.opcode('nodeHelper', mustache.id.string, mustache.params.length);
};
- compiler1.mustacheAttr = function(attrName, mustache) {
- var type = classifyMustache(mustache, this.options);
-
- if (type === 'simple') {
- this.opcode('dynamicAttr', attrName, mustache.id.parts, mustache.escaped);
- } else if (type === 'ambiguous') {
- this.opcode('ambiguousAttr', attrName, mustache.id.string, mustache.escaped);
- } else {
- this.opcode('program', null);
- processParams(this, mustache.params);
- processHash(this, mustache.hash);
- this.opcode('helperAttr', attrName, mustache.id.string, mustache.params.length);
- }
-
- applyAttribute(this, attrName, mustache);
- };
-
- compiler1.mustacheContent = function(mustache) {
+ compiler1.mustache = function(mustache) {
var type = classifyMustache(mustache, this.options);
if (type === 'simple') {
@@ -224,13 +197,5 @@ define(
}
}
- function applyAttribute(compiler, attrName, mustache) {
- if (mustache.escaped) {
- compiler.opcode('applyAttribute', attrName);
- } else {
- throw new Error("Unescaped attributes are not allowed");
- }
- }
-
__exports__.Compiler1 = Compiler1;
});
@@ -106,11 +106,7 @@ define(
this.push("var " + elRef + " = el = " + call('document.createElement', string(tagName)));
};
- compiler2.attribute = function(name, value) {
- this.push(call('el.setAttribute', string(name), string(value)));
- };
-
- compiler2.blockAttr = function(name, child) {
+ compiler2.attribute = function(name, child) {
var invokeRererender = call('el.setAttribute', string(name), call('child' + child, 'context', hash(['rerender:rerender'])));
var rerender = 'function rerender() { ' + invokeRererender + '}';
var options = hash(['rerender:' + rerender, 'element:el', 'attrName:' + string(name)]);
@@ -142,22 +138,5 @@ define(
this.push(helper('helperContents', string(name), this.el(), 'context', prepared.args, hash(prepared.options)));
};
- compiler2.dynamicAttr = function(attrName, parts) {
- pushStack(this.stack, helper('resolveAttr', 'context', quotedArray(parts), this.el(), string(attrName)));
- };
-
- compiler2.ambiguousAttr = function(attrName, str) {
- pushStack(this.stack, helper('ambiguousAttr', this.el(), 'context', string(attrName), string(str)));
- };
-
- compiler2.helperAttr = function(attrName, name, size) {
- var prepared = prepareHelper(this.stack, size);
- pushStack(this.stack, helper('helperAttr', string(name), this.el(), string(attrName), 'context', prepared.args, hash(prepared.options)));
- };
-
- compiler2.applyAttribute = function(attrName) {
- this.push(helper('applyAttribute', this.el(), string(attrName), popStack(this.stack)));
- };
-
__exports__.Compiler2 = Compiler2;
});
@@ -50,9 +50,11 @@ define(
}, context)
},
- ambiguousAttr: function(element, context, attrName, string) {
+ ambiguousAttr: function(context, string, options) {
var helper, value, args;
+ return this.resolveInAttr(context, [string], options);
+
if (helper = helpers[string]) {
throw new Error("helperAttr is not implemented yet");
} else {
@@ -68,11 +70,6 @@ define(
return helper.apply(context, args);
},
- applyAttribute: function(element, attrName, value) {
- if (value === undefined) { return; }
- element.setAttribute(attrName, value);
- },
-
resolveAttr: function(context, parts, element, attrName, escaped) {
var helper = helpers.RESOLVE_ATTR;
@@ -50,7 +50,7 @@ function processChildren(compiler, children) {
} else if (node instanceof BlockElement) {
compiler.block(node);
} else {
- compiler[node.type + "Content"](node);
+ compiler[node.type](node);
}
}
}
@@ -97,21 +97,11 @@ compiler1.attribute = function(attribute) {
var name = attribute[0],
value = attribute[1];
- if (value.length > 1) {
- var program = compileAttr(value);
- this.children.push(program);
-
- this.opcode('blockAttr', name, this.children.length - 1);
- return;
- }
-
- value = value[0];
+ var program = compileAttr(value);
+ this.children.push(program);
- if (typeof value === 'string') {
- this.opcode('attribute', name, value);
- } else {
- this[value.type + "Attr"](name, value);
- }
+ this.opcode('attribute', name, this.children.length - 1);
+ return;
};
compiler1.nodeHelper = function(mustache) {
@@ -121,24 +111,7 @@ compiler1.nodeHelper = function(mustache) {
this.opcode('nodeHelper', mustache.id.string, mustache.params.length);
};
-compiler1.mustacheAttr = function(attrName, mustache) {
- var type = classifyMustache(mustache, this.options);
-
- if (type === 'simple') {
- this.opcode('dynamicAttr', attrName, mustache.id.parts, mustache.escaped);
- } else if (type === 'ambiguous') {
- this.opcode('ambiguousAttr', attrName, mustache.id.string, mustache.escaped);
- } else {
- this.opcode('program', null);
- processParams(this, mustache.params);
- processHash(this, mustache.hash);
- this.opcode('helperAttr', attrName, mustache.id.string, mustache.params.length);
- }
-
- applyAttribute(this, attrName, mustache);
-};
-
-compiler1.mustacheContent = function(mustache) {
+compiler1.mustache = function(mustache) {
var type = classifyMustache(mustache, this.options);
if (type === 'simple') {
@@ -219,12 +192,4 @@ function appendMustache(compiler, mustache) {
}
}
-function applyAttribute(compiler, attrName, mustache) {
- if (mustache.escaped) {
- compiler.opcode('applyAttribute', attrName);
- } else {
- throw new Error("Unescaped attributes are not allowed");
- }
-}
-
export { Compiler1 };
@@ -96,11 +96,7 @@ compiler2.openElement = function(tagName) {
this.push("var " + elRef + " = el = " + call('document.createElement', string(tagName)));
};
-compiler2.attribute = function(name, value) {
- this.push(call('el.setAttribute', string(name), string(value)));
-};
-
-compiler2.blockAttr = function(name, child) {
+compiler2.attribute = function(name, child) {
var invokeRererender = call('el.setAttribute', string(name), call('child' + child, 'context', hash(['rerender:rerender'])));
var rerender = 'function rerender() { ' + invokeRererender + '}';
var options = hash(['rerender:' + rerender, 'element:el', 'attrName:' + string(name)]);
@@ -132,21 +128,4 @@ compiler2.nodeHelper = function(name, size) {
this.push(helper('helperContents', string(name), this.el(), 'context', prepared.args, hash(prepared.options)));
};
-compiler2.dynamicAttr = function(attrName, parts) {
- pushStack(this.stack, helper('resolveAttr', 'context', quotedArray(parts), this.el(), string(attrName)));
-};
-
-compiler2.ambiguousAttr = function(attrName, str) {
- pushStack(this.stack, helper('ambiguousAttr', this.el(), 'context', string(attrName), string(str)));
-};
-
-compiler2.helperAttr = function(attrName, name, size) {
- var prepared = prepareHelper(this.stack, size);
- pushStack(this.stack, helper('helperAttr', string(name), this.el(), string(attrName), 'context', prepared.args, hash(prepared.options)));
-};
-
-compiler2.applyAttribute = function(attrName) {
- this.push(helper('applyAttribute', this.el(), string(attrName), popStack(this.stack)));
-};
-
export { Compiler2 };
Oops, something went wrong.

0 comments on commit c945df1

Please sign in to comment.