From 782e57cb95fa6c59cdb95655b5ef25e82366bd0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20M=C3=B6sner?= Date: Thu, 12 Nov 2015 18:35:53 +0100 Subject: [PATCH] updated classList polyfill: supports second argument for toggle and more arguments tahn one for add/remove methods --- lib/jak.js | 43 ++++++++++++++++++++++++++++++-- lib/polyfills/classList.js | 43 ++++++++++++++++++++++++++++++-- tests/spec/libs/classlistSpec.js | 17 +++++++++++++ 3 files changed, 99 insertions(+), 4 deletions(-) diff --git a/lib/jak.js b/lib/jak.js index 9f3f4f06..67c6f201 100644 --- a/lib/jak.js +++ b/lib/jak.js @@ -629,16 +629,55 @@ if (!("classList" in document.documentElement) && window.Element) { Object.defineProperty(obj, prop, { get: getter }); - } else { + } else { obj.__defineGetter__(prop, getter); } } defineElementGetter(Element.prototype, "classList", function() { - return new DOMTokenList(this); + return new DOMTokenList(this); }); })(); } + +;(function() { + var testElement = document.createElement("_"); + + testElement.classList.add("c1", "c2"); + + if (!testElement.classList.contains("c2")) { + var createMethod = function(method) { + var original = DOMTokenList.prototype[method]; + + DOMTokenList.prototype[method] = function(token) { + var i, len = arguments.length; + + for (i = 0; i < len; i++) { + token = arguments[i]; + original.call(this, token); + } + }; + }; + createMethod("add"); + createMethod("remove"); + } + + testElement.classList.toggle("c3", false); + + if (testElement.classList.contains("c3")) { + var _toggle = DOMTokenList.prototype.toggle; + + DOMTokenList.prototype.toggle = function(token, force) { + if (1 in arguments && !this.contains(token) === !force) { + return force; + } else { + return _toggle.call(this, token); + } + }; + } + + testElement = null; +})(); /* Licencováno pod MIT Licencí, její celý text je uveden v souboru licence.txt Licenced under the MIT Licence, complete text is available in licence.txt file diff --git a/lib/polyfills/classList.js b/lib/polyfills/classList.js index 9089a71b..58a39dc4 100644 --- a/lib/polyfills/classList.js +++ b/lib/polyfills/classList.js @@ -61,13 +61,52 @@ if (!("classList" in document.documentElement) && window.Element) { Object.defineProperty(obj, prop, { get: getter }); - } else { + } else { obj.__defineGetter__(prop, getter); } } defineElementGetter(Element.prototype, "classList", function() { - return new DOMTokenList(this); + return new DOMTokenList(this); }); })(); } + +;(function() { + var testElement = document.createElement("_"); + + testElement.classList.add("c1", "c2"); + + if (!testElement.classList.contains("c2")) { + var createMethod = function(method) { + var original = DOMTokenList.prototype[method]; + + DOMTokenList.prototype[method] = function(token) { + var i, len = arguments.length; + + for (i = 0; i < len; i++) { + token = arguments[i]; + original.call(this, token); + } + }; + }; + createMethod("add"); + createMethod("remove"); + } + + testElement.classList.toggle("c3", false); + + if (testElement.classList.contains("c3")) { + var _toggle = DOMTokenList.prototype.toggle; + + DOMTokenList.prototype.toggle = function(token, force) { + if (1 in arguments && !this.contains(token) === !force) { + return force; + } else { + return _toggle.call(this, token); + } + }; + } + + testElement = null; +})(); diff --git a/tests/spec/libs/classlistSpec.js b/tests/spec/libs/classlistSpec.js index 40d887c0..934f90cb 100644 --- a/tests/spec/libs/classlistSpec.js +++ b/tests/spec/libs/classlistSpec.js @@ -25,6 +25,11 @@ describe("classList", function(){ node.classList.toggle("ccc"); expect(node.classList.contains("ccc")).toEqual(true); }); + + it("should toggle with second argument", function() { + node.classList.toggle("ccc", false); + expect(node.classList.contains("ccc")).toEqual(false); + }); }); describe("add", function() { @@ -37,6 +42,11 @@ describe("classList", function(){ node.classList.add("aaa"); expect(node.className).toEqual("aaa bbb"); }); + + it("should add with more arguments than one", function() { + node.classList.add("ccc", "ddd", "eee"); + expect(node.className).toEqual("aaa bbb ccc ddd eee"); + }); }); describe("remove", function() { @@ -52,5 +62,12 @@ describe("classList", function(){ expect(node.classList.contains("bbb")).toEqual(true); expect(node.classList.contains("ccc")).toEqual(false); }); + + it("should remove with more arguments than one", function() { + node.classList.remove("bbb", "ccc"); + expect(node.classList.contains("aaa")).toEqual(true); + expect(node.classList.contains("bbb")).toEqual(false); + expect(node.classList.contains("ccc")).toEqual(false); + }); }); });