Permalink
Browse files

Add cssExplainStyleSheets function

  • Loading branch information...
1 parent 9ee8c14 commit 2e8ce1d9787aaaaf5026ffba2bd4b6b46f82f61d @josh committed May 19, 2012
Showing with 19 additions and 0 deletions.
  1. +2 −0 README.md
  2. +17 −0 css-explain.js
View
2 README.md
@@ -9,6 +9,7 @@ Think of it like SQL `EXPLAIN`, but for CSS selectors.
``` javascript
cssExplain("li .item")
{
+ "selector": "li .item",
"parts": ["li", ".item"],
"specificity": [0, 1, 1],
"category": "class",
@@ -19,6 +20,7 @@ cssExplain("li .item")
### Results
+* **selector** - Selector input
* **parts** - Parsed selector components
* **specificity** - Computed specificy values as an Array (See [W3C calcuating selector specificity](http://www.w3.org/TR/CSS21/cascade.html#specificity))
* **category** - Category key selector falls under (`id`/`class`/`tag`/`universal`)
View
17 css-explain.js
@@ -198,6 +198,7 @@
var analysis = analyze(parts);
return {
+ selector: selector,
parts: parts,
category: category[0],
key: category[1],
@@ -207,9 +208,25 @@
};
}
+ function cssExplainStyleSheets() {
+ var rules, i, j, results = [];
+
+ for (i = 0; i < document.styleSheets.length; i++) {
+ rules = document.styleSheets[i].cssRules;
+ if (!rules) continue;
+
+ for (j = 0; j < rules.length; j++) {
+ results.concat(cssExplain(rules[j].selectorText, true));
+ }
+ }
+
+ return results;
+ }
+
if (typeof exports !== 'undefined') {
exports.cssExplain = cssExplain;
} else {
window.cssExplain = cssExplain;
+ window.cssExplainStyleSheets = cssExplainStyleSheets;
}
})();

0 comments on commit 2e8ce1d

Please sign in to comment.