Class: Headhunter::CssHunter

Inherits:
Object
  • Object
show all
Defined in:
lib/headhunter/css_hunter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stylesheets = []) ⇒ CssHunter

Returns a new instance of CssHunter.



9
10
11
12
13
14
15
16
17
18
# File 'lib/headhunter/css_hunter.rb', line 9

def initialize(stylesheets = [])
  @stylesheets      = stylesheets
  @unused_selectors = []
  @used_selectors   = []
  @error_selectors  = []

  @stylesheets.each do |stylesheet|
    add_css_selectors_from(IO.read(stylesheet))
  end
end

Instance Attribute Details

#error_selectorsObject (readonly)

Returns the value of attribute error_selectors.



7
8
9
# File 'lib/headhunter/css_hunter.rb', line 7

def error_selectors
  @error_selectors
end

#unused_selectorsObject (readonly)

Returns the value of attribute unused_selectors.



7
8
9
# File 'lib/headhunter/css_hunter.rb', line 7

def unused_selectors
  @unused_selectors
end

#used_selectorsObject (readonly)

Returns the value of attribute used_selectors.



7
8
9
# File 'lib/headhunter/css_hunter.rb', line 7

def used_selectors
  @used_selectors
end

Instance Method Details

#add_css_selectors_from(css) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/headhunter/css_hunter.rb', line 53

def add_css_selectors_from(css)
  parser = CssParser::Parser.new
  parser.add_block!(css)

  parser.each_selector do |selector, declarations, specificity|
    # next if @unused_selectors.include?(selector)
    # next if has_pseudo_classes?(selector) and @unused_selectors.include?(bare_selector_from(selector))
    @unused_selectors << selector
  end
end

#bare_selector_from(selector) ⇒ Object

def has_pseudo_classes?(selector)

selector =~ /::?[\w\-]+/

end



68
69
70
71
# File 'lib/headhunter/css_hunter.rb', line 68

def bare_selector_from(selector)
  # Add more clean up stuff here, e.g. stuff like @keyframe (Deadweight implemented this)?
  remove_pseudo_classes_from(selector)
end

#detect_used_selectors_in(html) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/headhunter/css_hunter.rb', line 38

def detect_used_selectors_in(html)
  document = Nokogiri::HTML(html)

  @unused_selectors.collect do |selector, declarations|
    bare_selector = bare_selector_from(selector)

    begin
      selector if document.search(bare_selector).any?
    rescue Nokogiri::CSS::SyntaxError => e
      @error_selectors << selector
      @unused_selectors.delete(selector)
    end
  end.compact # FIXME: Why is compact needed?
end

#process(html) ⇒ Object



20
21
22
23
24
25
# File 'lib/headhunter/css_hunter.rb', line 20

def process(html)
  detect_used_selectors_in(html).each do |selector|
    @used_selectors << selector
    @unused_selectors.delete(selector)
  end
end

#remove_pseudo_classes_from(selector) ⇒ Object



73
74
75
# File 'lib/headhunter/css_hunter.rb', line 73

def remove_pseudo_classes_from(selector)
  selector.gsub(/:.*/, '')  # input#x:nth-child(2):not(#z.o[type='file'])
end

#statisticsObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/headhunter/css_hunter.rb', line 27

def statistics
  lines = []

  lines << "Found #{used_selectors.size + unused_selectors.size + error_selectors.size} CSS selectors.".yellow
  lines << 'All selectors are in use.'.green unless (unused_selectors + error_selectors).any?
  lines << "#{unused_selectors.size} selectors are not in use: #{unused_selectors.sort.join(', ').red}".red if unused_selectors.any?
  lines << "#{error_selectors.size} selectors could not be parsed: #{error_selectors.sort.join(', ').red}".red if error_selectors.any?

  lines.join("\n")
end