2015-05-12 19:07:48 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
require 'nokogiri'
|
|
|
|
|
|
|
|
module Gitlab
|
2017-07-10 10:24:02 -04:00
|
|
|
describe Asciidoc do
|
2015-05-12 19:07:48 -04:00
|
|
|
let(:input) { '<b>ascii</b>' }
|
|
|
|
let(:context) { {} }
|
|
|
|
let(:html) { 'H<sub>2</sub>O' }
|
|
|
|
|
|
|
|
context "without project" do
|
2016-11-28 12:41:29 -05:00
|
|
|
before do
|
|
|
|
allow_any_instance_of(ApplicationSetting).to receive(:current).and_return(::ApplicationSetting.create_from_defaults)
|
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "converts the input using Asciidoctor and default options" do
|
2015-06-23 01:24:39 -04:00
|
|
|
expected_asciidoc_opts = {
|
|
|
|
safe: :secure,
|
2016-12-08 19:15:08 -05:00
|
|
|
backend: :gitlab_html5,
|
2015-06-23 01:24:39 -04:00
|
|
|
attributes: described_class::DEFAULT_ADOC_ATTRS
|
|
|
|
}
|
2015-05-12 19:07:48 -04:00
|
|
|
|
|
|
|
expect(Asciidoctor).to receive(:convert)
|
|
|
|
.with(input, expected_asciidoc_opts).and_return(html)
|
|
|
|
|
2017-05-02 10:52:19 -04:00
|
|
|
expect(render(input, context)).to eq(html)
|
2015-05-12 19:07:48 -04:00
|
|
|
end
|
|
|
|
|
2017-05-02 18:25:58 -04:00
|
|
|
context "with asciidoc_opts" do
|
|
|
|
it "merges the options with default ones" do
|
|
|
|
expected_asciidoc_opts = {
|
|
|
|
safe: :secure,
|
|
|
|
backend: :gitlab_html5,
|
|
|
|
attributes: described_class::DEFAULT_ADOC_ATTRS
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(Asciidoctor).to receive(:convert)
|
|
|
|
.with(input, expected_asciidoc_opts).and_return(html)
|
|
|
|
|
|
|
|
render(input, context)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-08 15:33:29 -05:00
|
|
|
context "XSS" do
|
|
|
|
links = {
|
|
|
|
'links' => {
|
|
|
|
input: 'link:mylink"onmouseover="alert(1)[Click Here]',
|
|
|
|
output: "<div>\n<p><a href=\"mylink\">Click Here</a></p>\n</div>"
|
|
|
|
},
|
|
|
|
'images' => {
|
|
|
|
input: 'image:https://localhost.com/image.png[Alt text" onerror="alert(7)]',
|
2017-05-02 10:52:19 -04:00
|
|
|
output: "<img src=\"https://localhost.com/image.png\" alt=\"Alt text\">"
|
2017-02-08 15:33:29 -05:00
|
|
|
},
|
|
|
|
'pre' => {
|
|
|
|
input: '```mypre"><script>alert(3)</script>',
|
|
|
|
output: "<div>\n<div>\n<pre lang=\"mypre\">\"><code></code></pre>\n</div>\n</div>"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
links.each do |name, data|
|
|
|
|
it "does not convert dangerous #{name} into HTML" do
|
2017-05-02 10:52:19 -04:00
|
|
|
expect(render(data[:input], context)).to include(data[:output])
|
2017-02-08 15:33:29 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-05-02 10:52:19 -04:00
|
|
|
|
|
|
|
context 'external links' do
|
|
|
|
it 'adds the `rel` attribute to the link' do
|
|
|
|
output = render('link:https://google.com[Google]', context)
|
|
|
|
|
2017-05-02 18:25:58 -04:00
|
|
|
expect(output).to include('rel="nofollow noreferrer noopener"')
|
2017-05-02 10:52:19 -04:00
|
|
|
end
|
|
|
|
end
|
2017-05-09 08:14:24 -04:00
|
|
|
|
|
|
|
context 'LaTex code' do
|
|
|
|
it 'adds class js-render-math to the output' do
|
|
|
|
input = <<~MD
|
|
|
|
:stem: latexmath
|
|
|
|
|
|
|
|
[stem]
|
|
|
|
++++
|
|
|
|
\sqrt{4} = 2
|
|
|
|
++++
|
|
|
|
|
|
|
|
another part
|
|
|
|
|
|
|
|
[latexmath]
|
|
|
|
++++
|
|
|
|
\beta_x \gamma
|
|
|
|
++++
|
|
|
|
|
|
|
|
stem:[2+2] is 4
|
|
|
|
MD
|
|
|
|
|
|
|
|
expect(render(input, context)).to include('<pre data-math-style="display" class="code math js-render-math"><code>eta_x gamma</code></pre>')
|
|
|
|
expect(render(input, context)).to include('<p><code data-math-style="inline" class="code math js-render-math">2+2</code> is 4</p>')
|
|
|
|
end
|
|
|
|
end
|
2018-02-14 14:41:13 -05:00
|
|
|
|
|
|
|
context 'outfilesuffix' do
|
|
|
|
it 'defaults to adoc' do
|
|
|
|
output = render("Inter-document reference <<README.adoc#>>", context)
|
|
|
|
|
|
|
|
expect(output).to include("a href=\"README.adoc\"")
|
|
|
|
end
|
|
|
|
end
|
2015-05-12 19:07:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def render(*args)
|
|
|
|
described_class.render(*args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|