Commit 66b84e4
committed
html_attribute: markdown.searialiers の内部変数を一時的に変更して実体参照を適切に処理
これまでは self._markdown.serializer が使われていたが、serializer は何
故か実体参照のパターンに一致する & を & に変換せずにそのまま出力す
る仕様になっている [1]。これを用いてHTMLに戻すと例えば元々のHTMLソース
で &var; となっていたものが処理後のHTMLソースで不正な実体参照
&var; になってしまい、ブラウザで正しく表示されなくなる可能性がある。例
えば reference/memory/addressof.html でこれが起こっている [2]。さらに、
後段でHTMLを解釈して処理しようとしたときにクラッシュする。
xml.etree.ElementTree が etree.tostring(method="xml") で正当なHTMLへの
変換に対応しているが、これを用いると <td></td> などが <td /> になって
しまう。代わりに method="html" を使うと <br /> などが <br> になってし
まう。また、どちらの method の場合でも HTML 属性の順序が変わってしまう
ので好ましくない。
この実装では代わりに markdown.searializers の内部変数
markdown.searializers.RE_AMP を書き換えることによって期待する動作を得
ている。上流の markdown.searializers で実装の変更があると動かなくなる
可能性があることに注意する。
[1] https://github.com/Python-Markdown/markdown/blob/a11431539d08e14b0bd821ceb101fa59d6a74c8a/markdown/serializers.py#L69-L70
[2] https://github.com/cpprefjp/cpprefjp.github.io/blob/335489b522dcbb648c19a49e39cc880e187911a5/reference/memory/addressof.html#L2631 parent 2ce6b71 commit 66b84e4
1 file changed
+28
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| |||
312 | 313 | | |
313 | 314 | | |
314 | 315 | | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
315 | 342 | | |
316 | 343 | | |
317 | 344 | | |
| |||
328 | 355 | | |
329 | 356 | | |
330 | 357 | | |
331 | | - | |
| 358 | + | |
332 | 359 | | |
333 | 360 | | |
334 | 361 | | |
| |||
0 commit comments