From 315f1ec4cfd7fa2adee3fb95955c150afbc71861 Mon Sep 17 00:00:00 2001 From: Khadija Sidhpuri <59063821+squarebat@users.noreply.github.com> Date: Thu, 15 Jul 2021 22:29:29 +0530 Subject: [PATCH] Fix shorten_urls to not strip out text formatting in comments (#14240) * fix: shorten_urls to only shorten URLs * revert: changes to schema.rb * modify: test to aggregate failures Co-authored-by: rhymes Co-authored-by: rhymes --- app/models/comment.rb | 9 +++++++-- spec/models/comment_spec.rb | 15 ++++++++++++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/app/models/comment.rb b/app/models/comment.rb index c3c460815..def361547 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -12,6 +12,8 @@ class Comment < ApplicationRecord TITLE_DELETED = "[deleted]".freeze TITLE_HIDDEN = "[hidden by post author]".freeze + URI_REGEXP = %r{(?https?://)?(?.+?)(?:\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 @@ -202,9 +204,12 @@ class Comment < ApplicationRecord def shorten_urls! doc = Nokogiri::HTML.fragment(processed_html) doc.css("a").each do |anchor| - unless anchor.to_s.include?("")).to be(true) + expect(comment.processed_html.include?("...")).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?("...")).to be(true) + + comment.body_markdown = "Hello ![Alt-text](#{long_url})" + comment.validate! + expect(comment.processed_html.include?("