Fixed issue with h- class elements being evaluated multiple times

This commit is contained in:
Brian Miller 2011-06-28 19:47:14 -07:00
parent b35a24becc
commit 9777b76b46
3 changed files with 23 additions and 29 deletions

View file

@ -12,8 +12,10 @@ 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)
@ -32,6 +34,8 @@ module Microformats2
microformats[constant_name.downcase.to_sym] << obj
end
end
end
return microformats
end

View file

@ -12,7 +12,7 @@
</a>
</h1>
<h1 class="h-card">
<h1 class="panda h-card">
<a class="p-fn u-url" href="http://iamshane.com">
<span class="p-given-name">Shane</span>
<abbr class="p-additional-name">B</abbr>

View file

@ -19,19 +19,9 @@ class TestMicroformats2 < Test::Unit::TestCase
end
def test_extracts_hcard_from_an_html_file
hcard = <<-END
<html>
<head>
<title>Simple hCard</title>
</head>
<body>
<h1 class="h-card">Chris</h1>
</body>
</html>
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