1 /* (This is the new BSD license.) 2 * Copyright (c) 2012, Chris Culy 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * * Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * * Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * * Neither the name of the Chris Culy nor the 13 * names of its contributors may be used to endorse or promote 14 * products from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY Chris Culy 17 * ``AS IS'' AND ANY OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 18 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL Chris Culy 20 * BE LIABLE FOR ANY, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 22 * GOODS OR SERVICES; OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 * CAUSED AND ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 24 * TORT INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 /* 29 * This is a substitute for the classlist attribute, since only Firefox currently supports it for both HTML and SVG elements. 30 * Shims do not seem to support SVG elements. 31 */ 32 33 function classListContains(elt, cls) { 34 if (elt.classList) { 35 return elt.classList.contains(cls); 36 } 37 if (! elt.class) { 38 return false; 39 } 40 var cList = elt.class.split(/ +/); 41 return (cList.indexOf(cls) > -1) 42 } 43 44 function classListAdd(elt, cls) { 45 if (elt.classList) { 46 elt.classList.add(cls); 47 return; 48 } 49 50 if (! classListContains(elt, cls) ) { 51 if (! elt.class) { 52 elt.class = cls; 53 } else { 54 elt.class += " " + cls; 55 } 56 } 57 } 58 59 function classListRemove(elt, cls) { 60 if (elt.classList) { 61 elt.classList.remove(cls); 62 return; 63 } 64 if (classListContains(elt, cls) ) { 65 var classList = " " + elt.class + " "; 66 classList = classList.replace(" " + cls + " ", " "); 67 elt.class = classList.trim(); 68 } 69 } 70 71 function classListToggle(elt, cls) { 72 if (elt.classList) { 73 elt.classList.toggle(cls); 74 return; 75 } 76 77 if (classListContains(elt,cls)) { 78 classListRemove(elt, cls); 79 } else { 80 classListAdd(elt, cls); 81 } 82 } 83 84 function classListToString(elt) { 85 if (elt.classList) { 86 var blorp = elt.classList.toString(); 87 return elt.classList.toString(); 88 } 89 return elt.class; 90 }