Fixes #122 Don't allow invalid attributes when using shorthand

This commit is contained in:
Patrick Steele-Idem 2015-08-17 16:50:27 -06:00
parent 6e406a0b8f
commit dd68358707
8 changed files with 79 additions and 3 deletions

View File

@ -65,7 +65,7 @@ Taglib.prototype = {
ok(arguments.length === 1, 'Invalid args');
ok(tag.name, '"tag.name" is required');
this.tags[tag.name] = tag;
tag.taglibId = this.id;
tag.taglibId = this.id || this.path;
},
addTextTransformer: function (transformer) {
this.textTransformers.push(transformer);

View File

@ -376,6 +376,20 @@ exports.isSupportedProperty = function(name) {
return TagHandlers.prototype.hasOwnProperty(name);
};
function hasAttributes(tagProps) {
if (tagProps.attributes != null) {
return true;
}
for (var name in tagProps) {
if (tagProps.hasOwnProperty(name) && name.startsWith('@')) {
return true;
}
}
return true;
}
function loadTag(tagProps, path, taglib, dirname) {
ok(tagProps);
ok(typeof path === 'string');
@ -384,7 +398,9 @@ function loadTag(tagProps, path, taglib, dirname) {
var tag = new Taglib.Tag(taglib);
if (tagProps.attributes == null) {
if (!hasAttributes(tagProps)) {
// allow any attributes if no attributes are declared
tagProps.attributes = {
'*': 'string'

View File

@ -119,5 +119,8 @@
"<test-nested-tags-tabs>": "./taglib/test-nested-tags-tabs/marko-tag.json",
"<test-nested-tags-overlay>": "./taglib/test-nested-tags-overlay/marko-tag.json",
"tags-dir": "./taglib/scanned-tags",
"taglib-imports": ["./package.json"]
"taglib-imports": ["./package.json"],
"<test-invalid-attr>": {
"@foo": "string"
}
}

View File

@ -0,0 +1,17 @@
<for>
Missing each attribute
</for>
<for each="item">
</for>
<for each="item in items" invalid="true">
Invalid attribute
</for>
<for each="item in items" separator="${;">
Invalid separator
</for>
<div for="item in items; invalid=true">
</div>

View File

@ -0,0 +1,22 @@
var expect = require('chai').expect;
exports.templateData = {};
exports.options = {
handleCompileError: function(e) {
expect(e.toString()).to.contain('template.marko:8:0');
expect(e.toString()).to.contain('does not support attribute "invalid"');
expect(e.toString()).to.contain('template.marko:12:0');
expect(e.toString()).to.contain('Invalid attribute value of "${;"');
expect(e.toString()).to.contain('template.marko:1:0');
expect(e.toString()).to.contain('"each" attribute is required');
expect(e.toString()).to.contain('template.marko:5:0');
expect(e.toString()).to.contain('Invalid each attribute of "item"');
expect(e.toString()).to.contain('template.marko:16:0');
expect(e.toString()).to.contain('Invalid for attribute of "item in items; invalid=true"');
}
};

View File

@ -0,0 +1 @@
<test-invalid-attr invalid="true">

View File

@ -0,0 +1,11 @@
var expect = require('chai').expect;
exports.templateData = {};
exports.options = {
handleCompileError: function(e) {
expect(e.toString()).to.contain('template.marko:1:0');
expect(e.toString()).to.contain('The tag "test-invalid-attr" in taglib');
expect(e.toString()).to.contain('does not support attribute "invalid"');
}
};

View File

@ -30,10 +30,12 @@ function createTestRender(options) {
var out = options.out || new AsyncWriter(new StringBuilder());
var template;
var hadError = false;
try {
template = marko.load(templatePath);
} catch(err) {
hadError = true;
if (options.handleCompileError) {
try {
options.handleCompileError(err);
@ -47,6 +49,10 @@ function createTestRender(options) {
return;
}
if (options.handleCompileError && !hadError) {
throw new Error('Error expected for test case, but no error occurred. Template: ' + templatePath);
}
template.render(templateData, out)
.on('finish', function() {
var output = out.getOutput();