Last active
March 21, 2016 19:49
-
-
Save bkardell/ad0fba550663b9ebbdea to your computer and use it in GitHub Desktop.
Rewind like 5 years no one was thinking much about a11y, developers I mean but developers were doing a lot of js on the dom dom has a simple API - hasAttribute, getAttribute, setAttribute and it has this one very special attribute 'class' which is a space delimited series so that was a big pain in the ass if you want to say 'does it have this cl…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HTMLElement.prototype._tokenListFor = function(attr) { | |
var el = this, | |
getTokens = function() { | |
return (el.hasAttribute(attr)) ? el.getAttribute(attr).split(' ') : []; | |
}, | |
tokList = { | |
contains: function(token) { | |
var contains = getTokens().some(function(item) { | |
return item === token; | |
}); | |
return contains; | |
}, | |
add: function(token) { | |
var refs = getTokens(); | |
if (!this.contains(token)) { | |
refs.push(token); | |
} | |
el.setAttribute(attr, refs.join(' ')); | |
}, | |
remove: function(token) { | |
var refs = getTokens().filter(function(item) { | |
return item !== token; | |
}); | |
el.setAttribute(attr, refs.join(' ')); | |
} | |
}; | |
return tokList; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment