Skip to content

Commit

Permalink
clean_html: allow SVG tags and SVG attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
akx committed Oct 27, 2022
1 parent a806744 commit 9809c53
Show file tree
Hide file tree
Showing 4 changed files with 699 additions and 1 deletion.
5 changes: 4 additions & 1 deletion nbconvert/filters/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
"text_base64",
]

from nbconvert.filters.svg_constants import ALLOWED_SVG_ATTRIBUTES, ALLOWED_SVG_TAGS


def wrap_text(text, width=100):
"""
Expand Down Expand Up @@ -85,9 +87,10 @@ def clean_html(element):
element = str(element)
return bleach.clean(
element,
tags=[*bleach.ALLOWED_TAGS, "div", "pre", "code", "span"],
tags=[*bleach.ALLOWED_TAGS, *ALLOWED_SVG_TAGS, "div", "pre", "code", "span"],
attributes={
**bleach.ALLOWED_ATTRIBUTES,
**{svg_tag: ALLOWED_SVG_ATTRIBUTES for svg_tag in ALLOWED_SVG_TAGS},
"*": ["class", "id"],
},
)
Expand Down
185 changes: 185 additions & 0 deletions nbconvert/filters/svg_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
# Via bleach/_vendor/html5lib/filters/sanitizer.py;
# we don't want to import it because it would raise a deprecation warning.

ALLOWED_SVG_TAGS = {
"a",
"animate",
"animateColor",
"animateMotion",
"animateTransform",
"circle",
"clipPath",
"defs",
"desc",
"ellipse",
"font-face",
"font-face-name",
"font-face-src",
"g",
"glyph",
"hkern",
"line",
"linearGradient",
"marker",
"metadata",
"missing-glyph",
"mpath",
"path",
"polygon",
"polyline",
"radialGradient",
"rect",
"set",
"stop",
"svg",
"switch",
"text",
"title",
"tspan",
"use",
}
ALLOWED_SVG_ATTRIBUTES = {
# SVG attributes
"accent-height",
"accumulate",
"additive",
"alphabetic",
"arabic-form",
"ascent",
"attributeName",
"attributeType",
"baseProfile",
"bbox",
"begin",
"by",
"calcMode",
"cap-height",
"class",
"clip-path",
"color",
"color-rendering",
"content",
"cx",
"cy",
"d",
"descent",
"display",
"dur",
"dx",
"dy",
"end",
"fill",
"fill-opacity",
"fill-rule",
"font-family",
"font-size",
"font-stretch",
"font-style",
"font-variant",
"font-weight",
"from",
"fx",
"fy",
"g1",
"g2",
"glyph-name",
"gradientUnits",
"hanging",
"height",
"horiz-adv-x",
"horiz-origin-x",
"id",
"ideographic",
"k",
"keyPoints",
"keySplines",
"keyTimes",
"lang",
"marker-end",
"marker-mid",
"marker-start",
"markerHeight",
"markerUnits",
"markerWidth",
"mathematical",
"max",
"min",
"name",
"offset",
"opacity",
"orient",
"origin",
"overline-position",
"overline-thickness",
"panose-1",
"path",
"pathLength",
"points",
"preserveAspectRatio",
"r",
"refX",
"refY",
"repeatCount",
"repeatDur",
"requiredExtensions",
"requiredFeatures",
"restart",
"rotate",
"rx",
"ry",
"slope",
"stemh",
"stemv",
"stop-color",
"stop-opacity",
"strikethrough-position",
"strikethrough-thickness",
"stroke",
"stroke-dasharray",
"stroke-dashoffset",
"stroke-linecap",
"stroke-linejoin",
"stroke-miterlimit",
"stroke-opacity",
"stroke-width",
"systemLanguage",
"target",
"text-anchor",
"to",
"transform",
"type",
"u1",
"u2",
"underline-position",
"underline-thickness",
"unicode",
"unicode-range",
"units-per-em",
"values",
"version",
"viewBox",
"visibility",
"width",
"widths",
"x",
"x-height",
"x1",
"x2",
"xlink:actuate",
"xlink:arcrole",
"xlink:href",
"xlink:href",
"xlink:role",
"xlink:show",
"xlink:show",
"xlink:title",
"xlink:type",
"xlink:type",
"xml:base",
"xml:lang",
"xml:space",
"y",
"y1",
"y2",
"zoomAndPan",
}
Loading

0 comments on commit 9809c53

Please sign in to comment.