From 25f5fda178d6169d088e35d28c242016bbffdf95 Mon Sep 17 00:00:00 2001 From: Jessica Lynn Suttles Date: Mon, 3 Mar 2014 15:32:36 -0800 Subject: [PATCH] Allow u-* properties to be invalid URIs --- lib/microformats2.rb | 1 + lib/microformats2/absolute_uri.rb | 32 +++++++++++++++++++++++ lib/microformats2/collection.rb | 15 ++--------- lib/microformats2/implied_property/url.rb | 14 +--------- lib/microformats2/property/url.rb | 14 +--------- 5 files changed, 37 insertions(+), 39 deletions(-) create mode 100644 lib/microformats2/absolute_uri.rb diff --git a/lib/microformats2.rb b/lib/microformats2.rb index 8515004..4359ecb 100644 --- a/lib/microformats2.rb +++ b/lib/microformats2.rb @@ -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" diff --git a/lib/microformats2/absolute_uri.rb b/lib/microformats2/absolute_uri.rb new file mode 100644 index 0000000..67e69e3 --- /dev/null +++ b/lib/microformats2/absolute_uri.rb @@ -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 diff --git a/lib/microformats2/collection.rb b/lib/microformats2/collection.rb index 8469ecc..5ed43e0 100644 --- a/lib/microformats2/collection.rb +++ b/lib/microformats2/collection.rb @@ -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 diff --git a/lib/microformats2/implied_property/url.rb b/lib/microformats2/implied_property/url.rb index 54ae407..26e0ea3 100644 --- a/lib/microformats2/implied_property/url.rb +++ b/lib/microformats2/implied_property/url.rb @@ -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 diff --git a/lib/microformats2/property/url.rb b/lib/microformats2/property/url.rb index 85847a6..c86c6b6 100644 --- a/lib/microformats2/property/url.rb +++ b/lib/microformats2/property/url.rb @@ -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