mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
Fixes #122 Don't allow invalid attributes when using shorthand
This commit is contained in:
parent
6e406a0b8f
commit
dd68358707
@ -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);
|
||||
|
||||
@ -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'
|
||||
|
||||
5
test/fixtures/marko-taglib.json
vendored
5
test/fixtures/marko-taglib.json
vendored
@ -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"
|
||||
}
|
||||
}
|
||||
17
test/fixtures/templates/error-for/template.marko
vendored
Normal file
17
test/fixtures/templates/error-for/template.marko
vendored
Normal 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>
|
||||
22
test/fixtures/templates/error-for/test.js
vendored
Normal file
22
test/fixtures/templates/error-for/test.js
vendored
Normal 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"');
|
||||
}
|
||||
};
|
||||
1
test/fixtures/templates/error-invalid-attr/template.marko
vendored
Normal file
1
test/fixtures/templates/error-invalid-attr/template.marko
vendored
Normal file
@ -0,0 +1 @@
|
||||
<test-invalid-attr invalid="true">
|
||||
11
test/fixtures/templates/error-invalid-attr/test.js
vendored
Normal file
11
test/fixtures/templates/error-invalid-attr/test.js
vendored
Normal 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"');
|
||||
}
|
||||
};
|
||||
@ -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();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user