class StyleCop::Selector

Constants

EXCLUDED_KEYS

Attributes

selector[R]

Public Class Methods

new(selector) click to toggle source
# File lib/style_cop/selector.rb, line 8
def initialize(selector)
  @selector = selector
end

Public Instance Methods

key() click to toggle source
# File lib/style_cop/selector.rb, line 12
def key
  if selector['class']
    ".#{selector['class'].gsub(' ', '.')}"
  elsif selector['id']
    "##{selector['id']}"
  else
    selector.tag_name
  end
end
representation() click to toggle source
# File lib/style_cop/selector.rb, line 22
def representation
  clean_key = key.gsub(".style-cop-pattern", "")
  return { clean_key => computed_style } if children.empty?
  children_hash = children.map(&:representation).inject({}) { |hash, h| hash.merge!(h) }
  Hash[children_hash.map { |key, value| ["#{clean_key} #{key}", value] }].merge(
    clean_key => computed_style
  )
end

Private Instance Methods

children() click to toggle source
# File lib/style_cop/selector.rb, line 41
def children
  selector.all(:xpath, "#{selector.path}/*").map do |child|
    Selector.new(child)
  end
end
computed_style() click to toggle source
# File lib/style_cop/selector.rb, line 35
def computed_style
  style_hash.tap do |hash|
    EXCLUDED_KEYS.each { |key| hash.delete(key) }
  end
end
computed_style_script() click to toggle source
# File lib/style_cop/selector.rb, line 63
def computed_style_script
  %{
    var node = document.evaluate("/#{selector.path}",
        document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue
    window.getComputedStyle(node);
  }
end
css() click to toggle source
# File lib/style_cop/selector.rb, line 51
def css
  if computed_style = session.evaluate_script(computed_style_script)
    computed_style["cssText"]
  else
    raise RuntimeError.new("Can't find css for #{selector.key}")
  end
end
session() click to toggle source
# File lib/style_cop/selector.rb, line 59
def session
  selector.session
end
style_hash() click to toggle source
# File lib/style_cop/selector.rb, line 47
def style_hash
  Hash[css.split(/\s*;\s*/).map { |s| s.split(/\s*:\s*/) }]
end