2021-03-11 19:13:27 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
RSpec.describe Banzai::Pipeline::PlainMarkdownPipeline, feature_category: :team_planning do
|
2021-03-11 19:13:27 +05:30
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
describe 'backslash escapes', :aggregate_failures do
|
|
|
|
let_it_be(:project) { create(:project, :public) }
|
|
|
|
let_it_be(:issue) { create(:issue, project: project) }
|
2021-03-11 19:13:27 +05:30
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
it 'converts all escapable punctuation to literals' do
|
|
|
|
markdown = Banzai::Filter::MarkdownPreEscapeFilter::ESCAPABLE_CHARS.pluck(:escaped).join
|
2021-03-11 19:13:27 +05:30
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
result = described_class.call(markdown, project: project)
|
|
|
|
output = result[:output].to_html
|
2021-03-11 19:13:27 +05:30
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
Banzai::Filter::MarkdownPreEscapeFilter::ESCAPABLE_CHARS.pluck(:char).each do |char|
|
|
|
|
char = '&' if char == '&'
|
|
|
|
|
|
|
|
expect(output).to include("<span>#{char}</span>")
|
|
|
|
end
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
expect(result[:escaped_literals]).to be_truthy
|
|
|
|
end
|
2021-03-11 19:13:27 +05:30
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
it 'ensure we handle all the GitLab reference characters', :eager_load do
|
|
|
|
reference_chars = ObjectSpace.each_object(Class).map do |klass|
|
|
|
|
next unless klass.included_modules.include?(Referable)
|
|
|
|
next unless klass.respond_to?(:reference_prefix)
|
|
|
|
next unless klass.reference_prefix.length == 1
|
2021-03-11 19:13:27 +05:30
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
klass.reference_prefix
|
|
|
|
end.compact
|
2021-04-17 20:07:23 +05:30
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
reference_chars.all? do |char|
|
2023-03-17 16:20:25 +05:30
|
|
|
Banzai::Filter::MarkdownPreEscapeFilter::TARGET_CHARS.include?(char)
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
2022-03-02 08:16:31 +05:30
|
|
|
end
|
2021-03-11 19:13:27 +05:30
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
it 'does not convert non-reference/latex punctuation to spans' do
|
|
|
|
markdown = %q(\"\'\*\+\,\-\.\/\:\;\<\=\>\?\[\]\`\|) + %q[\(\)\\\\]
|
2021-03-11 19:13:27 +05:30
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
result = described_class.call(markdown, project: project)
|
|
|
|
output = result[:output].to_html
|
2021-03-11 19:13:27 +05:30
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
expect(output).not_to include('<span>')
|
|
|
|
expect(result[:escaped_literals]).to be_falsey
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
2021-12-11 22:18:48 +05:30
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
it 'does not convert other characters to literals' do
|
|
|
|
markdown = %q(\→\A\a\ \3\φ\«)
|
|
|
|
expected = '\→\A\a\ \3\φ\«'
|
2021-12-11 22:18:48 +05:30
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
result = correct_html_included(markdown, expected)
|
|
|
|
expect(result[:escaped_literals]).to be_falsey
|
2021-12-11 22:18:48 +05:30
|
|
|
end
|
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
describe 'backslash escapes are untouched in code blocks, code spans, autolinks, or raw HTML' do
|
2022-03-02 08:16:31 +05:30
|
|
|
where(:markdown, :expected) do
|
|
|
|
%q(`` \@\! ``) | %q(<code>\@\!</code>)
|
|
|
|
%q( \@\!) | %Q(<code>\\@\\!\n</code>)
|
|
|
|
%Q(~~~\n\\@\\!\n~~~) | %Q(<code>\\@\\!\n</code>)
|
2023-03-17 16:20:25 +05:30
|
|
|
%q($1+\$2$) | %q(<code data-math-style="inline">1+\\$2</code>)
|
2022-03-02 08:16:31 +05:30
|
|
|
%q(<http://example.com?find=\@>) | %q(<a href="http://example.com?find=%5C@">http://example.com?find=\@</a>)
|
|
|
|
%q[<a href="/bar\@)">] | %q[<a href="/bar%5C@)">]
|
2021-12-11 22:18:48 +05:30
|
|
|
end
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
with_them do
|
|
|
|
it { correct_html_included(markdown, expected) }
|
|
|
|
end
|
2021-12-11 22:18:48 +05:30
|
|
|
end
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
describe 'work in all other contexts, including URLs and link titles, link references, and info strings in fenced code blocks' do
|
|
|
|
let(:markdown) { %Q(``` foo\\@bar\nfoo\n```) }
|
|
|
|
|
|
|
|
it 'renders correct html' do
|
|
|
|
correct_html_included(markdown, %Q(<pre data-sourcepos="1:1-3:3" lang="foo@bar"><code>foo\n</code></pre>))
|
|
|
|
end
|
|
|
|
|
|
|
|
where(:markdown, :expected) do
|
|
|
|
%q![foo](/bar\@ "\@title")! | %q(<a href="/bar@" title="@title">foo</a>)
|
|
|
|
%Q![foo]\n\n[foo]: /bar\\@ "\\@title"! | %q(<a href="/bar@" title="@title">foo</a>)
|
2021-12-11 22:18:48 +05:30
|
|
|
end
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
with_them do
|
|
|
|
it { correct_html_included(markdown, expected) }
|
|
|
|
end
|
2021-12-11 22:18:48 +05:30
|
|
|
end
|
|
|
|
end
|
2022-03-02 08:16:31 +05:30
|
|
|
|
|
|
|
def correct_html_included(markdown, expected)
|
|
|
|
result = described_class.call(markdown, {})
|
|
|
|
|
|
|
|
expect(result[:output].to_html).to include(expected)
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|