From 6ed4ec3e0b1340f96b7c043ef51d1b33bbe85fde Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Mon, 19 Sep 2022 23:18:09 +0000 Subject: Add latest changes from gitlab-org/gitlab@15-4-stable-ee --- spec/lib/banzai/color_parser_spec.rb | 2 +- .../banzai/filter/blockquote_fence_filter_spec.rb | 4 + spec/lib/banzai/filter/kroki_filter_spec.rb | 8 + spec/lib/banzai/filter/math_filter_spec.rb | 298 ++++++++++++--------- spec/lib/banzai/filter/output_safety_spec.rb | 2 +- spec/lib/banzai/filter/plantuml_filter_spec.rb | 10 + .../banzai/filter/repository_link_filter_spec.rb | 14 +- spec/lib/banzai/filter_array_spec.rb | 2 +- .../banzai/pipeline/pre_process_pipeline_spec.rb | 17 ++ spec/lib/banzai/pipeline_spec.rb | 2 +- spec/lib/banzai/querying_spec.rb | 2 +- spec/lib/banzai/renderer_spec.rb | 2 +- 12 files changed, 230 insertions(+), 133 deletions(-) (limited to 'spec/lib/banzai') diff --git a/spec/lib/banzai/color_parser_spec.rb b/spec/lib/banzai/color_parser_spec.rb index 95b3955d8fe..3914aee2d4c 100644 --- a/spec/lib/banzai/color_parser_spec.rb +++ b/spec/lib/banzai/color_parser_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'spec_helper' +require 'fast_spec_helper' RSpec.describe Banzai::ColorParser do describe '.parse' do diff --git a/spec/lib/banzai/filter/blockquote_fence_filter_spec.rb b/spec/lib/banzai/filter/blockquote_fence_filter_spec.rb index 2d326bd77a6..5712ed7da1f 100644 --- a/spec/lib/banzai/filter/blockquote_fence_filter_spec.rb +++ b/spec/lib/banzai/filter/blockquote_fence_filter_spec.rb @@ -14,6 +14,10 @@ RSpec.describe Banzai::Filter::BlockquoteFenceFilter do expect(output).to eq(expected) end + it 'does not require newlines at start or end of string' do + expect(filter(">>>\ntest\n>>>")).to eq("\n> test\n") + end + it 'allows trailing whitespace on blockquote fence lines' do expect(filter(">>> \ntest\n>>> ")).to eq("\n> test\n") end diff --git a/spec/lib/banzai/filter/kroki_filter_spec.rb b/spec/lib/banzai/filter/kroki_filter_spec.rb index 1fb61ad1991..3f4f3aafdd6 100644 --- a/spec/lib/banzai/filter/kroki_filter_spec.rb +++ b/spec/lib/banzai/filter/kroki_filter_spec.rb @@ -46,4 +46,12 @@ RSpec.describe Banzai::Filter::KrokiFilter do expect(doc.to_s).to start_with '#{text}") + + expect(doc.to_s).to start_with '2+2" - doc = filter(input) - - expect(doc.to_s).to eq input - end - - it 'removes surrounding dollar signs and adds class code, math and js-render-math' do - doc = filter("$2+2$") - - expect(doc.to_s).to eq '2+2' - end - - it 'only removes surrounding dollar signs' do - doc = filter("test $2+2$ test") - before = doc.xpath('descendant-or-self::text()[1]').first - after = doc.xpath('descendant-or-self::text()[3]').first - - expect(before.to_s).to eq 'test ' - expect(after.to_s).to eq ' test' - end - - it 'only removes surrounding single dollar sign' do - doc = filter("test $$2+2$$ test") - before = doc.xpath('descendant-or-self::text()[1]').first - after = doc.xpath('descendant-or-self::text()[3]').first - - expect(before.to_s).to eq 'test $' - expect(after.to_s).to eq '$ test' - end - - it 'adds data-math-style inline attribute to inline math' do - doc = filter('$2+2$') - code = doc.xpath('descendant-or-self::code').first - - expect(code['data-math-style']).to eq 'inline' - end - - it 'adds class code and math to inline math' do - doc = filter('$2+2$') - code = doc.xpath('descendant-or-self::code').first - - expect(code[:class]).to include("code") - expect(code[:class]).to include("math") - end - - it 'adds js-render-math class to inline math' do - doc = filter('$2+2$') - code = doc.xpath('descendant-or-self::code').first - - expect(code[:class]).to include("js-render-math") - end - - # Cases with faulty syntax. Should be a no-op - - it 'ignores cases with missing dolar sign at the end' do - input = "test $2+2 test" - doc = filter(input) - - expect(doc.to_s).to eq input - end - - it 'ignores cases with missing dolar sign at the beginning' do - input = "test 2+2$ test" - doc = filter(input) - - expect(doc.to_s).to eq input - end - - it 'ignores dollar signs if it is not adjacent' do - input = '

