Parse comment html as fragments instead of docs (#7419)

This commit is contained in:
Josh Puetz 2020-04-21 15:01:44 -05:00 committed by GitHub
parent 1f74b95e34
commit 98365c878e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 1 deletions

View file

@ -164,7 +164,7 @@ class Comment < ApplicationRecord
end
def shorten_urls!
doc = Nokogiri::HTML.parse(processed_html)
doc = Nokogiri::HTML.fragment(processed_html)
doc.css("a").each do |anchor|
unless anchor.to_s.include?("<img") || anchor.attr("class")&.include?("ltag")
anchor.content = strip_url(anchor.content) unless anchor.to_s.include?("<img")

View file

@ -153,6 +153,13 @@ RSpec.describe Comment, type: :model do
comment.validate!
expect(comment.processed_html.include?(">1:52:30</a>")).to eq(false)
end
it "does not add DOCTYPE and html body to processed html" do
comment.body_markdown = "Hello https://longurl.com/#{'x' * 100}?#{'y' * 100}"
comment.validate!
expect(comment.processed_html).not_to include("<!DOCTYPE")
expect(comment.processed_html).not_to include("<html><body>")
end
end
end