I have a string with the following css that I need to process with javascript
h1
{
color: red;
}
.info
{
border: 1px dotted blue;
padding: 10px;
}
#rect
{
background: pink;
}
h2,
h3,
h4
{
font-weight: bold;
}
table td:last td
{
background: gainsboro;
}
How can I add the prefix .page
to each rule so the css doesn't break?
I want this result
.page h1
{
color: red;
}
.page .info
{
border: 1px dotted blue;
padding: 10px;
}
...
Right now, I solve it with looking for the indentation, but the code fails on this case
h1
{
color: red;
}
Which then ends up with
.page h1
{
.page color: red;
}
I could also look for rows that only has brackets, but then this case would fail
h1 { color: red; }
I don't want to build my own css parser and the ones I found mostly handles css applied to elements in the DOM and not strings with css. Is there a good parser or can this be achieved otherwise?