Allow u-* properties to be invalid URIs
This commit is contained in:
parent
d85c12c606
commit
25f5fda178
5 changed files with 37 additions and 39 deletions
|
@ -4,6 +4,7 @@ require "json"
|
|||
require "active_support/inflector"
|
||||
|
||||
require "microformats2/version"
|
||||
require "microformats2/absolute_uri"
|
||||
require "microformats2/parser"
|
||||
require "microformats2/format_parser"
|
||||
require "microformats2/property_parser"
|
||||
|
|
32
lib/microformats2/absolute_uri.rb
Normal file
32
lib/microformats2/absolute_uri.rb
Normal file
|
@ -0,0 +1,32 @@
|
|||
module Microformats2
|
||||
class AbsoluteUri
|
||||
attr_accessor :base, :relative
|
||||
|
||||
def initialize(base, relative)
|
||||
@base = base
|
||||
@relative = relative
|
||||
end
|
||||
|
||||
def absolutize
|
||||
return nil if relative.nil? or relative == ""
|
||||
|
||||
uri = URI.parse(relative)
|
||||
|
||||
if base && !uri.absolute?
|
||||
uri = URI.join(base.to_s, relative.to_s)
|
||||
end
|
||||
|
||||
uri.normalize!
|
||||
uri.to_s
|
||||
rescue URI::InvalidURIError => e
|
||||
logger.warn e.message
|
||||
relative.to_s
|
||||
end
|
||||
|
||||
def logger
|
||||
@logger ||= Logger.new(STDOUT)
|
||||
@logger.level = Logger::WARN
|
||||
@logger
|
||||
end
|
||||
end
|
||||
end
|
|
@ -88,7 +88,7 @@ module Microformats2
|
|||
|
||||
if rel_values.member?("alternate")
|
||||
alternate_inst = {}
|
||||
alternate_inst["url"] = absolutize(rel.attribute("href").text)
|
||||
alternate_inst["url"] = Microformats2::AbsoluteUri.new(@base, rel.attribute("href").text).absolutize
|
||||
alternate_inst["rel"] = (rel_values - ["alternate"]).join(" ")
|
||||
unless rel.attribute("media").nil?
|
||||
alternate_inst["media"] = rel.attribute("media").text
|
||||
|
@ -103,21 +103,10 @@ module Microformats2
|
|||
else
|
||||
rel_values.each do |rel_value|
|
||||
@rels[rel_value] = [] unless @rels.has_key?(rel_value)
|
||||
@rels[rel_value] << absolutize(rel.attribute("href").text)
|
||||
@rels[rel_value] << Microformats2::AbsoluteUri.new(@base, rel.attribute("href").text).absolutize
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def absolutize(href)
|
||||
uri = URI.parse(href)
|
||||
|
||||
if @base && !uri.absolute?
|
||||
uri = URI.join(@base, href)
|
||||
end
|
||||
|
||||
uri.normalize!
|
||||
uri.to_s
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -7,19 +7,7 @@ module Microformats2
|
|||
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
|
||||
@to_s = Microformats2::AbsoluteUri.new(@base, super.to_s).absolutize
|
||||
end
|
||||
|
||||
protected
|
||||
|
|
|
@ -3,19 +3,7 @@ module Microformats2
|
|||
class Url < Foundation
|
||||
|
||||
def to_s
|
||||
@to_s = absolutize(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
|
||||
@to_s = Microformats2::AbsoluteUri.new(@base, super.to_s).absolutize
|
||||
end
|
||||
|
||||
protected
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue