8

Is there a way, or a tool, which can be used to get a list of the names of all the classes / ids contained within a css file?

I need to build an xml mapping of all css classes / ids, and doing it manually is very tedious for css files that contain thousands of styles.

4
  • The title reads "css file", the first paragraph reads "javascript file" and the second one reads "css files". Which is it? Commented May 3, 2014 at 12:29
  • @PhistucK Sorry, that was a typo. I've fixed it. Its 'css files' Commented May 3, 2014 at 12:30
  • I did something similar by writing some code. Commented May 3, 2014 at 12:34
  • This looks like a parser, it might help you get where you want. codetheory.in/parsing-css-in-javascript Commented May 3, 2014 at 12:34

4 Answers 4

14
  1. Include your css file into any html file.
  2. In console execute the following code:

    Array.prototype.forEach.call(document.styleSheets[0].cssRules,function(a){console.log(a.selectorText)})

  3. In the console will be the listing of all css tags used in your stylesheet.

Sign up to request clarification or add additional context in comments.

1 Comment

This does not give classes but selectors.
2

You can iterate over document.styleSheets to get each style sheet, then iterate over the rules to find everything that matches a class or ID selector. The following is a simplistic approach that provides a general approach, the regular expressions to get the classes and IDs from the rules needs more work.

function getClassesAndIds() {
  var sheet, sheets = document.styleSheets;
  var rule, rules;
  var classes = [];
  var ids = [];
  var temp;

  for (var i=0, iLen=sheets.length; i<iLen; i++) {
    sheet = sheets[i];
    rules = sheet.rules;

    for (var j=0, jLen=rules.length; j<jLen; j++) {
      rule = rules[j];

      // Get the classes
      temp = rule.cssText.match(/\.\w+/g);

      if (temp) {
        classes.push.apply(classes, temp);
      }

      // Get the IDs
      temp = rule.cssText.match(/\#\w+/g);

      if (temp) {
        ids.push.apply(ids, temp);
      }
    }
   }
  // Return an array of the class and ID arrays
  return [classes,ids];

  // or as an object
  // return {classes:classes, ids:ids};
}

window.onload = function() {
  console.log(getClassesAndIds());
};

From memory there were some quirks in older IE around sheets and rules but right now it evades me…

1 Comment

DOMException: CSSStyleSheet.rules getter: Not allowed to access cross-origin stylesheet ... when run in console.
1

See my working example here :)

https://codepen.io/pixelfast/pen/rNrovmj

<script>
function getEm() {
  url = document.getElementById("url").value;

  fetch(url)
    .then((response) => response.text())
    .then((data) => {
      var cssText = data;

      var classRegex = /\.([\w-]+)/g;
      var idRegex = /#([\w-]+)/g;

      var classes = [];
      var ids = [];

      var match;
      while ((match = classRegex.exec(cssText)) !== null) {
        classes.push(match[1]);
      }

      while ((match = idRegex.exec(cssText)) !== null) {
        ids.push(match[1]);
      }

      console.log("Classes: " + classes);
      console.log("IDs: " + ids);

      document.getElementById("classes").innerHTML = classes + "<hr>";
      document.getElementById("ids").innerHTML = ids;
    });
}
</script>


<!-- HTML -->

<input style="width:400px" type="text" id="url" value="https://getbootstrap.com/docs/5.3/assets/css/docs.css">
<button onclick="getEm()">Get Em</button>
<hr>
<div id="classes"></div>
<div id="ids"></div>

Comments

0

In my case, i need all class names (there are selectors which is start with ".icon-" ) in icons.css for listing all defined font icons.

const {cssRules} = Object.values(document.styleSheets)
      .find(sheet =>
        Object.values(sheet.cssRules).find(rule =>
          (rule.selectorText || '').match(/^.icon/)
        )
      ) || {cssRules: {}}

console.log(Object.values(cssRules).map(i => i.selectorText))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.