Skip to content

Commit

Permalink
Revert "FIX: When mutating a string to build a diff. Duplicate it fir…
Browse files Browse the repository at this point in the history
…st (discourse#7482)" (discourse#7484)

This reverts commit 20d4093.
  • Loading branch information
romanrizzi authored and SamSaffron committed May 7, 2019
1 parent cf99431 commit 9adfd66
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions lib/discourse_diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,25 +176,24 @@ def tokenize_html(html)
end

def add_class_or_wrap_in_tags(html_or_text, klass)
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=")
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=")
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 ">"
dupped_html_or_text.insert(index_of_next_chevron, " class=\"diff-#{klass}\"")
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 = dupped_html_or_text[/class=(["'])([^\1]*)\1/, 2]
classes = html_or_text[/class=(["'])([^\1]*)\1/, 2]
if classes.include?("diff-#{klass}")
dupped_html_or_text
html_or_text
else
dupped_html_or_text.insert(index_of_class + "class=".length + 1, "diff-#{klass} ")
html_or_text.insert(index_of_class + "class=".length + 1, "diff-#{klass} ")
end
end
else
"<#{klass}>#{dupped_html_or_text}</#{klass}>"
"<#{klass}>#{html_or_text}</#{klass}>"
end
end

Expand Down

0 comments on commit 9adfd66

Please sign in to comment.