Skip to content

Instantly share code, notes, and snippets.

@hillerstorm
Forked from 140bytes/LICENSE.txt
Last active December 25, 2015 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hillerstorm/6974495 to your computer and use it in GitHub Desktop.
Save hillerstorm/6974495 to your computer and use it in GitHub Desktop.
Checks to see if a given CSS selector is defined in any included stylesheet

CSS-selector defined or not?

Given a CSS selector, returns true or false depending on wether the selector is defined in any included or embedded stylesheet.

131bytes

function(
a, // The CSS selector to search for
b,c,d,e // Placeholders
){
for(c in b=document.styleSheets) // Iterate over every stylesheet
for(d in e=b[c].rules||b[c].cssRules) // then iterate over every rule
if(e[d].selectorText==a) // to see if it matches the input
return!0; // If so, return true!
return!b // No match :/
}
function(a,b,c,d,e){for(c in b=document.styleSheets)for(d in e=b[c].rules||b[c].cssRules)if(e[d].selectorText==a)return!0;return!b}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2013 Johan Hillerström <https://github.com/hillerstorm>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "cssSelectorDefinedOrNot",
"description": "Checks to see if a given CSS selector is defined in any included stylesheet.",
"keywords": [
"css",
"selector"
]
}
<!DOCTYPE html>
<title>CSS-selector defined or not?</title>
<style>
.this-exists{
}
</style>
<div>Expected value: <b>true</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
var exists = function(a,b,c,d,e){for(c in b=document.styleSheets)for(d in e=b[c].rules||b[c].cssRules)if(e[d].selectorText==a)return!0;return!b}
document.getElementById( "ret" ).innerHTML = exists('.this-exists')
</script>
@atk
Copy link

atk commented Oct 14, 2013

Even for..in loops can be used for definitions (save 5 chars):

function(a,b,c,d,e){for(c in b=document.styleSheets)for(d in e=b[c].rules||b[c].cssRules)if(e[d].selectorText==a)return!0;return!b}

@hillerstorm
Copy link
Author

:) coolio

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment