[PATCH] markup_renderer: support bleach >= 5
橋本 憲一
hashimoto.kenichi at genetec.co.jp
Thu Nov 5 03:32:10 UTC 2026
# HG changeset patch
# User Hashimoto Kenichi <hashimoto.kenichi at genetec.co.jp>
# Date 1793601806 -32400
# Mon Nov 02 15:43:26 2026 +0900
# Branch stable
# Node ID 61465b09d41e47c23345c12305da895197ffb2e6
# Parent 4c5d75319b9eef45c7738d909d4f9e307280a4c7
markup_renderer: support bleach >= 5
bleach 5 dropped the 'styles' argument of clean() in favour of a CSS
sanitizer, so rendering failed with TypeError. Pass a CSSSanitizer that
allows the same 'color' property on bleach >= 5 and keep 'styles' for
older versions. The CSS sanitizer needs tinycss2, pulled in by the
bleach[css] extra.
bleach >= 5 keeps the allowed property when others are removed, and
orders attributes differently, so the style doctest now compares with
the exact output for each bleach major version.
Assisted-by: Claude:claude-opus-5-5
diff -r 4c5d75319b9e -r 61465b09d41e kallithea/lib/markup_renderer.py
--- a/kallithea/lib/markup_renderer.py Fri Apr 10 20:57:47 2026 +0200
+++ b/kallithea/lib/markup_renderer.py Mon Nov 02 15:43:26 2026 +0900
@@ -42,6 +42,17 @@
log = logging.getLogger(__name__)
+# CSS properties allowed in rendered markup. bleach >= 5 replaced the 'styles'
+# argument of clean() with a CSS sanitizer (which requires tinycss2).
+_ALLOWED_CSS_PROPERTIES = ['color']
+_BLEACH_MAJOR = int(bleach.__version__.split('.')[0])
+if _BLEACH_MAJOR >= 5:
+ from bleach.css_sanitizer import CSSSanitizer
+ _bleach_css_kwargs = {'css_sanitizer': CSSSanitizer(allowed_css_properties=_ALLOWED_CSS_PROPERTIES)}
+else:
+ _bleach_css_kwargs = {'styles': _ALLOWED_CSS_PROPERTIES}
+
+
class MarkupRenderer(object):
RESTRUCTUREDTEXT_DISALLOWED_DIRECTIVES = ['include', 'meta', 'raw']
@@ -116,8 +127,10 @@
it detects renderers based on file extension or mimetype.
At last it will just do a simple html replacing new lines with <br/>
- >>> MarkupRenderer.render('''<img id="a" style="margin-top:-1000px;color:red" src="http://example.com/test.jpg">''', '.md')
- '<p><img id="a" src="http://example.com/test.jpg" style=""></p>'
+ >>> html = MarkupRenderer.render('''<img id="a" style="margin-top:-1000px;color:red" src="http://example.com/test.jpg">''', '.md')
+ >>> html == ('<p><img id="a" style="color:red;" src="http://example.com/test.jpg"></p>' if _BLEACH_MAJOR >= 5 else
+ ... '<p><img id="a" src="http://example.com/test.jpg" style=""></p>')
+ True
>>> MarkupRenderer.render('''<img class="c d" src="file://localhost/test.jpg">''', 'b.mkd')
'<p><img class="c d"></p>'
>>> MarkupRenderer.render('''<a href="foo">foo</a>''', 'c.mkdn')
@@ -142,9 +155,8 @@
'strong', 'sub', 'sup', 'table', 'tbody', 'td', 'th',
'thead', 'tr', 'ul'],
attributes=['class', 'id', 'style', 'label', 'title', 'alt', 'href', 'src'],
- styles=['color'],
protocols=['http', 'https', 'mailto'],
- )
+ **_bleach_css_kwargs)
@classmethod
def plain(cls, source, universal_newline=True):
diff -r 4c5d75319b9e -r 61465b09d41e setup.py
--- a/setup.py Fri Apr 10 20:57:47 2026 +0200
+++ b/setup.py Mon Nov 02 15:43:26 2026 +0900
@@ -66,7 +66,7 @@
"mercurial >= 5.2, < 7.2",
"decorator >= 4.2.1, < 5.3",
"Paste >= 2.0.3, < 3.11",
- "bleach >= 3.2, < 5", # TODO: Upgrade and fix TypeError: clean() got an unexpected keyword argument 'styles'
+ "bleach[css] >= 3.2, < 7",
"Click >= 7.0, < 8.4",
"ipaddr >= 2.2.0, < 2.3",
"paginate >= 0.5, < 0.6",
----------------------------------------
橋本 憲一
hashimoto.kenichi at genetec.co.jp
株式会社 ゼネテック
システムソリューション事業本部 第2ソフトウェア事業部
エンジニアリングバリューチェーン推進部
〒160-0023
東京都新宿区西新宿6-14-1 新宿グリーンタワービルB1
TEL:03-6258-5630 携帯: 090-1408-0488
----------------------------------------
More information about the kallithea-general
mailing list