user2579512
user2579512

Reputation: 93

How to parse CSSStyleSheet object into a valid css?

I've been playing with document.styleSheets API and it mostly does what I want, but once I add a bunch of rules I want to be able to parse the shylesheet object into a valid css (so I can put it into a downloadable file), is possible with the API or just by manually looping trough it?

Upvotes: 1

Views: 197

Answers (1)

lante
lante

Reputation: 7336

Iterate over it:

var rules = [], i, j, css;

for (i = 0; i < document.styleSheets.length; i++) {
    for (j = 0; j < document.styleSheets[i].rules.length; j++) {
        rules.push(document.styleSheets[i].rules[j].cssText);
    }
}

css = rules.join('\n');
console.log(css);

Upvotes: 2

Related Questions