mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
Marko v3: Allow attributes passed to createNodeForEl to be an object
This commit is contained in:
parent
3972b38f32
commit
298cc2a336
@ -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;
|
||||
|
||||
24
test/fixtures/compiler/autotest/createNodeFromEl/expected.js
vendored
Normal file
24
test/fixtures/compiler/autotest/createNodeFromEl/expected.js
vendored
Normal 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);
|
||||
1
test/fixtures/compiler/autotest/createNodeFromEl/template.marko
vendored
Normal file
1
test/fixtures/compiler/autotest/createNodeFromEl/template.marko
vendored
Normal file
@ -0,0 +1 @@
|
||||
<test-createNodeFromEl/>
|
||||
26
test/fixtures/taglib/scanned-tags/test-createNodeFromEl/code-generator.js
vendored
Normal file
26
test/fixtures/taglib/scanned-tags/test-createNodeFromEl/code-generator.js
vendored
Normal 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
|
||||
];
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user