使用 Sanitize gem,我正在清理一些 HTML。在我的锚标签的 href 属性中,我希望解析以下内容:
<a href="#fn:1">1</a>
这是使用 Kramdown gem实现脚注所必需的。
但是,Sanitize 似乎不喜欢 href 属性中的冒号。它只是简单地输出<a>1</a>
,完全跳过 href 属性。
我的清理代码如下所示:
# Setup whitelist of html elements, attributes, and protocols that are allowed.
allowed_elements = ['h2', 'a', 'img', 'p', 'ul', 'ol', 'li', 'strong', 'em', 'cite',
'blockquote', 'code', 'pre', 'dl', 'dt', 'dd', 'br', 'hr', 'sup', 'div']
allowed_attributes = {'a' => ['href', 'rel', 'rev'], 'img' => ['src', 'alt'],
'sup' => ['id'], 'div' => ['class'], 'li' => ['id']}
allowed_protocols = {'a' => {'href' => ['http', 'https', 'mailto', :relative]}}
# Clean text of any unwanted html tags.
html = Sanitize.clean(html, :elements => allowed_elements, :attributes => allowed_attributes,
:protocols => allowed_protocols)
有没有办法让 Sanitize 接受 href 属性中的冒号?