Merge pull request #199 from jekyll/nil-image-path-fix

Guard against invalid or missing URLs
This commit is contained in:
Ben Balter 2017-05-01 11:32:18 -04:00 committed by GitHub
commit a28cff0466
2 changed files with 23 additions and 1 deletions

View File

@ -162,6 +162,7 @@ module Jekyll
image = { "path" => image } if image.is_a?(String)
image["path"] ||= image["facebook"] || image["twitter"]
return @image = nil unless image["path"]
unless absolute_url? image["path"]
image["path"] = filters.absolute_url image["path"]
@ -177,7 +178,9 @@ module Jekyll
end
def canonical_url
@canonical_url ||= filters.absolute_url(page["url"]).gsub(%r!/index\.html$!, "/")
@canonical_url ||= begin
filters.absolute_url(page["url"]).to_s.gsub(%r!/index\.html$!, "/")
end
end
private
@ -205,7 +208,10 @@ module Jekyll
end
def absolute_url?(string)
return unless string
Addressable::URI.parse(string).absolute?
rescue Addressable::URI::InvalidURIError
nil
end
def format_string(string)

View File

@ -481,6 +481,22 @@ RSpec.describe Jekyll::SeoTag::Drop do
end
end
context "with some random hash" do
let(:image) { { "foo" => "bar" } }
it "returns nil" do
expect(subject.image).to be_nil
end
end
context "with an invalid path" do
let(:image) { ":" }
it "returns nil" do
expect(subject.image["path"]).to eql("/:")
end
end
context "with height and width" do
let(:image) { { "path" => "image.png", "height" => 5, "width" => 10 } }