Skip to content

Instantly share code, notes, and snippets.

@hallvors
Created December 6, 2013 11:14
Show Gist options
  • Select an option

  • Save hallvors/7822130 to your computer and use it in GitHub Desktop.

Select an option

Save hallvors/7822130 to your computer and use it in GitHub Desktop.
A method that tries to remove CSS rules that do not apply to anything in the current page, aids in minimising test cases
(function minimizeCSS(){
var s = document.styleSheets[0].cssRules;
var j=s.length-1, ar=[];
try{
for(;j>=0;j--){
if(document.querySelectorAll(s[j].selectorText).length>0)ar.push(s[j].cssText);
}
}catch(e){
console.log('error for style #'+j)
}
document.body.appendChild(document.createElement('pre')).appendChild(document.createTextNode(ar.join('\n')));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment