Skip to content

Commit 69c19b3

Browse files
committed
Merge branch '57634-issue-when-viewing-a-document-with-footnotes-actionview-template-error-undefined-method-at_css-for-nil-nilclass' into 'master'
Issue when viewing a document with footnotes Closes #57634 See merge request gitlab-org/gitlab-ce!25199
2 parents 66b20a6 + f8241f2 commit 69c19b3

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

lib/banzai/filter/footnote_filter.rb

+16-7
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,30 @@ def call
2929
# Sanitization stripped off the section wrapper - add it back in
3030
first_footnote.parent.wrap('<section class="footnotes">')
3131
rand_suffix = "-#{random_number}"
32+
modified_footnotes = {}
3233

3334
doc.css('sup > a[id]').each do |link_node|
3435
ref_num = link_node[:id].delete_prefix(FOOTNOTE_LINK_ID_PREFIX)
3536
footnote_node = doc.at_css("li[id=#{fn_id(ref_num)}]")
36-
backref_node = footnote_node.at_css("a[href=\"##{fnref_id(ref_num)}\"]")
3737

38-
if ref_num =~ INTEGER_PATTERN && footnote_node && backref_node
39-
link_node[:href] += rand_suffix
40-
link_node[:id] += rand_suffix
41-
footnote_node[:id] += rand_suffix
42-
backref_node[:href] += rand_suffix
38+
if INTEGER_PATTERN.match?(ref_num) && (footnote_node || modified_footnotes[ref_num])
39+
link_node[:href] += rand_suffix
40+
link_node[:id] += rand_suffix
4341

4442
# Sanitization stripped off class - add it back in
4543
link_node.parent.append_class('footnote-ref')
46-
backref_node.append_class('footnote-backref')
44+
45+
unless modified_footnotes[ref_num]
46+
footnote_node[:id] += rand_suffix
47+
backref_node = footnote_node.at_css("a[href=\"##{fnref_id(ref_num)}\"]")
48+
49+
if backref_node
50+
backref_node[:href] += rand_suffix
51+
backref_node.append_class('footnote-backref')
52+
end
53+
54+
modified_footnotes[ref_num] = true
55+
end
4756
end
4857
end
4958

spec/lib/banzai/filter/footnote_filter_spec.rb

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
let(:footnote) do
1212
<<~EOF
1313
<p>first<sup><a href="#fn1" id="fnref1">1</a></sup> and second<sup><a href="#fn2" id="fnref2">2</a></sup></p>
14+
<p>same reference<sup><a href="#fn1" id="fnref1">1</a></sup></p>
1415
<ol>
1516
<li id="fn1">
1617
<p>one <a href="#fnref1">↩</a></p>
@@ -25,6 +26,7 @@
2526
let(:filtered_footnote) do
2627
<<~EOF
2728
<p>first<sup class="footnote-ref"><a href="#fn1-#{identifier}" id="fnref1-#{identifier}">1</a></sup> and second<sup class="footnote-ref"><a href="#fn2-#{identifier}" id="fnref2-#{identifier}">2</a></sup></p>
29+
<p>same reference<sup class="footnote-ref"><a href="#fn1-#{identifier}" id="fnref1-#{identifier}">1</a></sup></p>
2830
<section class="footnotes"><ol>
2931
<li id="fn1-#{identifier}">
3032
<p>one <a href="#fnref1-#{identifier}" class="footnote-backref">↩</a></p>

0 commit comments

Comments
 (0)