diff --git a/lib/microformats2/collection.rb b/lib/microformats2/collection.rb index 875acc4..475031c 100644 --- a/lib/microformats2/collection.rb +++ b/lib/microformats2/collection.rb @@ -4,7 +4,7 @@ module Microformats2 def initialize(element, url = nil) @element = element - + @base = nil if url != nil @base = url @@ -12,7 +12,7 @@ module Microformats2 if @element.search("base").size > 0 @base = @element.search("base")[0].attribute("href") end - + @format_names = [] @rels = {} @alternates = [] @@ -46,7 +46,7 @@ module Microformats2 hash[:items] << format.to_hash end hash[:alternates] = @alternates unless @alternates.nil? || @alternates.empty? - + hash end @@ -81,11 +81,11 @@ module Microformats2 send("#{mn.pluralize}=", [value]) end end - + def parse_rels @element.search("*[@rel]").each do |rel| rel_values = rel.attribute("rel").text.split(" ") - + if rel_values.member?("alternate") alternate_inst = {} alternate_inst["url"] = absolutize(rel.attribute("href").text) @@ -108,13 +108,16 @@ module Microformats2 end end end - + def absolutize(href) - if URI.parse(href).absolute? || @base.nil? - href - else - URI.join(@base, href).to_s + uri = URI.parse(href) + + if @base && !uri.absolute? + uri = URI.join(@base, href) end + + uri.normalize! + uri.to_s end end end diff --git a/spec/lib/microformats2/collection_spec.rb b/spec/lib/microformats2/collection_spec.rb index e270dd1..496f9c7 100644 --- a/spec/lib/microformats2/collection_spec.rb +++ b/spec/lib/microformats2/collection_spec.rb @@ -192,6 +192,17 @@ describe Microformats2::Collection do end end + describe "rels-with-unnormalized-urls.html" do + before do + html = "spec/support/lib/microformats2/rels-with-unnormalized-urls.html" + @collection = Microformats2.parse(html) + end + + it "should normalize the url" do + @collection.to_hash[:rels]["me"].should eq([ "http://jessicard.com/" ]) + end + end + describe "rels-that-drop-the-base.html" do before do html = "spec/support/lib/microformats2/rels-that-drop-the-base.html" diff --git a/spec/support/lib/microformats2/rels-with-unnormalized-urls.html b/spec/support/lib/microformats2/rels-with-unnormalized-urls.html new file mode 100644 index 0000000..d0a566c --- /dev/null +++ b/spec/support/lib/microformats2/rels-with-unnormalized-urls.html @@ -0,0 +1,11 @@ + + + +Rels Test + + + + +
  • Jessica Dillon
  • + + \ No newline at end of file