avoid pre + code dom structure (#8896)

This commit is contained in:
ulferts
2020-12-17 12:24:34 +01:00
committed by GitHub
parent 422084b8cf
commit 69fd5d6d86
5 changed files with 56 additions and 22 deletions
@@ -69,7 +69,8 @@ module OpenProject::TextFormatting
def transformers
[
todo_list_transformer
todo_list_transformer,
code_block_transformer
]
end
@@ -120,6 +121,22 @@ module OpenProject::TextFormatting
}
end
# Prevent nested pre + code.
# In such a case, the code is removed.
def code_block_transformer
lambda { |env|
name = env[:node_name]
code = env[:node]
next unless name == 'code'
parent = code.parent
if parent&.name == 'pre'
parent.children = code.children
end
}
end
end
end
end
@@ -43,20 +43,20 @@ module OpenProject::TextFormatting::Formats::Markdown
end
def filters
[
:markdown,
:sanitization,
:task_list,
:table_of_contents,
:macro,
:mention,
:pattern_matcher,
:syntax_highlight,
:attachment,
:relative_link,
:figure_wrapped,
:bem_css,
:autolink
%i[
markdown
sanitization
task_list
table_of_contents
macro
mention
pattern_matcher
syntax_highlight
attachment
relative_link
figure_wrapped
bem_css
autolink
]
end
@@ -87,9 +87,9 @@ describe OpenProject::TextFormatting,
Text before
</p>
<pre class="op-uc-code-block"><code class="op-uc-code">
<pre class="op-uc-code-block">
some code
</code></pre>
</pre>
<p class="op-uc-p">
Text after
@@ -121,7 +121,7 @@ describe OpenProject::TextFormatting,
Text before
</p>
<pre lang="ruby" class="highlight highlight-ruby op-uc-code-block">
<pre lang="ruby" class="highlight highlight-ruby op-uc-code-block">
<span class="k">def</span> <span class="nf">foobar</span>
<span class="n">some</span> <span class="n">ruby</span> <span class="n">code</span>
<span class="k">end</span>
@@ -134,4 +134,21 @@ describe OpenProject::TextFormatting,
end
end
end
context 'blubs' do
it_behaves_like 'format_text produces' do
let(:raw) do
"\n\n git clone git@github.com:opf/openproject.git\n\n"
end
let(:expected) do
<<~EXPECTED
<pre class="op-uc-code-block">
git clone git@github.com:opf/openproject.git
</pre>
EXPECTED
end
end
end
end
@@ -86,10 +86,10 @@ describe OpenProject::TextFormatting,
<<~EXPECTED
<p class="op-uc-p">Some text before</p>
<pre class='op-uc-code-block'><code class='op-uc-code'>
<pre class='op-uc-code-block'>
&lt;h#{level}&gt;The heading &lt;/h#{level}&gt;
</code></pre>
</pre>
<p class="op-uc-p">more text</p>
EXPECTED
@@ -537,11 +537,11 @@ describe OpenProject::TextFormatting,
<<~EXPECTED
<p class='op-uc-p'><a class="wiki-page op-uc-link" href="/projects/#{project.identifier}/wiki/cookbook-documentation">CookBook documentation</a></p>
<p class='op-uc-p'><a class="issue work_package preview-trigger op-uc-link" href="/work_packages/#{work_package.id}">##{work_package.id}</a></p>
<pre class="op-uc-code-block"><code class="op-uc-code">
<pre class="op-uc-code-block">
[[CookBook documentation]]
##{work_package.id}
</code></pre>
</pre>
EXPECTED
end