Skip to content

Allow multiple event listeners on a single node #2709

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>