Skip to content

Instantly share code, notes, and snippets.

@defx
Created December 12, 2020 11:58
Show Gist options
  • Save defx/3c35df277aecf89fa33ea9277f2f48e5 to your computer and use it in GitHub Desktop.
Save defx/3c35df277aecf89fa33ea9277f2f48e5 to your computer and use it in GitHub Desktop.
Add a prefix to all selectors in a CSS string
function prefixSelectors(prefix, css) {
let insideBlock = false;
let look = true;
let output = '';
for (let char of css) {
if (char === '}') {
insideBlock = false;
look = true;
} else if (char === ',') {
look = true;
} else if (char === '{') {
insideBlock = true;
} else if (look && !insideBlock && !char.match(/\s/)) {
output += prefix + ' ';
look = false;
}
output += char;
}
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment