Skip to content

Instantly share code, notes, and snippets.

@koenbok
Created November 27, 2021 17:06
Show Gist options
  • Save koenbok/9a3a95792a860de4f9335a49609b66ad to your computer and use it in GitHub Desktop.
Save koenbok/9a3a95792a860de4f9335a49609b66ad to your computer and use it in GitHub Desktop.
Find duplicate styles
const rules = Array.from(document.getElementsByTagName("style"))
.map((el) => el.innerText.split("}"))
.flat()
.map((rule) => rule.trim())
.filter((rule) => rule && !rule.startsWith("@"))
.map((rule) => {
return {
selector: rule.split("{")[0].trim(),
style: rule.split("{")[1].trim(),
};
});
const global = {};
const counts = { total: 0, rules: 0, selectors: 0 };
for (const { selector, style } of rules) {
counts.total++;
if (global[selector] === style) {
console.log(`Duplicate rule: ${selector}`);
counts.rules++;
} else if (global[selector]) {
console.log(`Duplicate selector: ${selector}`);
counts.selectors++;
} else {
global[selector] = style;
}
}
console.log(`Total duplicates`, counts);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment