module UnusedCSS

Constants

ASSETS_EXTENSIONS
CSS_EXTENSIONS
SELECTOR_REGEX
SPLIT_REGEX

Public Class Methods

css_selectors(path) click to toggle source
# File lib/dead_css.rb, line 34
def self.css_selectors(path)
  parser = CssParser::Parser.new
  parser.load_file!(path)
  selectors = []
  parser.each_selector { |selector, declarations, specificity| selectors << selector }
  selectors.map(&:split).flatten.uniq
end
elementary_selectors(composed_selector) click to toggle source
# File lib/dead_css.rb, line 42
def self.elementary_selectors(composed_selector)
  composed_selector.split(/(?:\s|>|~)+/).map do |s|
    SELECTOR_REGEX.match(s)
  end.flatten.compact.map(&:to_s)
end
files_by_extensions(root, extensions) click to toggle source
# File lib/dead_css.rb, line 30
def self.files_by_extensions(root, extensions)
  extensions.map { |ext| Dir.glob("#{root}/**/*.#{ext}") }.flatten
end
unmatched_selectors(selectors, files) click to toggle source
# File lib/dead_css.rb, line 48
def self.unmatched_selectors(selectors, files)
  unmatched_selectors = Array.new(selectors)
  files.each do |file|
    lines = File.readlines(file)
    unmatched_selectors.delete_if { |selector| lines.grep(Regexp.new(selector.value)).size > 0 }
  end
  unmatched_selectors
end