Skip to content

Commit

Permalink
Merge pull request #2709 from LostKobrakai/multiple-event-listeners
Browse files Browse the repository at this point in the history
Allow multiple event listeners on a single node
  • Loading branch information
Rich-Harris authored May 12, 2019
2 parents 644b8a7 + abe486e commit 861c742
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/parse/state/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ export default function tag(parser: Parser) {
const type = meta_tags.has(name)
? meta_tags.get(name)
: (/[A-Z]/.test(name[0]) || name === 'svelte:self' || name === 'svelte:component') ? 'InlineComponent'
: name === 'title' && parent_is_head(parser.stack) ? 'Title'
: name === 'slot' && !parser.customElement ? 'Slot' : 'Element';
: name === 'title' && parent_is_head(parser.stack) ? 'Title'
: name === 'slot' && !parser.customElement ? 'Slot' : 'Element';

const element: Node = {
start,
Expand Down Expand Up @@ -360,14 +360,6 @@ function read_attribute(parser: Parser, unique_names: Set<string>) {

let name = parser.read_until(/(\s|=|\/|>)/);
if (!name) return null;
if (unique_names.has(name)) {
parser.error({
code: `duplicate-attribute`,
message: 'Attributes need to be unique'
}, start);
}

unique_names.add(name);

let end = parser.index;

Expand All @@ -376,6 +368,17 @@ function read_attribute(parser: Parser, unique_names: Set<string>) {
const colon_index = name.indexOf(':');
const type = colon_index !== -1 && get_directive_type(name.slice(0, colon_index));

if (unique_names.has(name)) {
parser.error({
code: `duplicate-attribute`,
message: 'Attributes need to be unique'
}, start);
}

if (type !== "EventHandler") {
unique_names.add(name);
}

let value: any[] | true = true;
if (parser.eat('=')) {
value = read_attribute_value(parser);
Expand Down Expand Up @@ -453,8 +456,8 @@ function read_attribute_value(parser: Parser) {

const regex = (
quote_mark === `'` ? /'/ :
quote_mark === `"` ? /"/ :
/(\/>|[\s"'=<>`])/
quote_mark === `"` ? /"/ :
/(\/>|[\s"'=<>`])/
);

const value = read_sequence(parser, () => !!parser.match_regex(regex));
Expand Down
14 changes: 14 additions & 0 deletions test/runtime/samples/event-handler-multiple/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export default {
html: `
<button>click me</button>
`,

async test({ assert, component, target, window }) {
const button = target.querySelector('button');
const event = new window.MouseEvent('click');

await button.dispatchEvent(event);
assert.equal(component.clickHandlerOne, 1);
assert.equal(component.clickHandlerTwo, 1);
}
};
6 changes: 6 additions & 0 deletions test/runtime/samples/event-handler-multiple/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
export let clickHandlerOne = 0;
export let clickHandlerTwo = 0;
</script>

<button on:click='{() => clickHandlerOne++}' on:click='{() => clickHandlerTwo++}'>click me</button>

0 comments on commit 861c742

Please sign in to comment.