From 25f5fda178d6169d088e35d28c242016bbffdf95 Mon Sep 17 00:00:00 2001 From: Jessica Lynn Suttles Date: Mon, 3 Mar 2014 15:32:36 -0800 Subject: [PATCH 1/4] 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 From d8545d9930180694c6227bb4c217cf50e76c967c Mon Sep 17 00:00:00 2001 From: Jessica Lynn Suttles Date: Fri, 7 Mar 2014 16:41:34 -0800 Subject: [PATCH 2/4] Do not log URI::InvalidURIError error message --- lib/microformats2/absolute_uri.rb | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/lib/microformats2/absolute_uri.rb b/lib/microformats2/absolute_uri.rb index 67e69e3..13eafad 100644 --- a/lib/microformats2/absolute_uri.rb +++ b/lib/microformats2/absolute_uri.rb @@ -18,15 +18,9 @@ module Microformats2 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 + rescue URI::InvalidURIError => e + relative.to_s end end end From cb918e2dd1cf86a9f1a56d86c83d77e336ea5aed Mon Sep 17 00:00:00 2001 From: Jessica Lynn Suttles Date: Mon, 10 Mar 2014 12:26:24 -0700 Subject: [PATCH 3/4] Add specs for Microformats2::AbsoluteUri --- lib/microformats2/absolute_uri.rb | 2 +- spec/lib/microformats2/absolute_uri_spec.rb | 73 +++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 spec/lib/microformats2/absolute_uri_spec.rb diff --git a/lib/microformats2/absolute_uri.rb b/lib/microformats2/absolute_uri.rb index 13eafad..a28e0d0 100644 --- a/lib/microformats2/absolute_uri.rb +++ b/lib/microformats2/absolute_uri.rb @@ -19,7 +19,7 @@ module Microformats2 uri.normalize! uri.to_s - rescue URI::InvalidURIError => e + rescue URI::BadURIError, URI::InvalidURIError => e relative.to_s end end diff --git a/spec/lib/microformats2/absolute_uri_spec.rb b/spec/lib/microformats2/absolute_uri_spec.rb new file mode 100644 index 0000000..4aa8a08 --- /dev/null +++ b/spec/lib/microformats2/absolute_uri_spec.rb @@ -0,0 +1,73 @@ +require "spec_helper" +require "microformats2/absolute_uri" + +describe Microformats2::AbsoluteUri do + let(:subject) { Microformats2::AbsoluteUri } + + describe "#absolutize" do + context "when relative is nil" do + it "returns nil" do + base = nil + relative = nil + expect(subject.new(base, relative).absolutize).to be_nil + end + end + + context "when relative is an empty string" do + it "returns nil" do + base = nil + relative = "" + expect(subject.new(base, relative).absolutize).to be_nil + end + end + + context "when relative is a valid URI" do + context "and relative is absolute" do + it "returns normalized relative" do + base = nil + relative = "http://google.com" + result = "http://google.com/" + expect(subject.new(base, relative).absolutize).to eq result + end + end + + context "and relative is not absolute" do + context "and base is present but not absolute" do + it "returns normalized relative" do + base = "foo" + relative = "bar/qux" + result = "bar/qux" + expect(subject.new(base, relative).absolutize).to eq result + end + end + + context "and base is present and absolute" do + it "returns normalized base and relative joined" do + base = "http://google.com" + relative = "foo/bar" + result = "http://google.com/foo/bar" + expect(subject.new(base, relative).absolutize).to eq result + end + end + + context "and base is not present" do + it "returns normalized relative" do + base = nil + relative = "foo/bar" + result = "foo/bar" + expect(subject.new(base, relative).absolutize).to eq result + end + end + end + end + + context "when relative in an invliad URI" do + it "returns relative" do + base = nil + relative = "git@github.com:G5/microformats2.git" + result = "git@github.com:G5/microformats2.git" + expect(subject.new(base, relative).absolutize).to eq result + end + end + end +end From fd6ea19652971b5f2d613d13de745cd3e753ccec Mon Sep 17 00:00:00 2001 From: Don Petersen Date: Mon, 10 Mar 2014 20:54:22 +0000 Subject: [PATCH 4/4] Convert a spec to an explicit subject and lets. --- spec/lib/microformats2/absolute_uri_spec.rb | 91 ++++++++------------- 1 file changed, 33 insertions(+), 58 deletions(-) diff --git a/spec/lib/microformats2/absolute_uri_spec.rb b/spec/lib/microformats2/absolute_uri_spec.rb index 4aa8a08..56b4ee5 100644 --- a/spec/lib/microformats2/absolute_uri_spec.rb +++ b/spec/lib/microformats2/absolute_uri_spec.rb @@ -2,72 +2,47 @@ require "spec_helper" require "microformats2/absolute_uri" describe Microformats2::AbsoluteUri do - let(:subject) { Microformats2::AbsoluteUri } - describe "#absolutize" do + subject { Microformats2::AbsoluteUri.new(base, relative).absolutize } + let(:base) { nil } + context "when relative is nil" do - it "returns nil" do - base = nil - relative = nil - expect(subject.new(base, relative).absolutize).to be_nil - end + let(:relative) { nil } + it { should be_nil } end context "when relative is an empty string" do - it "returns nil" do - base = nil - relative = "" - expect(subject.new(base, relative).absolutize).to be_nil + let(:relative) { "" } + it { should be_nil } + end + + context "when relative is a valid absolute URI" do + let(:relative) { "http://google.com" } + it { should eq("http://google.com/") } + end + + context "when relative is a valid non-absolute URI" do + let(:relative) { "bar/qux" } + + context "and base is present but not absolute" do + let(:base) { "foo" } + it { should eq("bar/qux") } + end + + context "and base is present and absolute" do + let(:base) { "http://google.com" } + it { should eq("http://google.com/bar/qux") } + end + + context "and base is not present" do + let(:base) { nil } + it { should eq("bar/qux") } end end - context "when relative is a valid URI" do - context "and relative is absolute" do - it "returns normalized relative" do - base = nil - relative = "http://google.com" - result = "http://google.com/" - expect(subject.new(base, relative).absolutize).to eq result - end - end - - context "and relative is not absolute" do - context "and base is present but not absolute" do - it "returns normalized relative" do - base = "foo" - relative = "bar/qux" - result = "bar/qux" - expect(subject.new(base, relative).absolutize).to eq result - end - end - - context "and base is present and absolute" do - it "returns normalized base and relative joined" do - base = "http://google.com" - relative = "foo/bar" - result = "http://google.com/foo/bar" - expect(subject.new(base, relative).absolutize).to eq result - end - end - - context "and base is not present" do - it "returns normalized relative" do - base = nil - relative = "foo/bar" - result = "foo/bar" - expect(subject.new(base, relative).absolutize).to eq result - end - end - end - end - - context "when relative in an invliad URI" do - it "returns relative" do - base = nil - relative = "git@github.com:G5/microformats2.git" - result = "git@github.com:G5/microformats2.git" - expect(subject.new(base, relative).absolutize).to eq result - end + context "when relative is an invalid URI" do + let(:relative) { "git@github.com:G5/microformats2.git" } + it { should eq("git@github.com:G5/microformats2.git") } end end end