diff --git a/lib/deadweight.rb b/lib/deadweight.rb index f31000d..a0e2c16 100644 --- a/lib/deadweight.rb +++ b/lib/deadweight.rb @@ -22,6 +22,7 @@ def initialize @pages = [] @rules = "" @ignore_selectors = [] + @error_selectors = [] @mechanize = false @log_file = STDERR yield self and run if block_given? @@ -37,9 +38,14 @@ def analyze(html) next if stripped_selector.empty? - if doc.search(stripped_selector).any? - log.puts(" #{selector.green}") - selector + begin + if doc.search(stripped_selector).any? + log.puts(" #{selector.green}") + selector + end + rescue Nokogiri::CSS::SyntaxError => e + log.puts(" #{selector.red}") + @error_selectors << selector end end end @@ -85,6 +91,7 @@ def reset! def report log.puts log.puts "found #{@unused_selectors.size} unused selectors out of #{@total_selectors} total".yellow + log.puts "found #{@error_selectors.size} which could not be parsed".red log.puts end @@ -125,6 +132,8 @@ def run def dump(output) output.puts(@unused_selectors) + output.puts "== Error" + output.puts(@error_selectors) end def process!(html) diff --git a/test/fixtures/style.css b/test/fixtures/style.css index 6882491..88729b2 100644 --- a/test/fixtures/style.css +++ b/test/fixtures/style.css @@ -27,4 +27,6 @@ input#fancy:nth-child(2):not(#z.o[type='file']) { color: red; } -@-webkit-keyframes rainbow {} \ No newline at end of file +@-webkit-keyframes rainbow {} + +* :click { outline: 0; } diff --git a/test/test_helper.rb b/test/test_helper.rb index 82ef815..69a9fbc 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -9,6 +9,7 @@ class Test::Unit::TestCase UNUSED_SELECTORS = ['#foo .bar .baz'] USED_SELECTORS = ['#foo', '#foo .bar'] + ERROR_SELECTORS = ['* :click'] def self.should_correctly_report_selectors should "report unused selectors" do @@ -18,6 +19,10 @@ def self.should_correctly_report_selectors should "not report used selectors" do assert_does_not_report_used_selectors(@result) end + + should "report errored selectors" do + assert_reports_error_selectors(@result) + end end def assert_correct_selectors_in_output(output) @@ -32,6 +37,12 @@ def assert_reports_unused_selectors(output) end end + def assert_reports_error_selectors(output) + ERROR_SELECTORS.each do |s| + assert output.include?(s), "output is missing #{s.inspect}:\n#{output}" + end + end + def assert_does_not_report_used_selectors(output) USED_SELECTORS.each do |s| assert !output.include?(s), "output should not contain #{s.inspect}:\n#{output}"