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

NoMethodError in HtmlCleaner.clean #2

Open
robg opened this issue Oct 8, 2010 · 0 comments
Open

NoMethodError in HtmlCleaner.clean #2

robg opened this issue Oct 8, 2010 · 0 comments

Comments

@robg
Copy link

robg commented Oct 8, 2010

I ran into this while processing a twitter feed where someone had managed to inject new-line characters into the tweet.

Here's the content in question:
"AppWireless: The following stores will have the Motorola Milestone by this afternoon: \nGordon’s\n Photo, Hazard, Harlan,... http://fb.me/HmcxV9X8"

When I run clean on that, I get:
>> FeedNormalizer::HtmlCleaner.clean str
NoMethodError: undefined method traverse_element' for nil:NilClass from /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/whiny_nil.rb:52:inmethod_missing'
from /usr/lib/ruby/gems/1.8/gems/hpricot-0.8.2/lib/hpricot/traverse.rb:313:in /' from /usr/lib/ruby/gems/1.8/gems/hpricot-0.8.2/lib/hpricot/traverse.rb:310:ineach'
from /usr/lib/ruby/gems/1.8/gems/hpricot-0.8.2/lib/hpricot/traverse.rb:310:in /' from /usr/lib/ruby/gems/1.8/gems/feed-normalizer-1.5.2/lib/html-cleaner.rb:164:inremove_tags!'
from /usr/lib/ruby/gems/1.8/gems/feed-normalizer-1.5.2/lib/html-cleaner.rb:72:in `clean'
from (irb):82

From what I can tell, the problem stems from this regular expression in #clean:
# get all the tags in the document
# Somewhere near hpricot 0.4.92 "" starting to return all elements,
# including text nodes instead of just tagged elements.
tags = (doc/"
").inject([]) { |m,e| m << e.name if(e.respond_to?(:name) && e.name =~ /^\w+$/) ; m }.uniq

Which is matching against the full text node, and matches "Gordon's" since it is wrapped in newlines. I'm guessing the regular expression is there to try to filter out text nodes? If so, I'd suggest changing the pattern to
/\A\w+\Z/
To force the match against to whole string. Unless there's a cleaner way to filter out text nodes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant