Skip to content

Commit

Permalink
Merge pull request sveltejs#1679 from sveltejs/sveltejsgh-1676
Browse files Browse the repository at this point in the history
Allows actions to use any expression type
  • Loading branch information
Rich-Harris authored Aug 24, 2018
2 parents f38bdaa + ba5ede5 commit e450c30
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/parse/read/directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const DIRECTIVES: Record<string, {
attribute(start, end, type, name, expression) {
return { start, end, type, name, expression };
},
allowedExpressionTypes: ['Identifier', 'MemberExpression', 'ObjectExpression', 'Literal', 'CallExpression'],
allowedExpressionTypes: ['*'],
error: 'Data passed to actions must be an identifier (e.g. `foo`), a member expression ' +
'(e.g. `foo.bar` or `foo[baz]`), a method call (e.g. `foo()`), or a literal (e.g. `true` or `\'a string\'`'
},
Expand Down Expand Up @@ -163,7 +163,8 @@ export function readDirective(

try {
expression = readExpression(parser, expressionStart, quoteMark);
if (directive.allowedExpressionTypes.indexOf(expression.type) === -1) {
const allowed = directive.allowedExpressionTypes;
if (allowed[0] !== '*' && allowed.indexOf(expression.type) === -1) {
parser.error({
code: `invalid-directive-value`,
message: directive.error
Expand Down
20 changes: 20 additions & 0 deletions test/runtime/samples/action-ternary-template/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export default {
data: {
target: 'World!',
display: true,
},

html: `
<h1></h1>
`,

test ( assert, component, target, window ) {
const header = target.querySelector( 'h1' );
const eventClick = new window.MouseEvent( 'click' );

header.dispatchEvent( eventClick );
assert.htmlEqual( target.innerHTML, `
<h1>Hello World!</h1>
` );
}
};
21 changes: 21 additions & 0 deletions test/runtime/samples/action-ternary-template/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<h1 use:insert="display ? `Hello ${target}` : ''"></h1>

<script>
export default {
actions: {
insert(node, text) {

function onClick() {
node.textContent = text;
}
node.addEventListener('click', onClick);

return {
destroy() {
node.removeEventListener('click', onClick);
}
}
}
}
}
</script>

0 comments on commit e450c30

Please sign in to comment.