Skip to content

Instantly share code, notes, and snippets.

@ripper234
Created November 10, 2011 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ripper234/1354590 to your computer and use it in GitHub Desktop.
Save ripper234/1354590 to your computer and use it in GitHub Desktop.
Remove background-image from within :hover tags
// Strip all background-image styles from :hover elements
// This causes weird issues in Chrome
// http://stackoverflow.com/questions/8035366/image-flickering-on-mouseover-even-when-sprites-are-used-for-hover-image/8057250#8057250
//
// Note - this snippet has not been thoroughly tested, and uses simple regex - it's not aware of the actual CSS structure, and might make mistakes
// (E.g. a "}" inside of a comment might fool it
def cssFile = new File("$outputDir/css/combined-sprite.css")
String newCss = ""
boolean inHover = false
cssFile.eachLine(){ line ->
def writeThisLine = ({
// in/out hover mode
if (line =~ /\:\s*hover.*\{/) { // entering hover
assert !inHover, "Can't enter hover while in hover mode"
inHover = true;
return true;
}
if (inHover && line =~ /\}/) {
inHover = false;
return true;
}
return !(inHover && line =~ /background-image\s*:/)
})()
if (writeThisLine) {
newCss += line + "\r\n";
}
}
cssFile.text = newCss
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment