"
+ else:
+ self.output += f"<{tag}>"
+
+ def handle_endtag(self, tag: str) -> None:
+ """Append the end tag only if it is in the allowed list."""
+ if tag in ALLOWED_TAGS:
+ self.output += f"{tag}>"
+
+ def handle_startendtag(self, tag: str, _attrs: dict[str, str]) -> None:
+ """Append a self-closing tag if it is a new line."""
+ if tag == "br":
+ self.output += "
"
+
+ def handle_data(self, data: str) -> None:
+ """Append any data inside tags after escaping it."""
+ self.output += escape(data)
+
+ def handle_entityref(self, name: str) -> None:
+ """Append any named character."""
+ self.output += f"&{name};"
+
+ def handle_charref(self, name: str) -> None:
+ """Append any character references."""
+ self.output += f"{name};"
+
+
+def sanitize_html(html: str) -> str:
+ """Santitise HTML to only have a small list of allowed tags.
+
+ Tags allowed without attrs: ``, , ,