mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 05:11:20 +08:00
FIX: When mutating a string to build a diff. Duplicate it first
Co-authored-by: Roman Rizzi <rizziromanalejandro@gmail.com>
This commit is contained in:

committed by
Guo Xiang Tan

parent
982c4496ee
commit
888e68a163
@ -178,24 +178,25 @@ class DiscourseDiff
|
|||||||
end
|
end
|
||||||
|
|
||||||
def add_class_or_wrap_in_tags(html_or_text, klass)
|
def add_class_or_wrap_in_tags(html_or_text, klass)
|
||||||
index_of_next_chevron = html_or_text.index(">")
|
result = html_or_text.dup
|
||||||
if html_or_text.length > 0 && html_or_text[0] == '<' && index_of_next_chevron
|
index_of_next_chevron = result.index(">")
|
||||||
index_of_class = html_or_text.index("class=")
|
if result.length > 0 && result[0] == '<' && index_of_next_chevron
|
||||||
|
index_of_class = result.index("class=")
|
||||||
if index_of_class.nil? || index_of_class > index_of_next_chevron
|
if index_of_class.nil? || index_of_class > index_of_next_chevron
|
||||||
# we do not have a class for the current tag
|
# we do not have a class for the current tag
|
||||||
# add it right before the ">"
|
# add it right before the ">"
|
||||||
html_or_text.insert(index_of_next_chevron, " class=\"diff-#{klass}\"")
|
result.insert(index_of_next_chevron, " class=\"diff-#{klass}\"")
|
||||||
else
|
else
|
||||||
# we have a class, insert it at the beginning if not already present
|
# we have a class, insert it at the beginning if not already present
|
||||||
classes = html_or_text[/class=(["'])([^\1]*)\1/, 2]
|
classes = result[/class=(["'])([^\1]*)\1/, 2]
|
||||||
if classes.include?("diff-#{klass}")
|
if classes.include?("diff-#{klass}")
|
||||||
html_or_text
|
result
|
||||||
else
|
else
|
||||||
html_or_text.insert(index_of_class + "class=".length + 1, "diff-#{klass} ")
|
result.insert(index_of_class + "class=".length + 1, "diff-#{klass} ")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
"<#{klass}>#{html_or_text}</#{klass}>"
|
"<#{klass}>#{result}</#{klass}>"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user