Skip to main content
Sanitizes and cleans HTML content by removing unwanted elements, attributes, and whitespace. Provides fine-grained control over each cleaning operation through configurable options.

Examples

Arguments

str
required
The HTML content to sanitize
bool
Remove all <script> elements. Defaults to True.
bool
Remove all <style> elements. Defaults to True.
bool
Remove all <svg> elements. Defaults to True.
bool
Remove HTML comments. Defaults to True.
bool
Remove attributes longer than max_attribute_length. Defaults to True.
int
Maximum length for attributes before removal. Defaults to 500.
list[str]
List of attribute names to always preserve. Defaults to [“class”, “src”].
bool
Remove empty tags (except preserved ones). Defaults to True.
list[str]
List of tag names to preserve even when empty. Defaults to [“img”].
bool
Remove extra whitespace between tags and empty lines. Defaults to True.

Returns: str

The sanitized HTML string