Skip to content

Commit 861c742

Browse files
authored
Merge pull request #2709 from LostKobrakai/multiple-event-listeners
Allow multiple event listeners on a single node
2 parents 644b8a7 + abe486e commit 861c742

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

src/parse/state/tag.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ export default function tag(parser: Parser) {
131131
const type = meta_tags.has(name)
132132
? meta_tags.get(name)
133133
: (/[A-Z]/.test(name[0]) || name === 'svelte:self' || name === 'svelte:component') ? 'InlineComponent'
134-
: name === 'title' && parent_is_head(parser.stack) ? 'Title'
135-
: name === 'slot' && !parser.customElement ? 'Slot' : 'Element';
134+
: name === 'title' && parent_is_head(parser.stack) ? 'Title'
135+
: name === 'slot' && !parser.customElement ? 'Slot' : 'Element';
136136

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

361361
let name = parser.read_until(/(\s|=|\/|>)/);
362362
if (!name) return null;
363-
if (unique_names.has(name)) {
364-
parser.error({
365-
code: `duplicate-attribute`,
366-
message: 'Attributes need to be unique'
367-
}, start);
368-
}
369-
370-
unique_names.add(name);
371363

372364
let end = parser.index;
373365

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

371+
if (unique_names.has(name)) {
372+
parser.error({
373+
code: `duplicate-attribute`,
374+
message: 'Attributes need to be unique'
375+
}, start);
376+
}
377+
378+
if (type !== "EventHandler") {
379+
unique_names.add(name);
380+
}
381+
379382
let value: any[] | true = true;
380383
if (parser.eat('=')) {
381384
value = read_attribute_value(parser);
@@ -453,8 +456,8 @@ function read_attribute_value(parser: Parser) {
453456

454457
const regex = (
455458
quote_mark === `'` ? /'/ :
456-
quote_mark === `"` ? /"/ :
457-
/(\/>|[\s"'=<>`])/
459+
quote_mark === `"` ? /"/ :
460+
/(\/>|[\s"'=<>`])/
458461
);
459462

460463
const value = read_sequence(parser, () => !!parser.match_regex(regex));
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export default {
2+
html: `
3+
<button>click me</button>
4+
`,
5+
6+
async test({ assert, component, target, window }) {
7+
const button = target.querySelector('button');
8+
const event = new window.MouseEvent('click');
9+
10+
await button.dispatchEvent(event);
11+
assert.equal(component.clickHandlerOne, 1);
12+
assert.equal(component.clickHandlerTwo, 1);
13+
}
14+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
export let clickHandlerOne = 0;
3+
export let clickHandlerTwo = 0;
4+
</script>
5+
6+
<button on:click='{() => clickHandlerOne++}' on:click='{() => clickHandlerTwo++}'>click me</button>

0 commit comments

Comments
 (0)