Fixed issue with h- class elements being evaluated multiple times
This commit is contained in:
parent
b35a24becc
commit
9777b76b46
3 changed files with 23 additions and 29 deletions
|
@ -12,25 +12,29 @@ module Microformats2
|
||||||
|
|
||||||
doc = Nokogiri::HTML(html)
|
doc = Nokogiri::HTML(html)
|
||||||
microformats = Hash.new{|hash, key| hash[key] = Array.new}
|
microformats = Hash.new{|hash, key| hash[key] = Array.new}
|
||||||
doc.css("*[class^=h-]").each do |microformat|
|
doc.css("*[class*=h-]").each do |microformat|
|
||||||
constant_name = classify(microformat.attribute("class").to_s.gsub("-","_"))
|
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)
|
if Object.const_defined?(constant_name)
|
||||||
klass = Object.const_get(constant_name)
|
klass = Object.const_get(constant_name)
|
||||||
else
|
else
|
||||||
klass = Class.new
|
klass = Class.new
|
||||||
Object.const_set constant_name, klass
|
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
|
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
|
||||||
|
|
||||||
return microformats
|
return microformats
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
</a>
|
</a>
|
||||||
</h1>
|
</h1>
|
||||||
|
|
||||||
<h1 class="h-card">
|
<h1 class="panda h-card">
|
||||||
<a class="p-fn u-url" href="http://iamshane.com">
|
<a class="p-fn u-url" href="http://iamshane.com">
|
||||||
<span class="p-given-name">Shane</span>
|
<span class="p-given-name">Shane</span>
|
||||||
<abbr class="p-additional-name">B</abbr>
|
<abbr class="p-additional-name">B</abbr>
|
||||||
|
|
|
@ -19,19 +19,9 @@ class TestMicroformats2 < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_extracts_hcard_from_an_html_file
|
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")))
|
result = Microformats2.parse(File.open(File.join(File.dirname(__FILE__), "simple.html")))
|
||||||
assert_equal HCard, result[:hcard].first.class
|
assert_equal HCard, result[:hcard].first.class
|
||||||
|
assert_equal 2, result[:hcard].length
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_extracts_hcard_from_html
|
def test_extracts_hcard_from_html
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue