Commit be495c6a authored by Stan Hu's avatar Stan Hu

Fix slow Markdown rendering

The sanitize transformers were being duplicated each time the Markdown
renderer was called, leading to expontential growth in rendering times.

The problem was that although HTML::Pipeline::SanitizationFilter.WHITELIST
is a frozen hash, the `:transformers` array can be modified. We need
to do deep copy of this to avoid adding duplicates.

Closes #49409
parent c06e2ac8
---
title: Fix slow Markdown rendering
merge_request: 20820
author:
type: performance
...@@ -13,7 +13,7 @@ module Banzai ...@@ -13,7 +13,7 @@ module Banzai
def whitelist def whitelist
strong_memoize(:whitelist) do strong_memoize(:whitelist) do
customize_whitelist(super.dup) customize_whitelist(super.deep_dup)
end end
end end
......
...@@ -54,6 +54,18 @@ describe Banzai::Filter::SanitizationFilter do ...@@ -54,6 +54,18 @@ describe Banzai::Filter::SanitizationFilter do
expect(instance.whitelist[:transformers].size).to eq control_count expect(instance.whitelist[:transformers].size).to eq control_count
end end
it 'customizes the whitelist only once for different instances' do
instance1 = described_class.new('Foo1')
instance2 = described_class.new('Foo2')
control_count = instance1.whitelist[:transformers].size
instance1.whitelist
instance2.whitelist
expect(instance1.whitelist[:transformers].size).to eq control_count
expect(instance2.whitelist[:transformers].size).to eq control_count
end
it 'sanitizes `class` attribute from all elements' do it 'sanitizes `class` attribute from all elements' do
act = %q{<pre class="code highlight white c"><code>&lt;span class="k"&gt;def&lt;/span&gt;</code></pre>} act = %q{<pre class="code highlight white c"><code>&lt;span class="k"&gt;def&lt;/span&gt;</code></pre>}
exp = %q{<pre><code>&lt;span class="k"&gt;def&lt;/span&gt;</code></pre>} exp = %q{<pre><code>&lt;span class="k"&gt;def&lt;/span&gt;</code></pre>}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment