From 0ca314c3af2a12156b178724fc2a741a52904e54 Mon Sep 17 00:00:00 2001 From: Alex Nicksay Date: Tue, 3 Mar 2015 02:29:04 -0500 Subject: [PATCH] Update spf.dom.classlist to handle empty strings safely. Progress on #299. --- src/client/dom/classlist.js | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/client/dom/classlist.js b/src/client/dom/classlist.js index 7fd89123..d08f0624 100644 --- a/src/client/dom/classlist.js +++ b/src/client/dom/classlist.js @@ -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); @@ -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; } } @@ -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(' '); } };