diff --git a/lib/microformats2/format.rb b/lib/microformats2/format.rb index 7b642dc..74ba975 100644 --- a/lib/microformats2/format.rb +++ b/lib/microformats2/format.rb @@ -41,7 +41,7 @@ module Microformats2 def parse_implied_properties ip = [] ip << ImpliedProperty::Name.new(@element).parse unless property_present?(:name) - ip << ImpliedProperty::Url.new(@element).parse unless property_present?(:url) + ip << ImpliedProperty::Url.new(@element, @base).parse unless property_present?(:url) ip << ImpliedProperty::Photo.new(@element).parse unless property_present?(:photo) ip.compact.each do |property| save_property_name(property.method_name) diff --git a/lib/microformats2/implied_property/foundation.rb b/lib/microformats2/implied_property/foundation.rb index 57bf199..fcd8fa4 100644 --- a/lib/microformats2/implied_property/foundation.rb +++ b/lib/microformats2/implied_property/foundation.rb @@ -2,8 +2,9 @@ module Microformats2 module ImpliedProperty class Foundation - def initialize(element) + def initialize(element, base=nil) @element = element + @base = base end def parse diff --git a/lib/microformats2/implied_property/url.rb b/lib/microformats2/implied_property/url.rb index 1fb3af0..54ae407 100644 --- a/lib/microformats2/implied_property/url.rb +++ b/lib/microformats2/implied_property/url.rb @@ -6,6 +6,22 @@ module Microformats2 "url" end + def to_s + @to_s = absolutize(super.to_s) if super.to_s != "" + end + + # TODO: make dry, repeated in Collection + def absolutize(href) + uri = URI.parse(href) + + if @base && !uri.absolute? + uri = URI.join(@base, href) + end + + uri.normalize! + uri.to_s + end + protected def name_map diff --git a/spec/lib/microformats2/implied_property/url_spec.rb b/spec/lib/microformats2/implied_property/url_spec.rb index 3c736f0..9a99b80 100644 --- a/spec/lib/microformats2/implied_property/url_spec.rb +++ b/spec/lib/microformats2/implied_property/url_spec.rb @@ -15,6 +15,33 @@ describe Microformats2::ImpliedProperty::Url do end end end + + describe "url-relative.html" do + html = "spec/support/lib/microformats2/implied_property/url-relative.html" + collection = Microformats2.parse(html) + it "should have 2 microformats" do + collection.all.length.should == 2 + end + collection.all.each_with_index do |format, index| + it "implies url to be 'http://example.org/' in case #{index+1}" do + format.url.to_s.should == "http://example.org/" + end + end + end + + describe "url-unnormalized.html" do + html = "spec/support/lib/microformats2/implied_property/url-unnormalized.html" + collection = Microformats2.parse(html) + it "should have 2 microformats" do + collection.all.length.should == 2 + end + collection.all.each_with_index do |format, index| + it "implies url to be 'http://github.com/' in case #{index+1}" do + format.url.to_s.should == "http://github.com/" + end + end + end + describe "url-fail.html" do html = "spec/support/lib/microformats2/implied_property/url-fail.html" collection = Microformats2.parse(html) @@ -22,8 +49,8 @@ describe Microformats2::ImpliedProperty::Url do collection.all.length.should == 2 end collection.all.each_with_index do |format, index| - it "implies url to be '' in case #{index+1}" do - expect {format.url.to_s.should == ""}.to raise_error(NoMethodError) + it "does not imply url in case #{index+1}" do + expect {format.url}.to raise_error(NoMethodError) end end end diff --git a/spec/support/lib/microformats2/implied_property/url-relative.html b/spec/support/lib/microformats2/implied_property/url-relative.html new file mode 100644 index 0000000..a382200 --- /dev/null +++ b/spec/support/lib/microformats2/implied_property/url-relative.html @@ -0,0 +1,13 @@ + + +
+