Skip to content

Commit

Permalink
Update spf.dom.classlist to handle empty strings safely.
Browse files Browse the repository at this point in the history
Progress on youtube#299.
  • Loading branch information
nicksay committed Mar 3, 2015
1 parent cd4db75 commit 0ca314c
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/client/dom/classlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ spf.dom.classlist.get = function(node) {
* @return {boolean} Whether node has the class.
*/
spf.dom.classlist.contains = function(node, cls) {
if (node.classList) {
if (!cls){
return false;
} else if (node.classList) {
return node.classList.contains(cls);
} else {
var classes = spf.dom.classlist.get(node);
Expand All @@ -57,10 +59,10 @@ spf.dom.classlist.contains = function(node, cls) {
* @param {string} cls Class name to add.
*/
spf.dom.classlist.add = function(node, cls) {
if (node.classList) {
node.classList.add(cls);
} else {
if (!spf.dom.classlist.contains(node, cls)) {
if (cls) {
if (node.classList) {
node.classList.add(cls);
} else if (!spf.dom.classlist.contains(node, cls)) {
node.className += ' ' + cls;
}
}
Expand All @@ -74,16 +76,18 @@ spf.dom.classlist.add = function(node, cls) {
* @param {string} cls Class name to remove.
*/
spf.dom.classlist.remove = function(node, cls) {
if (node.classList) {
node.classList.remove(cls);
} else {
var classes = spf.dom.classlist.get(node);
var newClasses = [];
for (var i = 0, l = classes.length; i < l; i++) {
if (classes[i] != cls) {
newClasses.push(classes[i]);
if (cls) {
if (node.classList) {
node.classList.remove(cls);
} else {
var classes = spf.dom.classlist.get(node);
var newClasses = [];
for (var i = 0, l = classes.length; i < l; i++) {
if (classes[i] != cls) {
newClasses.push(classes[i]);
}
}
node.className = newClasses.join(' ');
}
node.className = newClasses.join(' ');
}
};

0 comments on commit 0ca314c

Please sign in to comment.