Skip to content

Commit

Permalink
Allows actions to use any expression type
Browse files Browse the repository at this point in the history
Allow any expression to pass data to an action. Added a test for a ternary statement and a string template.
Fixes #1676
  • Loading branch information
jacwright committed Aug 23, 2018
1 parent f38bdaa commit ba5ede5
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 ba5ede5

Please sign in to comment.