Skip to content

Commit

Permalink
Merge pull request #141 from panther7/panther
Browse files Browse the repository at this point in the history
updated classList polyfill: supports second argument for toggle and m…
  • Loading branch information
ondras committed Nov 13, 2015
2 parents b92e611 + 782e57c commit 68e2d5f
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 4 deletions.
43 changes: 41 additions & 2 deletions lib/jak.js
Expand Up @@ -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
Expand Down
43 changes: 41 additions & 2 deletions lib/polyfills/classList.js
Expand Up @@ -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;
})();
17 changes: 17 additions & 0 deletions tests/spec/libs/classlistSpec.js
Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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);
});
});
});

0 comments on commit 68e2d5f

Please sign in to comment.