Skip to content

Commit 8ee1f6d

Browse files
committed
Fixed expanding absolute urls
StoryRepository.expand_absolute_urls crashes on line 69 when the tag being modified doesn't contain url attribute. This commit fixes that issue.
1 parent fc22ccc commit 8ee1f6d

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

app/repositories/story_repository.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def self.expand_absolute_urls(content, base_url)
6565
[["a", "href"], ["img", "src"], ["video", "src"]].each do |tag, attr|
6666
doc.css(tag).each do |node|
6767
url = node.get_attribute(attr)
68-
unless url =~ abs_re
68+
unless url =~ abs_re || url.nil?
6969
node.set_attribute(attr, URI.join(base_url, url).to_s)
7070
end
7171
end

spec/repositories/story_repository_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,25 @@
3333
it "handles empty body" do
3434
StoryRepository.expand_absolute_urls("", nil).should eq ""
3535
end
36+
37+
it "doesn't modify tags that do not have url attributes" do
38+
content = <<-EOS
39+
<div>
40+
<img foo="bar">
41+
<a name="something"/></a>
42+
<video foo="bar"></video>
43+
</div>
44+
EOS
45+
46+
result = StoryRepository.expand_absolute_urls(content, "http://oodl.io/d/")
47+
result.gsub(/\n/, "").should eq <<-EOS.gsub(/\n/, "")
48+
<div>
49+
<img foo="bar">
50+
<a name="something"></a>
51+
<video foo="bar"></video>
52+
</div>
53+
EOS
54+
end
3655
end
3756

3857
describe ".extract_content" do

0 commit comments

Comments
 (0)