Repository navigation
Support DOM event listeners and bubbling #40
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
Changes from all commits
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 |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@clack/ui': minor | ||
| '@bomb.sh/web-platform-tests': patch | ||
| --- | ||
|
|
||
| Support native event listeners and bubbling through the UI's node tree. |
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
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
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
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
61 changes: 61 additions & 0 deletions
61
packages/web-platform-tests/src/wpt/dom/events/EventListener-handleEvent.html.ts
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,61 @@ | ||
| import type { CompatibilityContext } from '../../../shared/context.ts'; | ||
|
|
||
| export function register(context: CompatibilityContext): void { | ||
| const { expect } = context.runner; | ||
|
|
||
| context.test('calls `handleEvent` method of `EventListener`', ({ document, Event, step }) => { | ||
| const type = 'foo'; | ||
| const target = document.createElement('div'); | ||
| const eventListener = { | ||
| handleEvent: function (evt: Event) { | ||
| const that = this; | ||
| step(() => { | ||
| expect(evt.type).toBe(type); | ||
| expect(evt.target).toBe(target); | ||
| expect(evt.srcElement).toBe(target); | ||
| expect(that).toBe(eventListener); | ||
| })(); | ||
| }, | ||
| }; | ||
|
|
||
| target.addEventListener(type, eventListener); | ||
| target.dispatchEvent(new Event(type)); | ||
| }); | ||
|
|
||
| context.test('performs `Get` every time event is dispatched', ({ document, Event }) => { | ||
| const type = 'foo'; | ||
| const target = document.createElement('div'); | ||
| let calls = 0; | ||
|
|
||
| target.addEventListener(type, { | ||
| get handleEvent() { | ||
| calls++; | ||
| return function () {}; | ||
| }, | ||
| }); | ||
|
|
||
| expect(calls).toBe(0); | ||
| target.dispatchEvent(new Event(type)); | ||
| target.dispatchEvent(new Event(type)); | ||
| expect(calls).toBe(2); | ||
| }); | ||
|
|
||
| context.test( | ||
| "doesn't call `handleEvent` method on callable `EventListener`", | ||
| ({ document, Event, step }) => { | ||
| const type = 'foo'; | ||
| const target = document.createElement('div'); | ||
| let calls = 0; | ||
| const eventListener = function () { | ||
| calls++; | ||
| }; | ||
| eventListener.handleEvent = step(() => { | ||
| expect.unreachable('`handleEvent` method should not be called on functions'); | ||
| }); | ||
|
|
||
| target.addEventListener(type, eventListener); | ||
| target.dispatchEvent(new Event(type)); | ||
| expect(calls).toBe(1); | ||
| }, | ||
| ); | ||
| } |
95 changes: 95 additions & 0 deletions
95
packages/web-platform-tests/src/wpt/dom/events/remove-all-listeners.html.ts
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,95 @@ | ||
| import type { CompatibilityContext } from '../../../shared/context.ts'; | ||
|
|
||
| export function register(context: CompatibilityContext): void { | ||
| const { expect } = context.runner; | ||
|
|
||
| context.test( | ||
| 'Removing all listeners and then adding a new one should work.', | ||
| ({ document, Event }) => { | ||
| const type = 'foo'; | ||
| const target = document.createElement('div'); | ||
|
|
||
| let listener1CallCount = 0; | ||
| let listener2CallCount = 0; | ||
| let listener3CallCount = 0; | ||
| function listener1(): void { | ||
| listener1CallCount++; | ||
| target.removeEventListener(type, listener1); | ||
| target.removeEventListener(type, listener2); | ||
| target.addEventListener(type, listener3); | ||
| } | ||
| function listener2(): void { | ||
| listener2CallCount++; | ||
| } | ||
| function listener3(): void { | ||
| listener3CallCount++; | ||
| } | ||
|
|
||
| target.addEventListener(type, listener1); | ||
| target.addEventListener(type, listener2); | ||
|
|
||
| // Dispatch the event. Only listener1 should be called because | ||
| // it removes listener2. And listener3 is added when we've already | ||
| // started iterating, so it shouldn't be called either. | ||
| target.dispatchEvent(new Event(type)); | ||
| expect(listener1CallCount).toBe(1); | ||
| expect(listener2CallCount).toBe(0); | ||
| expect(listener3CallCount).toBe(0); | ||
|
|
||
| // Now that only listener3 is set, dispatch another event. Only | ||
| // listener3 should be called. | ||
| target.dispatchEvent(new Event(type)); | ||
| expect(listener1CallCount).toBe(1); | ||
| expect(listener2CallCount).toBe(0); | ||
| expect(listener3CallCount).toBe(1); | ||
| }, | ||
| ); | ||
|
|
||
| context.test('Nested usage of once listeners should work.', ({ document, Event, step }) => { | ||
| const type = 'foo'; | ||
| const target = document.createElement('div'); | ||
|
|
||
| let listener1CallCount = 0; | ||
| let listener2CallCount = 0; | ||
| let listener3CallCount = 0; | ||
| const listener1 = step(function (): void { | ||
| listener1CallCount++; | ||
| // Recursively dispatch another event from this listener. | ||
| // This will only call listener2 because listener1 is a "once" listener. | ||
| target.dispatchEvent(new Event(type)); | ||
| expect(listener1CallCount).toBe(1); | ||
| expect(listener2CallCount).toBe(1); | ||
| expect(listener3CallCount).toBe(0); | ||
|
|
||
| // Now all listeners are removed - the two "once" listeners have already both | ||
| // been called once. Add another listener. | ||
| target.addEventListener(type, listener3); | ||
| }); | ||
| function listener2(): void { | ||
| listener2CallCount++; | ||
| } | ||
| function listener3(): void { | ||
| listener3CallCount++; | ||
| } | ||
|
|
||
| // Add two "once" listeners. | ||
| target.addEventListener(type, listener1, { once: true }); | ||
| target.addEventListener(type, listener2, { once: true }); | ||
|
|
||
| // Dispatch the event. | ||
| target.dispatchEvent(new Event(type)); | ||
|
|
||
| // The listener call counts should still match what they were | ||
| // at the end of listener1. | ||
| expect(listener1CallCount).toBe(1); | ||
| expect(listener2CallCount).toBe(1); | ||
| expect(listener3CallCount).toBe(0); | ||
|
|
||
| // Now that only listener3 is set, dispatch another event. Only | ||
| // listener3 should be called. | ||
| target.dispatchEvent(new Event(type)); | ||
| expect(listener1CallCount).toBe(1); | ||
| expect(listener2CallCount).toBe(1); | ||
| expect(listener3CallCount).toBe(1); | ||
| }); | ||
| } |
Oops, something went wrong.
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.
given its about as much code to re-declare these as it is to just have our own constants, what do you think about having our own constants?
i.e.
const EVENT_NONE = 0etcotherwise i would try solve this through augmentation rather than casting. augment the global interface/class with the extra members etc