From 9777b76b46540e8a6008b0e4d7bb3dd801c8902b Mon Sep 17 00:00:00 2001 From: Brian Miller Date: Tue, 28 Jun 2011 19:47:14 -0700 Subject: [PATCH] Fixed issue with h- class elements being evaluated multiple times --- lib/microformats2.rb | 38 +++++++++++++++++++++----------------- test/simple.html | 2 +- test/test_microformats2.rb | 12 +----------- 3 files changed, 23 insertions(+), 29 deletions(-) diff --git a/lib/microformats2.rb b/lib/microformats2.rb index 642519d..6cfe8e9 100644 --- a/lib/microformats2.rb +++ b/lib/microformats2.rb @@ -12,25 +12,29 @@ module Microformats2 doc = Nokogiri::HTML(html) microformats = Hash.new{|hash, key| hash[key] = Array.new} - doc.css("*[class^=h-]").each do |microformat| - constant_name = classify(microformat.attribute("class").to_s.gsub("-","_")) + doc.css("*[class*=h-]").each do |microformat| + microformat.attribute("class").to_s.split.each do |mf_class| + if mf_class =~ /^h-/ + constant_name = classify(mf_class.gsub("-","_")) - if Object.const_defined?(constant_name) - klass = Object.const_get(constant_name) - else - klass = Class.new - Object.const_set constant_name, klass + if Object.const_defined?(constant_name) + klass = Object.const_get(constant_name) + else + klass = Class.new + Object.const_set constant_name, klass + end + + obj = klass.new + + # Add any properties to the object + add_properties(microformat, obj) + add_urls(microformat, obj) + add_dates(microformat, obj) + add_times(microformat, obj) + + microformats[constant_name.downcase.to_sym] << obj + end end - - obj = klass.new - - # Add any properties to the object - add_properties(microformat, obj) - add_urls(microformat, obj) - add_dates(microformat, obj) - add_times(microformat, obj) - - microformats[constant_name.downcase.to_sym] << obj end return microformats diff --git a/test/simple.html b/test/simple.html index bb82de4..6aefbfe 100644 --- a/test/simple.html +++ b/test/simple.html @@ -12,7 +12,7 @@ -

+

Shane B diff --git a/test/test_microformats2.rb b/test/test_microformats2.rb index 694c756..a6fb1d6 100644 --- a/test/test_microformats2.rb +++ b/test/test_microformats2.rb @@ -19,19 +19,9 @@ class TestMicroformats2 < Test::Unit::TestCase end def test_extracts_hcard_from_an_html_file - hcard = <<-END - - - Simple hCard - - - -

Chris

- - - END result = Microformats2.parse(File.open(File.join(File.dirname(__FILE__), "simple.html"))) assert_equal HCard, result[:hcard].first.class + assert_equal 2, result[:hcard].length end def test_extracts_hcard_from_html