mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-27 09:36:19 -05:00
FIX: emails with embedded posts should always use absolute URLs
This commit is contained in:
parent
a13c7c26f9
commit
4ec0543362
3 changed files with 46 additions and 1 deletions
|
@ -10,7 +10,7 @@
|
|||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class='body'><%= post.cooked.html_safe %></td>
|
||||
<td class='body'><%= PrettyText.make_all_links_absolute(post.cooked).html_safe %></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
|
|
@ -234,6 +234,22 @@ module PrettyText
|
|||
fragment.to_html
|
||||
end
|
||||
|
||||
def self.make_all_links_absolute(html)
|
||||
site_uri = nil
|
||||
doc = Nokogiri::HTML.fragment(html)
|
||||
doc.css("a").each do |l|
|
||||
href = l["href"].to_s
|
||||
begin
|
||||
uri = URI(href)
|
||||
site_uri ||= URI(Discourse.base_url)
|
||||
l["href"] = "#{site_uri}#{l['href']}" unless uri.host.present?
|
||||
rescue URI::InvalidURIError
|
||||
# leave it
|
||||
end
|
||||
end
|
||||
doc.to_html
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def self.ctx_load(ctx, *files)
|
||||
|
|
|
@ -177,4 +177,33 @@ describe PrettyText do
|
|||
end
|
||||
end
|
||||
|
||||
describe "make_all_links_absolute" do
|
||||
let(:base_url) { "http://baseurl.net" }
|
||||
|
||||
before do
|
||||
Discourse.stubs(:base_url).returns(base_url)
|
||||
end
|
||||
|
||||
it "adds base url to relative links" do
|
||||
html = "<p><a class=\"mention\" href=\"/users/wiseguy\">@wiseguy</a>, <a class=\"mention\" href=\"/users/trollol\">@trollol</a> what do you guys think? </p>"
|
||||
output = described_class.make_all_links_absolute(html)
|
||||
output.should == "<p><a class=\"mention\" href=\"#{base_url}/users/wiseguy\">@wiseguy</a>, <a class=\"mention\" href=\"#{base_url}/users/trollol\">@trollol</a> what do you guys think? </p>"
|
||||
end
|
||||
|
||||
it "doesn't change external absolute links" do
|
||||
html = "<p>Check out <a href=\"http://mywebsite.com/users/boss\">this guy</a>.</p>"
|
||||
described_class.make_all_links_absolute(html).should == html
|
||||
end
|
||||
|
||||
it "doesn't change internal absolute links" do
|
||||
html = "<p>Check out <a href=\"#{base_url}/users/boss\">this guy</a>.</p>"
|
||||
described_class.make_all_links_absolute(html).should == html
|
||||
end
|
||||
|
||||
it "can tolerate invalid URLs" do
|
||||
html = "<p>Check out <a href=\"not a real url\">this guy</a>.</p>"
|
||||
expect { described_class.make_all_links_absolute(html) }.to_not raise_error
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue