summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorStan Hu <stanhu@gmail.com>2016-05-31 19:35:33 -0400
committerStan Hu <stanhu@gmail.com>2016-06-01 21:56:27 -0700
commitad3d0585aa2d36e57b53781a5bd6e3dbe96cb71d (patch)
treef149c33e7ad5ca9edee882ae167719dfbdcae8c7 /lib
parent30524901e28176e96e7c0d1a710508367ff99d9f (diff)
downloadgitlab-ce-ad3d0585aa2d36e57b53781a5bd6e3dbe96cb71d.tar.gz
Fix serious performance bug with rendering Markdown with InlineDiffFilter
Nokogiri's `node.replace` was being unnecessarily called for every text node in the document due to a comparison bug. The code previously was comparing the HTML representation of the full document against the text node, which would always fail. Fix the comparison to just compare the modified text. Closes #18011
Diffstat (limited to 'lib')
-rw-r--r--lib/banzai/filter/inline_diff_filter.rb12
1 files changed, 8 insertions, 4 deletions
diff --git a/lib/banzai/filter/inline_diff_filter.rb b/lib/banzai/filter/inline_diff_filter.rb
index 9e75edd4d4c..beb21b19ab3 100644
--- a/lib/banzai/filter/inline_diff_filter.rb
+++ b/lib/banzai/filter/inline_diff_filter.rb
@@ -8,15 +8,19 @@ module Banzai
next if has_ancestor?(node, IGNORED_ANCESTOR_TAGS)
content = node.to_html
- content = content.gsub(/(?:\[\-(.*?)\-\]|\{\-(.*?)\-\})/, '<span class="idiff left right deletion">\1\2</span>')
- content = content.gsub(/(?:\[\+(.*?)\+\]|\{\+(.*?)\+\})/, '<span class="idiff left right addition">\1\2</span>')
+ html_content = inline_diff_filter(content)
- next if html == content
+ next if content == html_content
- node.replace(content)
+ node.replace(html_content)
end
doc
end
+
+ def inline_diff_filter(text)
+ html_content = text.gsub(/(?:\[\-(.*?)\-\]|\{\-(.*?)\-\})/, '<span class="idiff left right deletion">\1\2</span>')
+ html_content.gsub(/(?:\[\+(.*?)\+\]|\{\+(.*?)\+\})/, '<span class="idiff left right addition">\1\2</span>')
+ end
end
end
end