Skip to content

Commit

Permalink
More robust pseudo class generation
Browse files Browse the repository at this point in the history
This add support for active & visited pseudo states, as well as having
a more robust matching method so that CSS rules with multiple pseudo
matchers work as expected.

Specifically this fixes rules like this:

  .test:hover, .test:active {
    color:blue;
  }
  • Loading branch information
kneath committed Dec 10, 2011
1 parent 6e5cc96 commit e2d2453
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
14 changes: 7 additions & 7 deletions example/public/javascripts/kss.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions lib/kss.coffee
@@ -1,23 +1,24 @@
# This class scans your stylesheets for pseudo classes, then inserts a new CSS
# rule with the same properties, but named 'psuedo-class-{{name}}'.
#
# Supported pseudo classes: hover, disabled.
# Supported pseudo classes: hover, disabled, active, visited.
#
# Example:
#
# a:hover{ color:blue; }
# => a.pseudo-class-hover{ color:blue; }
class KssStateGenerator
constructor: ->
hover = /:hover/
disabled = /:disabled/
pseudos = /(\:hover|\:disabled|\:active|\:visited)/g

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))
@insertRule(rule.cssText.replace(':', '.pseudo-class-'))
if (rule.type == CSSRule.STYLE_RULE) && pseudos.test(rule.selectorText)
replaceRule = (matched, stuff) ->
return ".pseudo-class-" + matched.replace(':', '')
@insertRule(rule.cssText.replace(pseudos, replaceRule))

# Takes a given style and attaches it to the current page.
#
Expand Down

0 comments on commit e2d2453

Please sign in to comment.