From e630923e1d70459857fbe04ea69aca8c9bc699ab Mon Sep 17 00:00:00 2001 From: jessicard Date: Sun, 3 Nov 2013 16:55:37 -0800 Subject: [PATCH 1/2] normalizes rels urls in issue https://github.com/G5/microformats2/issues/16 --- lib/microformats2/collection.rb | 10 +++++++--- spec/lib/microformats2/collection_spec.rb | 11 +++++++++++ .../microformats2/rels-with-unnormalized-urls.html | 11 +++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 spec/support/lib/microformats2/rels-with-unnormalized-urls.html diff --git a/lib/microformats2/collection.rb b/lib/microformats2/collection.rb index 875acc4..990d84f 100644 --- a/lib/microformats2/collection.rb +++ b/lib/microformats2/collection.rb @@ -110,11 +110,15 @@ module Microformats2 end def absolutize(href) - if URI.parse(href).absolute? || @base.nil? - href + uri = URI.parse(href) + + if uri.scheme || @base.nil? + uri = uri.normalize else - URI.join(@base, href).to_s + uri = URI.join(@base, href) end + + uri.to_s end end end diff --git a/spec/lib/microformats2/collection_spec.rb b/spec/lib/microformats2/collection_spec.rb index 9aa62af..0b0bda1 100644 --- a/spec/lib/microformats2/collection_spec.rb +++ b/spec/lib/microformats2/collection_spec.rb @@ -191,6 +191,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 From cdce96db132ddc7afaf47c09e40feba56954e0d3 Mon Sep 17 00:00:00 2001 From: Jessica Lynn Suttles Date: Mon, 4 Nov 2013 13:58:02 -0800 Subject: [PATCH 2/2] Simplify Microformats::Collection#absolutize --- lib/microformats2/collection.rb | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/microformats2/collection.rb b/lib/microformats2/collection.rb index 990d84f..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,16 +108,15 @@ module Microformats2 end end end - + def absolutize(href) uri = URI.parse(href) - if uri.scheme || @base.nil? - uri = uri.normalize - else + if @base && !uri.absolute? uri = URI.join(@base, href) end + uri.normalize! uri.to_s end end