Skip to content

Commit

Permalink
Merge pull request #613 from sveltejs/gh-612
Browse files Browse the repository at this point in the history
Sanitize event name in handler function name
  • Loading branch information
Rich-Harris authored Jun 1, 2017
2 parents 9bf9b51 + 9112671 commit 4cd5303
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/generators/dom/visitors/Element/EventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ export default function visitEventHandler ( generator: DomGenerator, block: Bloc

// get a name for the event handler that is globally unique
// if hoisted, locally unique otherwise
const handlerName = shouldHoist ?
generator.getUniqueName( `${name}_handler` ) :
block.getUniqueName( `${name}_handler` );
const handlerName = ( shouldHoist ? generator : block ).getUniqueName( `${name.replace( /[^a-zA-Z0-9_$]/g, '_' )}_handler` );

// create the handler body
const handlerBody = deindent`
Expand All @@ -71,7 +69,7 @@ export default function visitEventHandler ( generator: DomGenerator, block: Bloc
`;

if ( shouldHoist ) {
generator.blocks.push({
generator.blocks.push(<Block>{
render: () => handler
});
} else {
Expand All @@ -91,4 +89,4 @@ export default function visitEventHandler ( generator: DomGenerator, block: Bloc
${generator.helper( 'removeEventListener' )}( ${state.parentNode}, '${name}', ${handlerName} );
` );
}
}
}
21 changes: 21 additions & 0 deletions test/runtime/samples/event-handler-sanitize/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export default {
html: `
<div>toggle</div>
`,

test ( assert, component, target, window ) {
const div = target.querySelector( 'div' );
const event = new window.MouseEvent( 'some-event' );

div.dispatchEvent( event );
assert.htmlEqual( target.innerHTML, `
<div>toggle</div>
<p>hello!</p>
` );

div.dispatchEvent( event );
assert.htmlEqual( target.innerHTML, `
<div>toggle</div>
` );
}
};
5 changes: 5 additions & 0 deletions test/runtime/samples/event-handler-sanitize/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div on:some-event='set({ visible: !visible })'>toggle</div>

{{#if visible}}
<p>hello!</p>
{{/if}}

0 comments on commit 4cd5303

Please sign in to comment.