Revert "Fix shorten_urls to not strip out text formatting in comments (#14240)" (#14258)

This reverts commit 315f1ec4cf.
This commit is contained in:
Suzanne Aitchison 2021-07-16 14:37:26 +01:00 committed by GitHub
parent bd1b15d6b3
commit dfc08bf5b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 19 deletions

View file

@ -12,8 +12,6 @@ class Comment < ApplicationRecord
TITLE_DELETED = "[deleted]".freeze
TITLE_HIDDEN = "[hidden by post author]".freeze
URI_REGEXP = %r{(?<scheme>https?://)?(?<host>.+?)(?<port>:\d+)?$}.freeze
# The date that we began limiting the number of user mentions in a comment.
MAX_USER_MENTION_LIVE_AT = Time.utc(2021, 3, 12).freeze
@ -204,12 +202,9 @@ class Comment < ApplicationRecord
def shorten_urls!
doc = Nokogiri::HTML.fragment(processed_html)
doc.css("a").each do |anchor|
anchor_inner_html = anchor.inner_html
urls = anchor_inner_html.scan(URI_REGEXP).flatten.compact
urls.each do |url|
anchor_inner_html.sub!(/#{Regexp.escape(url)}/, strip_url(url))
unless anchor.to_s.include?("<img") || anchor.to_s.include?("<del") || anchor.attr("class")&.include?("ltag")
anchor.content = strip_url(anchor.content) unless anchor.to_s.include?("<img") # rubocop:disable Style/SoleNestedConditional
end
anchor.inner_html = anchor_inner_html
end
self.processed_html = doc.to_html.html_safe # rubocop:disable Rails/OutputSafety
end

View file

@ -184,20 +184,11 @@ RSpec.describe Comment, type: :model do
expect(comment.processed_html.include?("Hello <a")).to be(true)
end
it "shortens long urls without removing formatting", :aggregate_failures do
long_url = "https://longurl.com/#{'x' * 100}?#{'y' * 100}"
comment.body_markdown = "Hello #{long_url}"
it "shortens long urls" do
comment.body_markdown = "Hello https://longurl.com/#{'x' * 100}?#{'y' * 100}"
comment.validate!
expect(comment.processed_html.include?("...")).to be(true)
expect(comment.processed_html.include?("...</a>")).to be(true)
expect(comment.processed_html.size < 450).to be(true)
comment.body_markdown = "Hello [**#{long_url}**](#{long_url})"
comment.validate!
expect(comment.processed_html.include?("...</strong>")).to be(true)
comment.body_markdown = "Hello ![Alt-text](#{long_url})"
comment.validate!
expect(comment.processed_html.include?("<img")).to be(true)
end
# rubocop:disable RSpec/ExampleLength