Created
March 9, 2010 19:39
-
-
Save ryanmcgrath/326997 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* cssWalker.js | |
* | |
* Traverse every stylesheet in the document and filter based on their selectors. | |
* | |
* @Requires: Nothing, completely standalone. | |
* | |
* @Returns: An array of style objects/rules. The order of the array is determined by the point | |
* of the stylesheet in the document - e.g, first stylesheet rules are top of the array. | |
* | |
* @Author: Ryan McGrath ([email protected]) | |
*/ | |
var cssWalker = { | |
/* cssWalker.find({...}); | |
* | |
* The only function you need to worry about. Usage is something like below: | |
* | |
* var lol = cssWalker.find({ | |
* styleSheet: <optional; will take an existing stylesheet and parse only that>, | |
* selector: ".css_selector_example", | |
* exclude: "A string of selectors to exclude - e.g, we'll take anything that matches 'html' but doesn't have 'div'. | |
* To do multiples of these, separate them by pipes, ala... b|a|em|strong|etc" | |
* }); | |
* | |
*/ | |
find: function(params) { | |
var results = [], | |
styleSheets = typeof params.styleSheet === "undefined" ? document.styleSheets : params.styleSheet; | |
if(styleSheets.length === "undefined") { | |
return cssWalker._locate({ | |
styleSheet: params.styleSheet, | |
selector: params.selector, | |
exclude: params.exclude ? params.exclude : false | |
}); | |
} else { | |
for(var i = 0; i < styleSheets.length; i++) { | |
var extendedResults = cssWalker._locate({ | |
styleSheet: styleSheets[i], | |
selector: params.selector, | |
exclude: params.exclude ? params.exclude : false | |
}); | |
results = results.concat(extendedResults); | |
} | |
return results.reverse(); | |
} | |
}, | |
/* cssWalker._locate({...}); | |
* | |
* The internal (private) function/engine that actually handles lookups and traversals. | |
* Param object/arguments are the same as cssWalker.find(), see above for notes. | |
*/ | |
_locate: function(params) { | |
var styleSheet = params.styleSheet, | |
declarations = styleSheet.cssRules ? styleSheet.cssRules : styleSheet.rules, | |
results = []; | |
for(var i = 0; i < declarations.length; i++) { | |
var selector = declarations[i].selectorText.toLowerCase(); | |
if(selector.indexOf(params.selector) != -1) { | |
if(params.exclude) { | |
var exclusions = params.exclude.split("|"), | |
shouldPush = true; | |
for(var t = 0; t < exclusions.length; t++) { | |
if(selector.indexOf(exclusions[t]) >= 0) shouldPush = false; | |
} | |
if(shouldPush) results.push(declarations[i]); | |
} else { | |
results.push(declarations[i]); | |
} | |
} | |
} | |
return results; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment