Last active
March 22, 2019 10:48
-
-
Save emilio/37195e8dee541714769ff6dc3d61ba0f to your computer and use it in GitHub Desktop.
Ever wanted to reduce all the CSS in a test-case to the minimmum? :)
This file contains 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
var USELESS_PROPERTIES = []; | |
function processContainer(container) { | |
if (container instanceof CSSSupportsRule) | |
if (!CSS.supports(container.conditionText)) | |
return false; | |
if (container instanceof CSSMediaRule) | |
if (!matchMedia(container.conditionText).matches) | |
return false; | |
if (container.media && container.media.mediaText) | |
if (!matchMedia(container.media.mediaText).matches) | |
return false; | |
for (let rules = container.cssRules, i = 0; i < rules.length; ++i) { | |
let rule = rules[i]; | |
if (!processRule(rule)) { | |
container.deleteRule(i); | |
i--; | |
} | |
} | |
return !!container.cssRules.length; | |
} | |
function processRule(rule) { | |
if (rule instanceof CSSGroupingRule) | |
return processContainer(rule); | |
if (!(rule instanceof CSSStyleRule)) | |
return false; | |
let selector = rule.selectorText.replace(/::before/g, "").replace(/::after/g, ""); | |
if (selector.includes("form") || selector.includes("dropdown")) | |
return true; | |
if (!document.querySelector(selector)) | |
return false; | |
let style = rule.style; | |
for (let i = 0; i < style.length; ++i) { | |
let property = style[i]; | |
for (const prop of USELESS_PROPERTIES) { | |
if (property.indexOf(prop) != -1) { | |
style.removeProperty(property); | |
i--; | |
} | |
} | |
} | |
return !!style.length; | |
} | |
for (let sheet of document.styleSheets) | |
if (!processContainer(sheet)) | |
if (sheet.ownerNode) | |
sheet.ownerNode.remove(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment