mirror of
https://github.com/bobwen-dev/react-templates
synced 2025-04-12 00:56:39 +02:00
Restrict attributes on rt-virtual (fix #152)
This commit is contained in:
parent
4559fdf744
commit
9b8d15a530
@ -366,7 +366,7 @@ function convertHtmlToReact(node, context) {
|
|||||||
if (node.attribs[ifAttr]) {
|
if (node.attribs[ifAttr]) {
|
||||||
validateIfAttribute(node, context, data);
|
validateIfAttribute(node, context, data);
|
||||||
data.condition = node.attribs[ifAttr].trim();
|
data.condition = node.attribs[ifAttr].trim();
|
||||||
if (!node.attribs.key) {
|
if (!node.attribs.key && node.name !== virtualNode) {
|
||||||
_.set(node, ['attribs', 'key'], `${node.startIndex}`);
|
_.set(node, ['attribs', 'key'], `${node.startIndex}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -385,13 +385,22 @@ function convertHtmlToReact(node, context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// provide a key to virtual node children if missing
|
if (node.name === virtualNode) {
|
||||||
if (node.name === virtualNode && node.children.length > 1) {
|
const invalidAttributes = _.without(_.keys(node.attribs), scopeAttr, ifAttr, repeatAttr);
|
||||||
_(node.children)
|
if (invalidAttributes.length > 0) {
|
||||||
.reject('attribs.key')
|
throw RTCodeError.build(context, node, "<rt-virtual> may not contain attributes other than 'rt-scope', 'rt-if' and 'rt-repeat'");
|
||||||
.forEach((child, i) => {
|
}
|
||||||
_.set(child, ['attribs', 'key'], `${node.startIndex}${i}`);
|
|
||||||
});
|
// provide a key to virtual node children if missing
|
||||||
|
if (node.children.length > 1) {
|
||||||
|
_(node.children)
|
||||||
|
.reject('attribs.key')
|
||||||
|
.forEach((child, i) => {
|
||||||
|
if (child.type === 'tag' && child.name !== virtualNode) {
|
||||||
|
_.set(child, ['attribs', 'key'], `${node.startIndex}${i}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const children = _.map(node.children, child => {
|
const children = _.map(node.children, child => {
|
||||||
|
5
test/data/invalid/invalid-virtual-2.rt
Normal file
5
test/data/invalid/invalid-virtual-2.rt
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<div>
|
||||||
|
<rt-virtual rt-scope="'42' as fortytwo" onClick={console.log}>
|
||||||
|
The answer is {fortytwo}
|
||||||
|
</rt-virtual>
|
||||||
|
</div>
|
@ -34,15 +34,14 @@ module.exports = {
|
|||||||
{file: 'invalid-brace.rt', issue: new RTCodeError('Unexpected end of input', 128, 163, 5, 11)},
|
{file: 'invalid-brace.rt', issue: new RTCodeError('Unexpected end of input', 128, 163, 5, 11)},
|
||||||
{file: 'invalid-style-1.rt', issue: new RTCodeError('Unexpected token ILLEGAL', 10, 39, 2, 5)},
|
{file: 'invalid-style-1.rt', issue: new RTCodeError('Unexpected token ILLEGAL', 10, 39, 2, 5)},
|
||||||
{file: 'invalid-style-2.rt', issue: new RTCodeError('style attribute keys cannot contain { } expressions', 35, 68, 2, 5)},
|
{file: 'invalid-style-2.rt', issue: new RTCodeError('style attribute keys cannot contain { } expressions', 35, 68, 2, 5)},
|
||||||
{file: 'invalid-virtual.rt', issue: new RTCodeError('Document should not have <rt-virtual> as root element', 0, 60, 1, 1)}
|
{file: 'invalid-virtual-1.rt', issue: new RTCodeError('Document should not have <rt-virtual> as root element', 0, 60, 1, 1)},
|
||||||
|
{file: 'invalid-virtual-2.rt', issue: new RTCodeError("<rt-virtual> may not contain attributes other than 'rt-scope', 'rt-if' and 'rt-repeat'", 9, 119, 2, 4)}
|
||||||
];
|
];
|
||||||
|
|
||||||
test('invalid tests', t => {
|
test('invalid tests', t => {
|
||||||
t.plan(invalidFiles.length);
|
t.plan(invalidFiles.length);
|
||||||
|
|
||||||
invalidFiles.forEach(check);
|
invalidFiles.forEach(testFile => {
|
||||||
|
|
||||||
function check(testFile) {
|
|
||||||
const filename = path.join(dataPath, testFile.file);
|
const filename = path.join(dataPath, testFile.file);
|
||||||
const html = testUtils.readFileNormalized(filename);
|
const html = testUtils.readFileNormalized(filename);
|
||||||
let error = null;
|
let error = null;
|
||||||
@ -52,7 +51,7 @@ module.exports = {
|
|||||||
error = e;
|
error = e;
|
||||||
}
|
}
|
||||||
t.deepEqual(omitStack(error), omitStack(testFile.issue), 'Expect convertTemplateToReact to throw an error');
|
t.deepEqual(omitStack(error), omitStack(testFile.issue), 'Expect convertTemplateToReact to throw an error');
|
||||||
}
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user