Newer versions of the DOMTokenList specification allow several add() and remove() arguments, as well as a second toggle() argument to force state.
During recording, Chrome supports several add() and remove() arguments, but none of the other browsers do. IE 10 and below, Firefox 23 and below, Chrome 23 and below, and other browsers do not support the second argument toggle() .
I wrote the following little polyfill to cover me until the support expands:
(function () { /*global DOMTokenList */ var dummy = document.createElement('div'), dtp = DOMTokenList.prototype, toggle = dtp.toggle, add = dtp.add, rem = dtp.remove; dummy.classList.add('class1', 'class2'); // Older versions of the HTMLElement.classList spec didn't allow multiple // arguments, easy to test for if (!dummy.classList.contains('class2')) { dtp.add = function () { Array.prototype.forEach.call(arguments, add.bind(this)); }; dtp.remove = function () { Array.prototype.forEach.call(arguments, rem.bind(this)); }; } // Older versions of the spec didn't have a forcedState argument for // `toggle` either, test by checking the return value after forcing if (!dummy.classList.toggle('class1', true)) { dtp.toggle = function (cls, forcedState) { if (forcedState === undefined) return toggle.call(this, cls); (forcedState ? add : rem).call(this, cls); return !!forcedState; }; } })();
A modern browser is expected in compliance with ES5 and DOMTokenList , but I use this polyfill in several specially designed environments, so it works fine for me, but it may need to be configured for scripts that will run in legacy browsers like IE 8 and lower.
Andy E Apr 26 '14 at 21:22 2014-04-26 21:22
source share