I'm working on an app that allows a bit of user-defined CSS. I need to wrap every CSS selector with a parent class, .template-container. Example:
h2 {
border: 5px solid yellow;
background: green;
border-radius: 100%;
text-align: center;
}
label, li, p, h1 {
color: red !important;
}
.class > p {
color: green;
}
should get changed to:
.template-container h2 {
border: 5px solid yellow;
background: green;
border-radius: 100%;
text-align: center;
}
.template-container label,
.template-container li,
.template-container p,
.template-container h1 {
color: red !important;
}
.template-container .class > p {
color: green;
}
Link to my expression example: https://regex101.com/r/uPIdR4/1
So far I've come up with: (.*)(,|{) -- the selectors get added to the first group, the , or { gets added to a second group. Then for the substitution I'm using .template-container $1 $2
The issue is, for the second group, only the label is getting that parent class added, the rest of the elements do not. So it comes out as:
.template-container label, li, p, h1 {
color: red !important;
}
I've been trying to fix this but I'm not having any luck. Any help would be much appreciated.
