From 07ab221d3ea0c26c0910f4cb7fdd32155c82332c Mon Sep 17 00:00:00 2001 From: Don Petersen Date: Tue, 8 Oct 2013 22:06:59 +0000 Subject: [PATCH] Don't explode when a rel attribute exists without a base tag. It tries to build an absolute URL for link tags that contain a rel attribute and a relative URL, using the tag to decide what to prefix the relative URLs with. It's very common for people to omit the base tag and to use rel attributes in stylesheets, and that blows up the parser. Now we don't attempt to build absolute URLs when the base URL can't be determined. --- lib/microformats2/collection.rb | 2 +- spec/lib/microformats2/collection_spec.rb | 11 +++++++++++ .../lib/microformats2/rels-that-drop-the-base.html | 11 +++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 spec/support/lib/microformats2/rels-that-drop-the-base.html diff --git a/lib/microformats2/collection.rb b/lib/microformats2/collection.rb index 09d106a..875acc4 100644 --- a/lib/microformats2/collection.rb +++ b/lib/microformats2/collection.rb @@ -110,7 +110,7 @@ module Microformats2 end def absolutize(href) - if URI.parse(href).absolute? + if URI.parse(href).absolute? || @base.nil? href else URI.join(@base, href).to_s diff --git a/spec/lib/microformats2/collection_spec.rb b/spec/lib/microformats2/collection_spec.rb index dfca7f3..9aa62af 100644 --- a/spec/lib/microformats2/collection_spec.rb +++ b/spec/lib/microformats2/collection_spec.rb @@ -190,6 +190,17 @@ describe Microformats2::Collection do end end end + + describe "rels-that-drop-the-base.html" do + before do + html = "spec/support/lib/microformats2/rels-that-drop-the-base.html" + @collection = Microformats2.parse(html) + end + + it "keeps the relative path" do + @collection.to_hash[:rels]["stylesheet"].should eq([ "/path/to/stylesheet.css" ]) + end + end end diff --git a/spec/support/lib/microformats2/rels-that-drop-the-base.html b/spec/support/lib/microformats2/rels-that-drop-the-base.html new file mode 100644 index 0000000..56ed5f4 --- /dev/null +++ b/spec/support/lib/microformats2/rels-that-drop-the-base.html @@ -0,0 +1,11 @@ + + + +Rels without Base Test + + + +
  • Jeremy Keith
  • +
  • Tantek Çelik
  • + +