Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gracefully handle nokogiri errors #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions lib/deadweight.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def initialize
@pages = []
@rules = ""
@ignore_selectors = []
@error_selectors = []
@mechanize = false
@log_file = STDERR
yield self and run if block_given?
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -125,6 +132,8 @@ def run

def dump(output)
output.puts(@unused_selectors)
output.puts "== Error"
output.puts(@error_selectors)
end

def process!(html)
Expand Down
4 changes: 3 additions & 1 deletion test/fixtures/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ input#fancy:nth-child(2):not(#z.o[type='file']) {
color: red;
}

@-webkit-keyframes rainbow {}
@-webkit-keyframes rainbow {}

* :click { outline: 0; }
11 changes: 11 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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}"
Expand Down