Strip control characters and cap length. Safe for embedding in JSON data (inside tags) and plain text. For direct HTML injection, wrap the result with html.escape().
(text: str | None)
| 391 | |
| 392 | |
| 393 | def sanitize_label(text: str | None) -> str: |
| 394 | """Strip control characters and cap length. |
| 395 | |
| 396 | Safe for embedding in JSON data (inside <script> tags) and plain text. |
| 397 | For direct HTML injection, wrap the result with html.escape(). |
| 398 | """ |
| 399 | if text is None: |
| 400 | return "" |
| 401 | text = _CONTROL_CHAR_RE.sub("", str(text)) |
| 402 | if len(text) > _MAX_LABEL_LEN: |
| 403 | text = text[:_MAX_LABEL_LEN] |
| 404 | return text |
| 405 | |
| 406 | |
| 407 | # --------------------------------------------------------------------------- |
no outgoing calls