From 4cde844ffe6e2e129fab778a24ad5273dabcb59c Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 12 Oct 2018 15:25:38 +0200 Subject: [PATCH] Add a `DOMTokenList.toggle` polyfill for the second, optional, "force" parameter This is based on the polyfill available at https://developer.mozilla.org/en-US/docs/Web/API/Element/classList#Polyfill --- src/shared/compatibility.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/shared/compatibility.js b/src/shared/compatibility.js index d1417421b..66eff0591 100644 --- a/src/shared/compatibility.js +++ b/src/shared/compatibility.js @@ -90,6 +90,28 @@ const hasDOM = typeof window === 'object' && typeof document === 'object'; }; })(); +// Provides support for DOMTokenList.prototype.toggle, with the optional +// "force" parameter, in legacy browsers. +// Support: IE +(function checkDOMTokenListToggle() { + if (!hasDOM || isNodeJS()) { + return; + } + const div = document.createElement('div'); + if (div.classList.toggle('test', 0) === false) { + return; + } + const originalDOMTokenListToggle = DOMTokenList.prototype.toggle; + + DOMTokenList.prototype.toggle = function(token) { + if (arguments.length > 1) { + const force = !!arguments[1]; + return (this[force ? 'add' : 'remove'](token), force); + } + return originalDOMTokenListToggle(token); + }; +})(); + // Provides support for String.prototype.includes in legacy browsers. // Support: IE, Chrome<41 (function checkStringIncludes() {