Marko v3: Allow attributes passed to createNodeForEl to be an object

This commit is contained in:
Patrick Steele-Idem 2016-02-03 14:39:11 -07:00
parent 3972b38f32
commit 298cc2a336
4 changed files with 75 additions and 3 deletions

View File

@ -11,6 +11,7 @@ var CompileError = require('./CompileError');
var path = require('path');
var Node = require('./ast/Node');
var macros = require('./util/macros');
var extend = require('raptor-util/extend');
function getTaglibPath(taglibPath) {
if (typeof window === 'undefined') {
@ -182,13 +183,33 @@ class CompileContext {
elDef = { tagName, argument, attributes, openTagOnly, selfClosed };
}
ok(typeof tagName === 'string', 'Invalid "tagName"');
ok(attributes == null || Array.isArray(attributes), 'Invalid "attributes"');
if (!attributes) {
attributes = elDef.attributes = [];
} else if (typeof attributes === 'object') {
if (!Array.isArray(attributes)) {
attributes = elDef.attributes = Object.keys(attributes).map((attrName) => {
var attrDef = {
name: attrName
};
var val = attributes[attrName];
if (val == null) {
} if (val instanceof Node) {
attrDef.value = val;
} else {
extend(attrDef, val);
}
return attrDef;
});
}
} else {
throw new Error('Invalid attributes');
}
ok(typeof tagName === 'string', 'Invalid "tagName"');
var node;
var elNode = builder.htmlElement(elDef);
var taglibLookup = this.taglibLookup;

View File

@ -0,0 +1,24 @@
function create(__helpers) {
var str = __helpers.s,
empty = __helpers.e,
notEmpty = __helpers.ne,
escapeXml = __helpers.x,
__loadTag = __helpers.t,
test_hello = __loadTag(require("../../../taglib/test-hello/renderer"));
return function render(data, out) {
test_hello({
name: "Frank"
}, out);
test_hello({
name: "Frank"
}, out);
test_hello({
name: "Frank"
}, out);
};
}
(module.exports = require("marko").c(__filename)).c(create);

View File

@ -0,0 +1 @@
<test-createNodeFromEl/>

View File

@ -0,0 +1,26 @@
module.exports = function(elNode, codegen) {
var builder = codegen.builder;
var helloNode1 = codegen.context.createNodeForEl('test-hello', [
{
name: 'name',
value: builder.literal('Frank')
}
]);
var helloNode2 = codegen.context.createNodeForEl('test-hello', {
name: builder.literal('Frank')
});
var helloNode3 = codegen.context.createNodeForEl('test-hello', {
name: {
value: builder.literal('Frank')
}
});
return [
helloNode1,
helloNode2,
helloNode3
];
};