提交 cc036417 编写于 作者: B Brett Walker

Updates based on review comments

上级 459d6f98
......@@ -16,25 +16,25 @@ module Banzai
# can be used for a single render). So you get `id=fn1-4335` and `id=fn2-4335`.
#
class FootnoteFilter < HTML::Pipeline::Filter
INTEGER_PATTERN = /\A\d+\Z/.freeze
INTEGER_PATTERN = /\A\d+\z/.freeze
def call
return doc unless first_footnote = doc.at_css('ol > li[id=fn1]')
# Sanitization stripped off the section wrapper - add it back in
first_footnote.parent.wrap('<section class="footnotes">')
rand_suffix = "-#{random_number}"
doc.css('sup > a[id]').each do |link_node|
ref_num = link_node[:id].delete_prefix('fnref')
footnote_node = doc.at_css("li[id=fn#{ref_num}]")
backref_node = doc.at_css("li[id=fn#{ref_num}] a[href=\"#fnref#{ref_num}\"]")
backref_node = footnote_node.at_css("a[href=\"#fnref#{ref_num}\"]")
if ref_num =~ INTEGER_PATTERN && footnote_node && backref_node
rand_ref_num = "#{ref_num}-#{random_number}"
link_node[:href] = "#fn#{rand_ref_num}"
link_node[:id] = "fnref#{rand_ref_num}"
footnote_node[:id] = "fn#{rand_ref_num}"
backref_node[:href] = "#fnref#{rand_ref_num}"
link_node[:href] += rand_suffix
link_node[:id] += rand_suffix
footnote_node[:id] += rand_suffix
backref_node[:href] += rand_suffix
# Sanitization stripped off class - add it back in
link_node.parent.append_class('footnote-ref')
......
......@@ -10,8 +10,8 @@ module Banzai
UNSAFE_PROTOCOLS = %w(data javascript vbscript).freeze
TABLE_ALIGNMENT_PATTERN = /text-align: (?<alignment>center|left|right)/.freeze
FOOTNOTE_LINK_REFERENCE_PATTERN = /\Afnref\d\z/.freeze
FOOTNOTE_LI_REFERENCE_PATTERN = /\Afn\d\z/.freeze
FOOTNOTE_LINK_REFERENCE_PATTERN = /\Afnref\d+\z/.freeze
FOOTNOTE_LI_REFERENCE_PATTERN = /\Afn\d+\z/.freeze
def whitelist
strong_memoize(:whitelist) do
......
......@@ -335,6 +335,13 @@ describe Banzai::Filter::SanitizationFilter do
expect(act.to_html).to eq exp
end
end
it 'allows footnotes numbered higher than 9' do
exp = %q{<a href="#fn15" id="fnref15">link</a><ol><li id="fn15">footnote</li></ol>}
act = filter(exp)
expect(act.to_html).to eq exp
end
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册