-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make css sanitization non-default, fix docs, fix tests (#633)
In order to use css sanitization, you have to install the css extras which installs tinycss2. Additionally, I reworked css sanitization to be encapsulated in a class making it easier for developers to provide their own if they want to. I changed the ALLOWED_CSS_PROPERTIES (previously called styles) to match what html5lib has. I updated the tests and documentation accordingly.
- Loading branch information
Showing
11 changed files
with
271 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import tinycss2 | ||
|
||
|
||
ALLOWED_CSS_PROPERTIES = frozenset( | ||
( | ||
"azimuth", | ||
"background-color", | ||
"border-bottom-color", | ||
"border-collapse", | ||
"border-color", | ||
"border-left-color", | ||
"border-right-color", | ||
"border-top-color", | ||
"clear", | ||
"color", | ||
"cursor", | ||
"direction", | ||
"display", | ||
"elevation", | ||
"float", | ||
"font", | ||
"font-family", | ||
"font-size", | ||
"font-style", | ||
"font-variant", | ||
"font-weight", | ||
"height", | ||
"letter-spacing", | ||
"line-height", | ||
"overflow", | ||
"pause", | ||
"pause-after", | ||
"pause-before", | ||
"pitch", | ||
"pitch-range", | ||
"richness", | ||
"speak", | ||
"speak-header", | ||
"speak-numeral", | ||
"speak-punctuation", | ||
"speech-rate", | ||
"stress", | ||
"text-align", | ||
"text-decoration", | ||
"text-indent", | ||
"unicode-bidi", | ||
"vertical-align", | ||
"voice-family", | ||
"volume", | ||
"white-space", | ||
"width", | ||
) | ||
) | ||
|
||
|
||
ALLOWED_SVG_PROPERTIES = frozenset( | ||
( | ||
"fill", | ||
"fill-opacity", | ||
"fill-rule", | ||
"stroke", | ||
"stroke-width", | ||
"stroke-linecap", | ||
"stroke-linejoin", | ||
"stroke-opacity", | ||
) | ||
) | ||
|
||
|
||
class CSSSanitizer: | ||
def __init__( | ||
self, | ||
allowed_css_properties=ALLOWED_CSS_PROPERTIES, | ||
allowed_svg_properties=ALLOWED_SVG_PROPERTIES, | ||
): | ||
self.allowed_css_properties = allowed_css_properties | ||
self.allowed_svg_properties = allowed_svg_properties | ||
|
||
def sanitize_css(self, style): | ||
"""Sanitizes css in style tags""" | ||
parsed = tinycss2.parse_declaration_list(style) | ||
|
||
if not parsed: | ||
return "" | ||
|
||
new_tokens = [] | ||
for token in parsed: | ||
if token.type == "at-rule": | ||
print("omg") | ||
elif token.type == "declaration": | ||
if ( | ||
token.lower_name in self.allowed_css_properties | ||
or token.lower_name in self.allowed_svg_properties | ||
): | ||
new_tokens.append(token) | ||
elif token.type in ("comment", "whitespace"): | ||
if new_tokens and new_tokens[-1].type != token.type: | ||
new_tokens.append(token) | ||
# Declaration | ||
# AtRule | ||
# Comment | ||
# WhitespaceToken | ||
# ParseError | ||
|
||
if not new_tokens: | ||
return "" | ||
|
||
return tinycss2.serialize(new_tokens).strip() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.