Skip to content
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

Extend EventTarget to support event bubbling #84

Merged
merged 29 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
d8c5fa3
Extend EventTarget to support event bubbling + tests
Jul 18, 2021
9967ace
Event bubbling: unit tests, method name and function return
Jul 19, 2021
fe0b846
Event bubbling: _getParent protected
Jul 19, 2021
56a677b
Event bubbling: fix for Node 16.5
Jul 19, 2021
f546696
Event bubbling: extend Event to fix Node's stopImmediatePropagation
Jul 19, 2021
8e4f67c
Event bubbling: implement comment + super method check
Jul 19, 2021
57ae688
Extend EventTarget to support event bubbling + tests
Jul 18, 2021
64a74d4
Event bubbling: unit tests, method name and function return
Jul 19, 2021
023cc31
Event bubbling: _getParent protected
Jul 19, 2021
3754826
Event bubbling: fix for Node 16.5
Jul 19, 2021
88d4cd8
Event bubbling: extend Event to fix Node's stopImmediatePropagation
Jul 19, 2021
26545dc
Event bubbling: implement comment + super method check
Jul 19, 2021
495587a
Event bubbling: package update + improved test
Jul 20, 2021
3d35561
Merge branch 'event-bubbling' of https://github.com/mikemadest/linked…
Jul 20, 2021
88824dc
Extend EventTarget to support event bubbling + tests
Jul 18, 2021
67024da
Adding NamedNodeMap to global export (#85)
mikemadest Jul 19, 2021
26a6d7f
Event bubbling: unit tests, method name and function return
Jul 19, 2021
86169fa
Event bubbling: _getParent protected
Jul 19, 2021
96df7a8
Event bubbling: fix for Node 16.5
Jul 19, 2021
89de535
Event bubbling: extend Event to fix Node's stopImmediatePropagation
Jul 19, 2021
5f6c890
Event bubbling: implement comment + super method check
Jul 19, 2021
4b1c1e6
Extend EventTarget to support event bubbling + tests
Jul 18, 2021
bc734cf
Event bubbling: unit tests, method name and function return
Jul 19, 2021
e941912
Event bubbling: _getParent protected
Jul 19, 2021
3b909c4
Event bubbling: fix for Node 16.5
Jul 19, 2021
feafc8c
Event bubbling: extend Event to fix Node's stopImmediatePropagation
Jul 19, 2021
05b5f21
Event bubbling: implement comment + super method check
Jul 19, 2021
45d58f8
Event bubbling: package update + improved test
Jul 20, 2021
e7253bc
Merge branch 'event-bubbling' of https://github.com/mikemadest/linked…
Jul 21, 2021
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
12 changes: 6 additions & 6 deletions cjs/interface/event-target.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ const EventTarget = (m => /* c8 ignore start */ m.__esModule ? m.default : m /*
* @implements globalThis.EventTarget
*/
class DOMEventTarget extends EventTarget {
getParent() {
_getParent() {
return null;
}

dispatchEvent(event) {
super.dispatchEvent(event);
const dispatched = super.dispatchEvent(event);

// intentionally simplified, specs imply way more code: https://dom.spec.whatwg.org/#event-path
if (event.bubbles && !event._stopPropagationFlag) {
const parent = this.getParent();
if (dispatched && event.bubbles && !event._stopPropagationFlag) {
const parent = this._getParent();
if (parent && parent.dispatchEvent)
parent.dispatchEvent(event);
return parent.dispatchEvent(event);
}
return true;
return dispatched;
}
}

Expand Down
2 changes: 1 addition & 1 deletion cjs/interface/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class Node extends EventTarget {
return false;
}

getParent() {
_getParent() {
return this.parentNode;
}

Expand Down
12 changes: 6 additions & 6 deletions esm/interface/event-target.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import EventTarget from '@ungap/event-target';
* @implements globalThis.EventTarget
*/
class DOMEventTarget extends EventTarget {
getParent() {
_getParent() {
return null;
}

dispatchEvent(event) {
super.dispatchEvent(event);
const dispatched = super.dispatchEvent(event);

// intentionally simplified, specs imply way more code: https://dom.spec.whatwg.org/#event-path
if (event.bubbles && !event._stopPropagationFlag) {
const parent = this.getParent();
if (dispatched && event.bubbles && !event._stopPropagationFlag) {
const parent = this._getParent();
if (parent && parent.dispatchEvent)
parent.dispatchEvent(event);
return parent.dispatchEvent(event);
}
return true;
return dispatched;
}
}

Expand Down
2 changes: 1 addition & 1 deletion esm/interface/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export class Node extends EventTarget {
return false;
}

getParent() {
_getParent() {
WebReflection marked this conversation as resolved.
Show resolved Hide resolved
return this.parentNode;
}

Expand Down
19 changes: 18 additions & 1 deletion test/interface/event-target.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ 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');
assert(eventTarget._getParent(), null, 'getParent should return null');


// check propagation now
Expand Down Expand Up @@ -75,3 +75,20 @@ 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',
(event) => {
event.stopImmediatePropagation();
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, 1, 'listener should be called once before stopping bubbling');
4 changes: 2 additions & 2 deletions types/interface/document.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,10 @@ export class Document extends NonElementParentNode implements globalThis.Documen
isSameNode(node: any): boolean;
compareDocumentPosition(target: any): number;
isEqualNode(node: any): boolean;
getParent(): any;
_getParent(): any;
getRootNode(): any;
[PREV]: any;
dispatchEvent(event: any): boolean;
dispatchEvent(event: any): any;
};
readonly observedAttributes: any[];
readonly ELEMENT_NODE: number;
Expand Down
4 changes: 2 additions & 2 deletions types/interface/event-target.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ export { DOMEventTarget as EventTarget };
* @implements globalThis.EventTarget
*/
declare class DOMEventTarget implements globalThis.EventTarget {
getParent(): any;
dispatchEvent(event: any): boolean;
_getParent(): any;
dispatchEvent(event: any): any;
}
4 changes: 2 additions & 2 deletions types/interface/image.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ export function ImageClass(ownerDocument: any): {
isSameNode(node: any): boolean;
compareDocumentPosition(target: any): number;
isEqualNode(node: any): boolean;
getParent(): any;
_getParent(): any;
getRootNode(): any;
[PREV]: any;
dispatchEvent(event: any): boolean;
dispatchEvent(event: any): any;
};
readonly observedAttributes: any[];
readonly ELEMENT_NODE: number;
Expand Down