Skip to content

Instantly share code, notes, and snippets.

Created December 12, 2012 19:57
Show Gist options
  • Save anonymous/4271023 to your computer and use it in GitHub Desktop.
Save anonymous/4271023 to your computer and use it in GitHub Desktop.
A modified version of kss.coffee for the kss-rails engine.
class KssStateGenerator
constructor: ->
hover = /:hover/
disabled = /:disabled/
active = /:active/
rules = [] # to hold all the css rules
try
for stylesheet in document.styleSheets
idxs = []
for rule, idx in stylesheet.cssRules
if rule.type is CSSRule.STYLE_RULE and (hover.test(rule.selectorText) or disabled.test(rule.selectorText) or active.test(rule.selectorText))
# add the css rule to the list
rules.push(rule.cssText.replace(':', '.pseudo-class-'))
# pass the entire list of rules to the insertRules method
@insertRules(rules)
insertRules: (rules) ->
headEl = document.getElementsByTagName('head')[0]
styleEl = document.createElement('style')
styleEl.type = 'text/css'
currentRules = ''
# Loop through all the rules and concatenate them together in one string. Then create
# one style sheet and append it to the page.
if styleEl.styleSheet
for rule in rules
currentRules += rule
styleEl.styleSheet.cssText = currentRules
else
for rule in rules
currentRules += rule
styleEl.appendChild(document.createTextNode(currentRules))
headEl.appendChild(styleEl)
new KssStateGenerator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment