-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Rich-Harris
merged 3 commits into
sveltejs:master
from
LostKobrakai:multiple-event-listeners
May 12, 2019
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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, | ||
|
|
@@ -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; | ||
|
|
||
|
|
@@ -376,6 +368,15 @@ 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) && type != "EventHandler") { | ||
| parser.error({ | ||
| code: `duplicate-attribute`, | ||
| message: 'Attributes need to be unique' | ||
| }, start); | ||
| } | ||
|
|
||
| unique_names.add(name); | ||
|
||
|
|
||
| let value: any[] | true = true; | ||
| if (parser.eat('=')) { | ||
| value = read_attribute_value(parser); | ||
|
|
@@ -453,8 +454,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)); | ||
|
|
||
1 change: 1 addition & 0 deletions
1
test/parser/samples/non-unique-attribute-event-handler/input.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <button on:click="{() => visible = !visible}" on:click="{() => ajax = false}"></button> |
96 changes: 96 additions & 0 deletions
96
test/parser/samples/non-unique-attribute-event-handler/output.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| { | ||
| "html": { | ||
| "start": 0, | ||
| "end": 87, | ||
| "type": "Fragment", | ||
| "children": [ | ||
| { | ||
| "start": 0, | ||
| "end": 87, | ||
| "type": "Element", | ||
| "name": "button", | ||
| "attributes": [ | ||
| { | ||
| "start": 8, | ||
| "end": 45, | ||
| "type": "EventHandler", | ||
| "name": "click", | ||
| "modifiers": [], | ||
| "expression": { | ||
| "type": "ArrowFunctionExpression", | ||
| "start": 19, | ||
| "end": 43, | ||
| "id": null, | ||
| "expression": true, | ||
| "generator": false, | ||
| "async": false, | ||
| "params": [], | ||
| "body": { | ||
| "type": "AssignmentExpression", | ||
| "start": 25, | ||
| "end": 43, | ||
| "operator": "=", | ||
| "left": { | ||
| "type": "Identifier", | ||
| "start": 25, | ||
| "end": 32, | ||
| "name": "visible" | ||
| }, | ||
| "right": { | ||
| "type": "UnaryExpression", | ||
| "start": 35, | ||
| "end": 43, | ||
| "operator": "!", | ||
| "prefix": true, | ||
| "argument": { | ||
| "type": "Identifier", | ||
| "start": 36, | ||
| "end": 43, | ||
| "name": "visible" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| { | ||
| "start": 46, | ||
| "end": 77, | ||
| "type": "EventHandler", | ||
| "name": "click", | ||
| "modifiers": [], | ||
| "expression": { | ||
| "type": "ArrowFunctionExpression", | ||
| "start": 57, | ||
| "end": 75, | ||
| "id": null, | ||
| "expression": true, | ||
| "generator": false, | ||
| "async": false, | ||
| "params": [], | ||
| "body": { | ||
| "type": "AssignmentExpression", | ||
| "start": 63, | ||
| "end": 75, | ||
| "operator": "=", | ||
| "left": { | ||
| "type": "Identifier", | ||
| "start": 63, | ||
| "end": 67, | ||
| "name": "ajax" | ||
| }, | ||
| "right": { | ||
| "type": "Literal", | ||
| "start": 70, | ||
| "end": 75, | ||
| "value": false, | ||
| "raw": "false" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ], | ||
| "children": [] | ||
| } | ||
| ] | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not that it would really matter here, but we are trying to use non-coercion comparisons everywhere.