Skip to content

Commit

Permalink
Merge pull request #1 from maddada/patch-1
Browse files Browse the repository at this point in the history
Modified the Polyfill to avoid rare issue with ".has" doesn't exist
  • Loading branch information
mfreed7 authored Aug 23, 2024
2 parents 2e0a301 + 531256c commit d66cffc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
20 changes: 13 additions & 7 deletions src/mutation_events.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,18 @@
return rootNode;
}

// Helper function to avoid errors when has doesn't exist
function hasTarget(elem, target) {
return elem && elem.has && elem.has(target)
}

const observerOptions = {subtree: true, childList: true, attributes: true, attributeOldValue: true, characterData: true, characterDataOldValue: true};
function enableMutationEventPolyfill(target) {
if (listeningNodes.has(target))
if (hasTarget(listeningNodes, target))
return;
listeningNodes.add(target);
const rootElement = getRootElement(target);
if (documentsToObservers.has(rootElement)) {
if (hasTarget(documentsToObservers, rootElement)) {
documentsToObservers.get(rootElement).count++;
return;
}
Expand All @@ -143,11 +148,11 @@
}

function disableMutationEventPolyfill(target) {
if (!listeningNodes.has(target))
if (!hasTarget(listeningNodes, target))
return;
listeningNodes.delete(target);
const rootElement = getRootElement(target);
if (!documentsToObservers.has(rootElement))
if (!hasTarget(documentsToObservers, rootElement))
return;
if (--documentsToObservers.get(rootElement).count === 0) {
const observer = documentsToObservers.get(rootElement).observer;
Expand All @@ -159,7 +164,7 @@
// Monkeypatch addEventListener/removeEventListener
const originalAddEventListener = Element.prototype.addEventListener;
function getAugmentedListener(eventName, listener, options) {
if (mutationEvents.has(eventName)) {
if (hasTarget(mutationEvents, eventName)) {
return {fullEventName: eventName + polyfillEventNameExtension,
augmentedListener: (event) => {
// Remove polyfillEventNameExtension:
Expand All @@ -170,7 +175,7 @@
return {fullEventName: eventName,augmentedListener: listener};
}
Element.prototype.addEventListener = function(eventName, listener, options) {
if (mutationEvents.has(eventName)) {
if (hasTarget(mutationEvents, eventName)) {
enableMutationEventPolyfill(this);
const {augmentedListener,fullEventName} = getAugmentedListener(eventName, listener, options);
originalAddEventListener.apply(this, [fullEventName, augmentedListener, options]);
Expand All @@ -180,7 +185,7 @@
};
const originalRemoveEventListener = window.removeEventListener;
Element.prototype.removeEventListener = function(eventName, listener, options) {
if (mutationEvents.has(eventName)) {
if (hasTarget(mutationEvents, eventName)) {
disableMutationEventPolyfill(this);
const {augmentedListener,fullEventName} = getAugmentedListener(eventName, listener, options);
originalRemoveEventListener.apply(this, [fullEventName, augmentedListener, options]);
Expand Down Expand Up @@ -214,4 +219,5 @@
};

console.log(`Mutation Events polyfill installed (native feature: ${nativeFeatureSupported ? "supported" : "not present"}).`);

})();
2 changes: 1 addition & 1 deletion src/mutation_events.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d66cffc

Please sign in to comment.