Merge pull request #18 from G5/normalize-rel-urls

Normalizes rels urls
This commit is contained in:
jessicard 2013-11-04 14:28:23 -08:00
commit c7a9820168
3 changed files with 35 additions and 10 deletions

View file

@ -4,7 +4,7 @@ module Microformats2
def initialize(element, url = nil) def initialize(element, url = nil)
@element = element @element = element
@base = nil @base = nil
if url != nil if url != nil
@base = url @base = url
@ -12,7 +12,7 @@ module Microformats2
if @element.search("base").size > 0 if @element.search("base").size > 0
@base = @element.search("base")[0].attribute("href") @base = @element.search("base")[0].attribute("href")
end end
@format_names = [] @format_names = []
@rels = {} @rels = {}
@alternates = [] @alternates = []
@ -46,7 +46,7 @@ module Microformats2
hash[:items] << format.to_hash hash[:items] << format.to_hash
end end
hash[:alternates] = @alternates unless @alternates.nil? || @alternates.empty? hash[:alternates] = @alternates unless @alternates.nil? || @alternates.empty?
hash hash
end end
@ -81,11 +81,11 @@ module Microformats2
send("#{mn.pluralize}=", [value]) send("#{mn.pluralize}=", [value])
end end
end end
def parse_rels def parse_rels
@element.search("*[@rel]").each do |rel| @element.search("*[@rel]").each do |rel|
rel_values = rel.attribute("rel").text.split(" ") rel_values = rel.attribute("rel").text.split(" ")
if rel_values.member?("alternate") if rel_values.member?("alternate")
alternate_inst = {} alternate_inst = {}
alternate_inst["url"] = absolutize(rel.attribute("href").text) alternate_inst["url"] = absolutize(rel.attribute("href").text)
@ -108,13 +108,16 @@ module Microformats2
end end
end end
end end
def absolutize(href) def absolutize(href)
if URI.parse(href).absolute? || @base.nil? uri = URI.parse(href)
href
else if @base && !uri.absolute?
URI.join(@base, href).to_s uri = URI.join(@base, href)
end end
uri.normalize!
uri.to_s
end end
end end
end end

View file

@ -192,6 +192,17 @@ describe Microformats2::Collection do
end end
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 describe "rels-that-drop-the-base.html" do
before do before do
html = "spec/support/lib/microformats2/rels-that-drop-the-base.html" html = "spec/support/lib/microformats2/rels-that-drop-the-base.html"

View file

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>Rels Test</title>
<base href="http://example.org/">
<link rel="updates alternate" href="http://tantek.com/updates.atom" type="application/atom+xml" />
</head>
<body>
<li><a rel="me" href="http://jessicard.com">Jessica Dillon</a></li>
</body>
</html>