Skip to content

Instantly share code, notes, and snippets.

@rosylilly
Created January 8, 2009 21:41
Show Gist options
  • Save rosylilly/44899 to your computer and use it in GitHub Desktop.
Save rosylilly/44899 to your computer and use it in GitHub Desktop.
class CS5
Pseudo_Classes = [
/root/,
/nth\-child\((\d+n\+\d+|\d+|odd|even)\)/,
/nth\-last\-child\((\d+n\+\d+|\d+|odd|even)\)/,
/nth\-of\-type\((\d+n\+\d+|\d+|odd|even)\)/,
/nth\-last\-of\-type\((\d+n\+\d+|\d+|odd|even)\)/,
/first\-child/,
/last\-child/,
/first\-of\-type/,
/last\-of\-type/,
/only\-child/,
/only\-of\-type/,
/empty/,
/link/,
/visited/,
/active/,
/hover/,
/focus/,
/target/,
/lang\([a-zA-Z\-]+\)/,
/enabled/,
/disabled/,
/cheked/
]
Pseudo_Elements = [
/first\-line/,
/first\-letter/,
/selection/,
/before/,
/after/
]
Elements = [ 'a', 'abbr', 'acronym', 'address', 'applet', 'area', 'b', 'base', 'basefont', 'bdo', 'big', 'blockquote', 'body', 'br', 'button', 'caption', 'center', 'cite', 'code', 'col', 'colgroup', 'dd', 'del', 'dfn', 'dir', 'div', 'dl', 'dt', 'em', 'fieldset', 'font', 'form', 'frame', 'frameset', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'hr', 'html', 'i', 'iframe', 'img', 'input', 'ins', 'isindex', 'kbd', 'label', 'legend', 'li', 'link', 'listing', 'map', 'menu', 'meta', 'nextid', 'noframes', 'noscript', 'object', 'ol', 'optgroup', 'option', 'p', 'param', 'plaintext', 'pre', 'q', 's', 'samp', 'script', 'select', 'small', 'span', 'strike', 'strong', 'style', 'sub', 'sup', 'table', 'tbody', 'td', 'textarea', 'tfoot', 'th', 'thead', 'title', 'tr', 'tt', 'u', 'ul', 'var', 'xmp' ].sort(){|a,b| a.size <=> b.size}.reverse
REGS = {
'universal' => /\*/,
'combinator' => /(\+|>|\~| )/,
'element' => /(#{Elements.join('|')})/oi,
'id' => /\#[a-zA-Z\\][a-zA-Z0-9_\-\\]*/,
'class' => /\.[a-zA-Z\\][a-zA-Z0-9_\-\\]*/,
'attribute' => /\[[a-zA-Z\-\:]+((\~|\^|\$|\*|\|)?=".*")?\]/,
'pseudo-class' => /\:(#{ Pseudo_Classes.join('|')})/o,
'pseudo-element' => /\:{1,2}(#{ Pseudo_Elements.join('|')})/o
}
REGS['negative-pseudo-class'] = /\:not\((#{REGS.values.join('|')})+\)/o
REGS['element'] = /(^| )#{REGS['element']}/o
def self.hash(_selector)
spec = {
'id' => 0,
'class' => 0,
'element' => 0,
'universal' => 0,
'pseudo-class' => 0,
'pseudo-element' => 0,
'negative-pseudo-class' => 0,
'combinator' => 0
}
@selector = _selector.gsub(/\s+/,' ')
# 否定擬似クラスは他セレクタを含むので優先的に実行
@selector.gsub!( REGS['negative-pseudo-class']){ spec['negative-pseudo-class'] += 1; nil}
['id','class','element','universal','pseudo-class','pseudo-element','combinator'].each do | spectype |
@selector.gsub!( REGS[spectype]){ spec[spectype] += 1; nil }
end
return 'Invalid selector: '+@selector if !@selector.empty? # 何一つのこらない。はず。
spec
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment