delegate-it
Advanced tools
Comparing version 2.0.1 to 2.0.2
24
index.js
@@ -27,2 +27,15 @@ /** Keeps track of raw listeners added to the base elements to avoid duplication */ | ||
} | ||
function safeClosest(event, selector) { | ||
let target = event.target; | ||
if (target instanceof Text) { | ||
target = target.parentElement; | ||
} | ||
if (target instanceof Element && event.currentTarget instanceof Element) { | ||
// `.closest()` may match ancestors of `currentTarget` but we only need its children | ||
const closest = target.closest(selector); | ||
if (closest && event.currentTarget.contains(closest)) { | ||
return closest; | ||
} | ||
} | ||
} | ||
/** | ||
@@ -52,10 +65,5 @@ * Delegates event to a selector. | ||
const listenerFn = (event) => { | ||
const delegateTarget = event.target.closest(selector); | ||
if (!delegateTarget) { | ||
return; | ||
} | ||
event.delegateTarget = delegateTarget; | ||
// Closest may match elements outside of the currentTarget | ||
// so it needs to be limited to elements inside it | ||
if (event.currentTarget.contains(event.delegateTarget)) { | ||
const delegateTarget = safeClosest(event, selector); | ||
if (delegateTarget) { | ||
event.delegateTarget = delegateTarget; | ||
callback.call(baseElement, event); | ||
@@ -62,0 +70,0 @@ } |
{ | ||
"name": "delegate-it", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "Lightweight event delegation", | ||
@@ -32,7 +32,6 @@ "keywords": [ | ||
"rules": { | ||
"max-params": 0, | ||
"prefer-rest-params": 0, | ||
"import/extensions": 0, | ||
"unicorn/import-index": 0, | ||
"import/no-useless-path-segments": 0 | ||
"import/extensions": "off", | ||
"import/no-useless-path-segments": "off", | ||
"max-params": "off", | ||
"unicorn/import-index": "off" | ||
} | ||
@@ -47,4 +46,4 @@ }, | ||
"typescript": "^4.0.2", | ||
"xo": "^0.33.1" | ||
"xo": "^0.34.2" | ||
} | ||
} |
# delegate-it [![][badge-gzip]][link-bundlephobia] | ||
[badge-gzip]: https://img.shields.io/bundlephobia/minzip/delegate-it.svg?label=gzipped | ||
[link-bundlephobia]: https://bundlephobia.com/result?p=delegate-it | ||
[badge-gzip]: https://img.shields.io/bundlephobia/minzip/delegate-it.svg?label=gzipped | ||
[link-bundlephobia]: https://bundlephobia.com/result?p=delegate-it | ||
@@ -6,0 +6,0 @@ > Lightweight event delegation |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
9408
100
0