Skip to content

Commit b91c7a5

Browse files
authored
clean_html: allow SVG tags and SVG attributes (#1890)
1 parent a806744 commit b91c7a5

File tree

4 files changed

+707
-1
lines changed

4 files changed

+707
-1
lines changed

nbconvert/filters/strings.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
"text_base64",
4040
]
4141

42+
from nbconvert.filters.svg_constants import ALLOWED_SVG_ATTRIBUTES, ALLOWED_SVG_TAGS
43+
4244

4345
def wrap_text(text, width=100):
4446
"""
@@ -85,9 +87,11 @@ def clean_html(element):
8587
element = str(element)
8688
return bleach.clean(
8789
element,
88-
tags=[*bleach.ALLOWED_TAGS, "div", "pre", "code", "span"],
90+
tags=[*bleach.ALLOWED_TAGS, *ALLOWED_SVG_TAGS, "div", "pre", "code", "span"],
91+
strip_comments=False,
8992
attributes={
9093
**bleach.ALLOWED_ATTRIBUTES,
94+
**{svg_tag: ALLOWED_SVG_ATTRIBUTES for svg_tag in ALLOWED_SVG_TAGS},
9195
"*": ["class", "id"],
9296
},
9397
)

nbconvert/filters/svg_constants.py

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
# These are lifted from html5lib's sanitizer module, which is deprecated.
2+
#
3+
# Quoth the migration guide (https://github.com/mozilla/bleach/blob/main/docs/migrating.rst#different-allow-lists):
4+
#
5+
# > If you want to stick to the html5lib sanitizer's allow lists, get them from the sanitizer code.
6+
# > It's probably best to copy them as static lists.
7+
8+
# TODO: Bleach currently drops attribute namespaces; cleaning SVGs will probably require more elbow grease.
9+
# See https://github.com/mozilla/bleach/issues/362
10+
11+
ALLOWED_SVG_TAGS = {
12+
"a",
13+
"animate",
14+
"animateColor",
15+
"animateMotion",
16+
"animateTransform",
17+
"circle",
18+
"clipPath",
19+
"defs",
20+
"desc",
21+
"ellipse",
22+
"font-face",
23+
"font-face-name",
24+
"font-face-src",
25+
"g",
26+
"glyph",
27+
"hkern",
28+
"line",
29+
"linearGradient",
30+
"marker",
31+
"metadata",
32+
"missing-glyph",
33+
"mpath",
34+
"path",
35+
"polygon",
36+
"polyline",
37+
"radialGradient",
38+
"rect",
39+
"set",
40+
"stop",
41+
"svg",
42+
"switch",
43+
"text",
44+
"title",
45+
"tspan",
46+
"use",
47+
}
48+
ALLOWED_SVG_ATTRIBUTES = {
49+
"accent-height",
50+
"accumulate",
51+
"additive",
52+
"alphabetic",
53+
"arabic-form",
54+
"ascent",
55+
"attributeName",
56+
"attributeType",
57+
"baseProfile",
58+
"bbox",
59+
"begin",
60+
"by",
61+
"calcMode",
62+
"cap-height",
63+
"class",
64+
"clip-path",
65+
"color",
66+
"color-rendering",
67+
"content",
68+
"cx",
69+
"cy",
70+
"d",
71+
"descent",
72+
"display",
73+
"dur",
74+
"dx",
75+
"dy",
76+
"end",
77+
"fill",
78+
"fill-opacity",
79+
"fill-rule",
80+
"font-family",
81+
"font-size",
82+
"font-stretch",
83+
"font-style",
84+
"font-variant",
85+
"font-weight",
86+
"from",
87+
"fx",
88+
"fy",
89+
"g1",
90+
"g2",
91+
"glyph-name",
92+
"gradientUnits",
93+
"hanging",
94+
"height",
95+
"horiz-adv-x",
96+
"horiz-origin-x",
97+
"id",
98+
"ideographic",
99+
"k",
100+
"keyPoints",
101+
"keySplines",
102+
"keyTimes",
103+
"lang",
104+
"marker-end",
105+
"marker-mid",
106+
"marker-start",
107+
"markerHeight",
108+
"markerUnits",
109+
"markerWidth",
110+
"mathematical",
111+
"max",
112+
"min",
113+
"name",
114+
"offset",
115+
"opacity",
116+
"orient",
117+
"origin",
118+
"overline-position",
119+
"overline-thickness",
120+
"panose-1",
121+
"path",
122+
"pathLength",
123+
"points",
124+
"preserveAspectRatio",
125+
"r",
126+
"refX",
127+
"refY",
128+
"repeatCount",
129+
"repeatDur",
130+
"requiredExtensions",
131+
"requiredFeatures",
132+
"restart",
133+
"rotate",
134+
"rx",
135+
"ry",
136+
"slope",
137+
"stemh",
138+
"stemv",
139+
"stop-color",
140+
"stop-opacity",
141+
"strikethrough-position",
142+
"strikethrough-thickness",
143+
"stroke",
144+
"stroke-dasharray",
145+
"stroke-dashoffset",
146+
"stroke-linecap",
147+
"stroke-linejoin",
148+
"stroke-miterlimit",
149+
"stroke-opacity",
150+
"stroke-width",
151+
"style",
152+
"systemLanguage",
153+
"target",
154+
"text-anchor",
155+
"to",
156+
"transform",
157+
"type",
158+
"u1",
159+
"u2",
160+
"underline-position",
161+
"underline-thickness",
162+
"unicode",
163+
"unicode-range",
164+
"units-per-em",
165+
"values",
166+
"version",
167+
"viewBox",
168+
"visibility",
169+
"width",
170+
"widths",
171+
"x",
172+
"x-height",
173+
"x1",
174+
"x2",
175+
"xlink:actuate",
176+
"xlink:arcrole",
177+
"xlink:href",
178+
"xlink:role",
179+
"xlink:show",
180+
"xlink:title",
181+
"xlink:type",
182+
"xml:base",
183+
"xml:lang",
184+
"xml:space",
185+
"y",
186+
"y1",
187+
"y2",
188+
"zoomAndPan",
189+
}

0 commit comments

Comments
 (0)