Module: UnusedCSS
- Includes:
- CssParser
- Defined in:
- lib/dead_css.rb
Defined Under Namespace
Classes: Selector
Constant Summary collapse
- CSS_EXTENSIONS =
['css', 'scss']
- ASSETS_EXTENSIONS =
['html', 'js', 'coffee', 'haml']
- SPLIT_REGEX =
/(?:\s|>|~)+/
- SELECTOR_REGEX =
/((\.|#)\w+)/
Class Method Summary collapse
- .css_selectors(path) ⇒ Object
- .elementary_selectors(composed_selector) ⇒ Object
- .files_by_extensions(root, extensions) ⇒ Object
- .unmatched_selectors(selectors, files) ⇒ Object
Class Method Details
.css_selectors(path) ⇒ Object
34 35 36 37 38 39 40 |
# 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) ⇒ Object
42 43 44 45 46 |
# 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) ⇒ Object
30 31 32 |
# 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) ⇒ Object
48 49 50 51 52 53 54 55 |
# 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 |