From 20d4093a673f45d7efa7f00dbbe6f563c1ca4598 Mon Sep 17 00:00:00 2001
From: Roman Rizzi <rizziromanalejandro@gmail.com>
Date: Mon, 6 May 2019 13:20:10 -0300
Subject: [PATCH] FIX: When mutating a string to build a diff. Duplicate it
 first (#7482)

---
 lib/discourse_diff.rb | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/lib/discourse_diff.rb b/lib/discourse_diff.rb
index 89d293e4319..06272c8d6fe 100644
--- a/lib/discourse_diff.rb
+++ b/lib/discourse_diff.rb
@@ -176,24 +176,25 @@ class DiscourseDiff
   end
 
   def add_class_or_wrap_in_tags(html_or_text, klass)
-    index_of_next_chevron = html_or_text.index(">")
-    if html_or_text.length > 0 && html_or_text[0] == '<' && index_of_next_chevron
-      index_of_class = html_or_text.index("class=")
+    dupped_html_or_text = html_or_text.dup
+    index_of_next_chevron = dupped_html_or_text.index(">")
+    if dupped_html_or_text.length > 0 && dupped_html_or_text[0] == '<' && index_of_next_chevron
+      index_of_class = dupped_html_or_text.index("class=")
       if index_of_class.nil? || index_of_class > index_of_next_chevron
         # we do not have a class for the current tag
         # add it right before the ">"
-        html_or_text.insert(index_of_next_chevron, " class=\"diff-#{klass}\"")
+        dupped_html_or_text.insert(index_of_next_chevron, " class=\"diff-#{klass}\"")
       else
         # we have a class, insert it at the beginning if not already present
-        classes = html_or_text[/class=(["'])([^\1]*)\1/, 2]
+        classes = dupped_html_or_text[/class=(["'])([^\1]*)\1/, 2]
         if classes.include?("diff-#{klass}")
-          html_or_text
+          dupped_html_or_text
         else
-          html_or_text.insert(index_of_class + "class=".length + 1, "diff-#{klass} ")
+          dupped_html_or_text.insert(index_of_class + "class=".length + 1, "diff-#{klass} ")
         end
       end
     else
-      "<#{klass}>#{html_or_text}</#{klass}>"
+      "<#{klass}>#{dupped_html_or_text}</#{klass}>"
     end
   end