Created
November 27, 2021 17:06
-
-
Save koenbok/9a3a95792a860de4f9335a49609b66ad to your computer and use it in GitHub Desktop.
Find duplicate styles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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