admin管理员组文章数量:1220485
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.
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.
Share Improve this question edited May 3, 2014 at 12:33 user3199690 444 bronze badges asked May 3, 2014 at 12:27 AliAli 267k267 gold badges591 silver badges784 bronze badges 4- The title reads "css file", the first paragraph reads "javascript file" and the second one reads "css files". Which is it? – PhistucK Commented May 3, 2014 at 12:29
- @PhistucK Sorry, that was a typo. I've fixed it. Its 'css files' – Ali Commented May 3, 2014 at 12:30
- I did something similar by writing some code. – Ejaz 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 – PhistucK Commented May 3, 2014 at 12:34
4 Answers
Reset to default 14- Include your css file into any html file.
In console execute the following code:
Array.prototype.forEach.call(document.styleSheets[0].cssRules,function(a){console.log(a.selectorText)})
In the console will be the listing of all css tags used in your stylesheet.
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…
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>
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))
本文标签: javascriptGetting the names of all classesids contained within a css fileStack Overflow
版权声明:本文标题:javascript - Getting the names of all classesids contained within a css file? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739268012a2155687.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论