We check strictly $2+2 and 2+2$

' - doc = filter(input) - - expect(doc.to_s).to eq input - end - - it 'ignores dollar signs if they are inside another element' do - input = '

We check strictly $2+2$

' - doc = filter(input) - - expect(doc.to_s).to eq input - end - - # Display math - - it 'adds data-math-style display attribute to display math' do - doc = filter('
2+2
') - pre = doc.xpath('descendant-or-self::pre').first - - expect(pre['data-math-style']).to eq 'display' - end - - it 'adds js-render-math class to display math' do - doc = filter('
2+2
') - pre = doc.xpath('descendant-or-self::pre').first - - expect(pre[:class]).to include("js-render-math") - end - - it 'ignores code blocks that are not math' do - input = '
2+2
' - doc = filter(input) - - expect(doc.to_s).to eq input - end - - it 'requires the pre to contain both code and math' do - input = '
2+2
' - doc = filter(input) - - expect(doc.to_s).to eq input - end - - it 'dollar signs around to display math' do - doc = filter('$
2+2
$') - before = doc.xpath('descendant-or-self::text()[1]').first - after = doc.xpath('descendant-or-self::text()[3]').first - - expect(before.to_s).to eq '$' - expect(after.to_s).to eq '$' + shared_examples 'inline math' do + it 'removes surrounding dollar signs and adds class code, math and js-render-math' do + doc = filter(text) + expected = result_template.gsub('', '') + expected.gsub!('', '') + + expect(doc.to_s).to eq expected + end + end + + shared_examples 'display math' do + let_it_be(:template_prefix_with_pre) { '
' }
+    let_it_be(:template_prefix_with_code) { '' }
+    let(:use_pre_tags) { false }
+
+    it 'removes surrounding dollar signs and adds class code, math and js-render-math' do
+      doc = filter(text)
+
+      template_prefix = use_pre_tags ? template_prefix_with_pre : template_prefix_with_code
+      template_suffix = "#{'
' if use_pre_tags}" + expected = result_template.gsub('', template_prefix) + expected.gsub!('', template_suffix) + + expect(doc.to_s).to eq expected + end + end + + describe 'inline math using $...$ syntax' do + context 'with valid syntax' do + where(:text, :result_template) do + '$2+2$' | '2+2' + '$22+1$ and $22 + a^2$' | '22+1 and 22 + a^2' + '$22 and $2+2$' | '$22 and 2+2' + '$2+2$ $22 and flightjs/Flight$22 $2+2$' | '2+2 $22 and flightjs/Flight$22 2+2' + '$1/2$ <b>test</b>' | '1/2 <b>test</b>' + '$a!$' | 'a!' + '$x$' | 'x' + end + + with_them do + it_behaves_like 'inline math' + end + end + + it 'does not handle dollar literals properly' do + doc = filter('$20+30\$$') + expected = '20+30\\$' + + expect(doc.to_s).to eq expected + end + end + + describe 'inline math using $`...`$ syntax' do + context 'with valid syntax' do + where(:text, :result_template) do + '$2+2$' | '2+2' + '$22+1$ and $22 + a^2$' | '22+1 and 22 + a^2' + '$22 and $2+2$' | '$22 and 2+2' + '$2+2$ $22 and flightjs/Flight$22 $2+2$' | '2+2 $22 and flightjs/Flight$22 2+2' + 'test $$2+2$$ test' | 'test $2+2$ test' + end + + with_them do + it_behaves_like 'inline math' + end + end + end + + describe 'inline display math using $$...$$ syntax' do + context 'with valid syntax' do + where(:text, :result_template) do + '$$2+2$$' | '2+2' + '$$ 2+2 $$' | '2+2' + '$$22+1$$ and $$22 + a^2$$' | '22+1 and 22 + a^2' + '$22 and $$2+2$$' | '$22 and 2+2' + '$$2+2$$ $22 and flightjs/Flight$22 $$2+2$$' | '2+2 $22 and flightjs/Flight$22 2+2' + 'flightjs/Flight$22 and $$a^2 + b^2 = c^2$$' | 'flightjs/Flight$22 and a^2 + b^2 = c^2' + '$$a!$$' | 'a!' + '$$x$$' | 'x' + '$$20,000 and $$30,000' | '20,000 and30,000' + end + + with_them do + it_behaves_like 'display math' + end + end + end + + describe 'block display math using $$\n...\n$$ syntax' do + context 'with valid syntax' do + where(:text, :result_template) do + "$$\n2+2\n$$" | "2+2" + end + + with_them do + it_behaves_like 'display math' do + let(:use_pre_tags) { true } + end + end + end + end + + describe 'display math using ```math...``` syntax' do + it 'adds data-math-style display attribute to display math' do + doc = filter('
2+2
') + pre = doc.xpath('descendant-or-self::pre').first + + expect(pre['data-math-style']).to eq 'display' + end + + it 'adds js-render-math class to display math' do + doc = filter('
2+2
') + pre = doc.xpath('descendant-or-self::pre').first + + expect(pre[:class]).to include("js-render-math") + end + + it 'ignores code blocks that are not math' do + input = '
2+2
' + doc = filter(input) + + expect(doc.to_s).to eq input + end + + it 'requires the pre to contain both code and math' do + input = '
2+2
' + doc = filter(input) + + expect(doc.to_s).to eq input + end + + it 'dollar signs around to display math' do + doc = filter('$
2+2
$') + before = doc.xpath('descendant-or-self::text()[1]').first + after = doc.xpath('descendant-or-self::text()[3]').first + + expect(before.to_s).to eq '$' + expect(after.to_s).to eq '$' + end + end + + describe 'unrecognized syntax' do + where(:text) do + [ + '2+2', + 'test $2+2 test', + 'test 2+2$ test', + '$2+2$', + '$20,000 and $30,000', + '$20,000 in $USD', + '$ a^2 $', + "test $$\n2+2\n$$", + "$\n$", + '$$$' + ] + end + + with_them do + it 'is ignored' do + expect(filter(text).to_s).to eq text + end + end + end + + it 'handles multiple styles in one text block' do + doc = filter('$2+2$ + $3+3$ + $$4+4$$') + + expect(doc.search('.js-render-math').count).to eq(3) + expect(doc.search('[data-math-style="inline"]').count).to eq(2) + expect(doc.search('[data-math-style="display"]').count).to eq(1) end it 'limits how many elements can be marked as math' do @@ -134,4 +185,11 @@ RSpec.describe Banzai::Filter::MathFilter do expect(doc.search('.js-render-math').count).to eq(2) end + + it 'does not recognize new syntax when feature flag is off' do + stub_feature_flags(markdown_dollar_math: false) + doc = filter('$1+2$') + + expect(doc.to_s).to eq '$1+2$' + end end diff --git a/spec/lib/banzai/filter/output_safety_spec.rb b/spec/lib/banzai/filter/output_safety_spec.rb index 5b7b7298411..8186935f4b2 100644 --- a/spec/lib/banzai/filter/output_safety_spec.rb +++ b/spec/lib/banzai/filter/output_safety_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'spec_helper' +require 'fast_spec_helper' RSpec.describe Banzai::Filter::OutputSafety do subject do diff --git a/spec/lib/banzai/filter/plantuml_filter_spec.rb b/spec/lib/banzai/filter/plantuml_filter_spec.rb index dcfeb2ce3ba..4373af90cde 100644 --- a/spec/lib/banzai/filter/plantuml_filter_spec.rb +++ b/spec/lib/banzai/filter/plantuml_filter_spec.rb @@ -15,6 +15,16 @@ RSpec.describe Banzai::Filter::PlantumlFilter do expect(doc.to_s).to eq output end + it 'allows the lang attribute on the code tag to support RST files processed by gitlab-markup gem' do + stub_application_setting(plantuml_enabled: true, plantuml_url: "http://localhost:8080") + + input = '
Bob -> Sara : Hello
' + output = '' + doc = filter(input) + + expect(doc.to_s).to eq output + end + it 'does not replace plantuml pre tag with img tag if disabled' do stub_application_setting(plantuml_enabled: false) diff --git a/spec/lib/banzai/filter/repository_link_filter_spec.rb b/spec/lib/banzai/filter/repository_link_filter_spec.rb index 815053aac2f..c220263b238 100644 --- a/spec/lib/banzai/filter/repository_link_filter_spec.rb +++ b/spec/lib/banzai/filter/repository_link_filter_spec.rb @@ -8,14 +8,14 @@ RSpec.describe Banzai::Filter::RepositoryLinkFilter do def filter(doc, contexts = {}) contexts.reverse_merge!({ - commit: commit, - project: project, - current_user: user, - group: group, - wiki: wiki, - ref: ref, + commit: commit, + project: project, + current_user: user, + group: group, + wiki: wiki, + ref: ref, requested_path: requested_path, - only_path: only_path + only_path: only_path }) described_class.call(doc, contexts) diff --git a/spec/lib/banzai/filter_array_spec.rb b/spec/lib/banzai/filter_array_spec.rb index 47bc5633300..f341d5d51a0 100644 --- a/spec/lib/banzai/filter_array_spec.rb +++ b/spec/lib/banzai/filter_array_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'spec_helper' +require 'fast_spec_helper' RSpec.describe Banzai::FilterArray do describe '#insert_after' do diff --git a/spec/lib/banzai/pipeline/pre_process_pipeline_spec.rb b/spec/lib/banzai/pipeline/pre_process_pipeline_spec.rb index 5021ef3a79a..303d0fcb6c2 100644 --- a/spec/lib/banzai/pipeline/pre_process_pipeline_spec.rb +++ b/spec/lib/banzai/pipeline/pre_process_pipeline_spec.rb @@ -32,4 +32,21 @@ RSpec.describe Banzai::Pipeline::PreProcessPipeline do expect(result[:output]).to eq('foo foo f...') end + + context 'when multiline blockquote' do + it 'data-sourcepos references correct line in source markdown' do + markdown = <<~MD + >>> + foo + >>> + MD + + pipeline_output = described_class.call(markdown, {})[:output] + pipeline_output = Banzai::Pipeline::PlainMarkdownPipeline.call(pipeline_output, {})[:output] + sourcepos = pipeline_output.at('blockquote')['data-sourcepos'] + source_line = sourcepos.split(':').first.to_i + + expect(markdown.lines[source_line - 1]).to eq "foo\n" + end + end end diff --git a/spec/lib/banzai/pipeline_spec.rb b/spec/lib/banzai/pipeline_spec.rb index 7d4df2ca5ce..b2c970e4394 100644 --- a/spec/lib/banzai/pipeline_spec.rb +++ b/spec/lib/banzai/pipeline_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'spec_helper' +require 'fast_spec_helper' RSpec.describe Banzai::Pipeline do describe '.[]' do diff --git a/spec/lib/banzai/querying_spec.rb b/spec/lib/banzai/querying_spec.rb index b76f6ec533c..fc7aaa94954 100644 --- a/spec/lib/banzai/querying_spec.rb +++ b/spec/lib/banzai/querying_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'spec_helper' +require 'fast_spec_helper' RSpec.describe Banzai::Querying do describe '.css' do diff --git a/spec/lib/banzai/renderer_spec.rb b/spec/lib/banzai/renderer_spec.rb index ae9cf4c5068..705f44baf16 100644 --- a/spec/lib/banzai/renderer_spec.rb +++ b/spec/lib/banzai/renderer_spec.rb @@ -76,7 +76,7 @@ RSpec.describe Banzai::Renderer do let(:object) { fake_object(fresh: true) } it 'uses the cache' do - expect(object).to receive(:refresh_markdown_cache!).never + expect(object).not_to receive(:refresh_markdown_cache!) is_expected.to eq('field_html') end -- cgit v1.2.3