-
Notifications
You must be signed in to change notification settings - Fork 92
Extend EventTarget to support event bubbling #84
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
Show all changes
29 commits
Select commit
Hold shift + click to select a range
d8c5fa3
Extend EventTarget to support event bubbling + tests
9967ace
Event bubbling: unit tests, method name and function return
fe0b846
Event bubbling: _getParent protected
56a677b
Event bubbling: fix for Node 16.5
f546696
Event bubbling: extend Event to fix Node's stopImmediatePropagation
8e4f67c
Event bubbling: implement comment + super method check
57ae688
Extend EventTarget to support event bubbling + tests
64a74d4
Event bubbling: unit tests, method name and function return
023cc31
Event bubbling: _getParent protected
3754826
Event bubbling: fix for Node 16.5
88d4cd8
Event bubbling: extend Event to fix Node's stopImmediatePropagation
26545dc
Event bubbling: implement comment + super method check
495587a
Event bubbling: package update + improved test
3d35561
Merge branch 'event-bubbling' of https://github.com/mikemadest/linked…
88824dc
Extend EventTarget to support event bubbling + tests
67024da
Adding NamedNodeMap to global export (#85)
mikemadest 26a6d7f
Event bubbling: unit tests, method name and function return
86169fa
Event bubbling: _getParent protected
96df7a8
Event bubbling: fix for Node 16.5
89de535
Event bubbling: extend Event to fix Node's stopImmediatePropagation
5f6c890
Event bubbling: implement comment + super method check
4b1c1e6
Extend EventTarget to support event bubbling + tests
bc734cf
Event bubbling: unit tests, method name and function return
e941912
Event bubbling: _getParent protected
3b909c4
Event bubbling: fix for Node 16.5
feafc8c
Event bubbling: extend Event to fix Node's stopImmediatePropagation
05b5f21
Event bubbling: implement comment + super method check
45d58f8
Event bubbling: package update + improved test
e7253bc
Merge branch 'event-bubbling' of https://github.com/mikemadest/linked…
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
const assert = require('../assert.js').for('EventTarget'); | ||
|
||
const { parseHTML } = global[Symbol.for('linkedom')]; | ||
|
||
const { Event, document, EventTarget } = parseHTML( | ||
'<html><div id="container"><button id="buttonTarget" type="button">Click me!</button></div></html>', | ||
); | ||
|
||
// check basics | ||
|
||
let callCount = 0; | ||
const basicHandler = () => { | ||
callCount++; | ||
}; | ||
|
||
|
||
const eventTarget = new EventTarget(); | ||
eventTarget.addEventListener('foo', basicHandler); | ||
eventTarget.dispatchEvent(new Event('foo')); | ||
assert(callCount, 1, 'basicHandler should have been called'); | ||
|
||
assert( | ||
eventTarget.dispatchEvent(new Event('click')), | ||
true, | ||
'Dispatching an event type with no handlers should return true', | ||
); | ||
assert(callCount, 1, 'Dispatching an event type should only call appropriate listeners'); | ||
|
||
eventTarget.removeEventListener('foo', basicHandler); | ||
eventTarget.dispatchEvent(new Event('foo')); | ||
assert(callCount, 1, 'basicHandler should not have been called after being removed'); | ||
|
||
assert(eventTarget._getParent(), null, 'getParent should return null'); | ||
|
||
|
||
// check propagation now | ||
callCount = 0; | ||
const buttonTarget = document.getElementById('buttonTarget'); | ||
const containerTarget = document.getElementById('container'); | ||
const bodyTarget = document; | ||
buttonTarget.addEventListener('click', basicHandler, { once: true }); | ||
containerTarget.addEventListener('click', basicHandler, { once: true }); | ||
bodyTarget.addEventListener('click', basicHandler, { once: true }); | ||
|
||
buttonTarget.dispatchEvent(new Event('click', { bubbles: true })); | ||
assert(callCount, 3, 'Event bubbling, listener should be called 3 times'); | ||
|
||
|
||
// ensure once removed listeners | ||
buttonTarget.dispatchEvent(new Event('click', { bubbles: true })); | ||
assert(callCount, 3, 'listeners should only have been called once then removed'); | ||
|
||
// check no bubbling | ||
callCount = 0; | ||
buttonTarget.addEventListener('click', basicHandler, { once: true }); | ||
containerTarget.addEventListener('click', basicHandler, { once: true }); | ||
bodyTarget.addEventListener('click', basicHandler, { once: true }); | ||
|
||
buttonTarget.dispatchEvent(new Event('click', { bubbles: false })); | ||
assert(callCount, 1, 'Expect listener to be called once'); | ||
|
||
// check stop propagation | ||
buttonTarget.addEventListener( | ||
'click', | ||
(event) => { | ||
event.stopPropagation(); | ||
callCount++; | ||
}, | ||
{ | ||
once: true, | ||
}, | ||
); | ||
containerTarget.addEventListener('click', basicHandler, { once: true }); | ||
|
||
callCount = 0; | ||
buttonTarget.dispatchEvent(new Event('click', { bubbles: true })); | ||
assert(callCount, 1, 'listener should be called once before stopping bubbling'); | ||
|
||
// check stop immediate propagation | ||
buttonTarget.addEventListener('click', () => callCount++, { once: true }); | ||
buttonTarget.addEventListener( | ||
'click', | ||
(event) => { | ||
event.stopImmediatePropagation(); | ||
callCount++; | ||
}, | ||
{ | ||
once: true, | ||
}, | ||
); | ||
buttonTarget.addEventListener('click', () => callCount++, { once: true }); | ||
containerTarget.addEventListener('click', basicHandler, { once: true }); | ||
WebReflection marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
callCount = 0; | ||
buttonTarget.dispatchEvent(new Event('click', { bubbles: true })); | ||
assert(callCount, 2, '2 listeners should be called before stopping'); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,11 @@ | ||
export { EventTarget }; | ||
export { DOMEventTarget as EventTarget }; | ||
/** | ||
* @implements globalThis.EventTarget | ||
*/ | ||
declare class DOMEventTarget implements globalThis.EventTarget { | ||
/** | ||
* @protected | ||
*/ | ||
protected _getParent(): any; | ||
dispatchEvent(event: any): any; | ||
} |
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 |
---|---|---|
@@ -1,32 +1,7 @@ | ||
export { GlobalEvent as Event }; | ||
export { DOMEvent as Event }; | ||
/** | ||
* @implements globalThis.Event | ||
*/ | ||
declare const GlobalEvent: { | ||
new (type: string, eventInitDict?: EventInit): Event; | ||
prototype: Event; | ||
readonly AT_TARGET: number; | ||
readonly BUBBLING_PHASE: number; | ||
readonly CAPTURING_PHASE: number; | ||
readonly NONE: number; | ||
} | { | ||
new (type: any, eventInitDict?: {}): { | ||
type: any; | ||
bubbles: boolean; | ||
cancelable: boolean; | ||
eventPhase: number; | ||
timeStamp: number; | ||
defaultPrevented: boolean; | ||
originalTarget: any; | ||
returnValue: any; | ||
srcElement: any; | ||
target: any; | ||
readonly BUBBLING_PHASE: number; | ||
readonly CAPTURING_PHASE: number; | ||
preventDefault(): void; | ||
stopPropagation(): void; | ||
stopImmediatePropagation(): void; | ||
}; | ||
readonly BUBBLING_PHASE: number; | ||
readonly CAPTURING_PHASE: number; | ||
}; | ||
declare class DOMEvent implements globalThis.Event { | ||
stopImmediatePropagation(): void; | ||
} |
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
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.
Uh oh!
There was an error while loading. Please reload this page.