feed-normalizer icon indicating copy to clipboard operation
feed-normalizer copied to clipboard

NoMethodError in HtmlCleaner.clean

Open robg opened this issue 15 years ago • 0 comments

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?

robg avatar Oct 08 '10 21:10 robg