0

Sanitize gemを使用して、いくつかのHTMLをクリーンアップしています。アンカータグのhref属性で、以下を解析したいと思います。

<a href="#fn:1">1</a>

これは、Kramdowngemを使用して脚注を実装するために必要です。

ただし、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属性のコロンを受け入れるようにする方法はありますか?

4

1 に答える 1

4

これは、デフォルトで最も安全なことを行うSanitizeです。:の前のURLの部分がプロトコル(またはRFC 1738の用語のスキーム)であると想定し、プロトコルホワイトリストに含まれていないため、属性#fn全体が削除されます。href

#fnプロトコルホワイトリストに追加することで、このようなURLを許可できます。

allowed_protocols = {'a' => {'href' => ['#fn', 'http', 'https', 'mailto', :relative]}}
于 2011-05-16T03:16:55.020 に答える