summaryrefslogtreecommitdiff
path: root/spec/lib/banzai
diff options
context:
space:
mode:
authorBrett Walker <bwalker@gitlab.com>2019-01-04 20:54:02 -0600
committerBrett Walker <bwalker@gitlab.com>2019-01-08 16:33:28 -0600
commit34ab6dfa051c29d27353a9f555e713f36c7954a4 (patch)
tree1d96ede7c9e421fc8ee42578c564783c634e575a /spec/lib/banzai
parent1b3affafab0f28c690ce93cc98ac6bd09cbda59f (diff)
downloadgitlab-ce-34ab6dfa051c29d27353a9f555e713f36c7954a4.tar.gz
Properly process footnotes in markdown
All the ids and classes were stripped. Add them back in and make ids unique
Diffstat (limited to 'spec/lib/banzai')
-rw-r--r--spec/lib/banzai/filter/footnote_filter_spec.rb48
-rw-r--r--spec/lib/banzai/filter/sanitization_filter_spec.rb38
2 files changed, 85 insertions, 1 deletions
diff --git a/spec/lib/banzai/filter/footnote_filter_spec.rb b/spec/lib/banzai/filter/footnote_filter_spec.rb
new file mode 100644
index 00000000000..0b7807b2c4c
--- /dev/null
+++ b/spec/lib/banzai/filter/footnote_filter_spec.rb
@@ -0,0 +1,48 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe Banzai::Filter::FootnoteFilter do
+ include FilterSpecHelper
+
+ # first[^1] and second[^second]
+ # [^1]: one
+ # [^second]: two
+ let(:footnote) do
+ <<-EOF.strip_heredoc
+ <p>first<sup><a href="#fn1" id="fnref1">1</a></sup> and second<sup><a href="#fn2" id="fnref2">2</a></sup></p>
+ <ol>
+ <li id="fn1">
+ <p>one <a href="#fnref1">↩</a></p>
+ </li>
+ <li id="fn2">
+ <p>two <a href="#fnref2">↩</a></p>
+ </li>
+ </ol>
+ EOF
+ end
+
+ context 'when footnotes exist' do
+ let(:doc) { filter(footnote) }
+ let(:link_node) { doc.css('sup > a').first }
+ let(:identifier) { link_node[:id].delete_prefix('fnref1-') }
+
+ it 'adds identifier to footnotes' do
+ expect(link_node[:id]).to eq "fnref1-#{identifier}"
+ expect(link_node[:href]).to eq "#fn1-#{identifier}"
+ expect(doc.css("li[id=fn1-#{identifier}]")).not_to be_empty
+ expect(doc.css("li[id=fn1-#{identifier}] a[href=\"#fnref1-#{identifier}\"]")).not_to be_empty
+ end
+
+ it 'uses the same identifier for all footnotes' do
+ expect(doc.css("li[id=fn2-#{identifier}]")).not_to be_empty
+ expect(doc.css("li[id=fn2-#{identifier}] a[href=\"#fnref2-#{identifier}\"]")).not_to be_empty
+ end
+
+ it 'adds section and classes' do
+ expect(doc.css("section[class=footnotes]")).not_to be_empty
+ expect(doc.css("sup[class=footnote-ref]").count).to eq 2
+ expect(doc.css("a[class=footnote-backref]").count).to eq 2
+ end
+ end
+end
diff --git a/spec/lib/banzai/filter/sanitization_filter_spec.rb b/spec/lib/banzai/filter/sanitization_filter_spec.rb
index 0b3c2390304..bfec7384443 100644
--- a/spec/lib/banzai/filter/sanitization_filter_spec.rb
+++ b/spec/lib/banzai/filter/sanitization_filter_spec.rb
@@ -246,7 +246,7 @@ describe Banzai::Filter::SanitizationFilter do
'protocol-based JS injection: spaces and entities' => {
input: '<a href=" &#14; javascript:alert(\'XSS\');">foo</a>',
- output: '<a href>foo</a>'
+ output: '<a href="">foo</a>'
},
'protocol whitespace' => {
@@ -300,5 +300,41 @@ describe Banzai::Filter::SanitizationFilter do
expect(act.to_html).to eq exp
end
+
+ describe 'footnotes' do
+ it 'allows id property on links' do
+ exp = %q{<a href="#fn1" id="fnref1">foo/bar.md</a>}
+ act = filter(exp)
+
+ expect(act.to_html).to eq exp
+ end
+
+ it 'allows id property on li element' do
+ exp = %q{<ol><li id="fn1">footnote</li></ol>}
+ act = filter(exp)
+
+ expect(act.to_html).to eq exp
+ end
+
+ it 'only allows valid footnote formats for links' do
+ exp = %q{<a href="#fn1">link</a>}
+
+ %w[fnrefx test xfnref1].each do |id|
+ act = filter(%Q{<a href="#fn1" id="#{id}">link</a>})
+
+ expect(act.to_html).to eq exp
+ end
+ end
+
+ it 'only allows valid footnote formats for li' do
+ exp = %q{<ol><li>footnote</li></ol>}
+
+ %w[fnx test xfn1].each do |id|
+ act = filter(%Q{<ol><li id="#{id}">footnote</li></ol>})
+
+ expect(act.to_html).to eq exp
+ end
+ end
+ end
end
end