Spaces:
Runtime error
Runtime error
Download core/escaping.py from Jellyfish042/Compression-Lens: direct link, hf CLI and curl.
- Browser
- Download file 1.19 kB
-
https://huggingface.co/spaces/Jellyfish042/Compression-Lens/resolve/main/core/escaping.py
- Command line
-
hf download hf://spaces/Jellyfish042/Compression-Lens/core/escaping.py
-
curl -L -o escaping.py https://huggingface.co/spaces/Jellyfish042/Compression-Lens/resolve/main/core/escaping.py
1.19 kB
| """ | |
| Unified escaping helpers for HTML rendering. | |
| Keep all HTML/attribute/script escaping logic in one place to avoid divergence. | |
| """ | |
| import json | |
| from typing import Any | |
| def escape_text_node(text: str) -> str: | |
| """Escape text for HTML text node insertion.""" | |
| if text is None: | |
| return "" | |
| return ( | |
| text.replace("&", "&") | |
| .replace("<", "<") | |
| .replace(">", ">") | |
| ) | |
| def escape_attr(text: str) -> str: | |
| """Escape text for safe placement in HTML attribute values.""" | |
| if text is None: | |
| return "" | |
| return ( | |
| text.replace("&", "&") | |
| .replace('"', """) | |
| .replace("'", "'") | |
| .replace("<", "<") | |
| .replace(">", ">") | |
| .replace("\n", " ") | |
| .replace("\r", " ") | |
| .replace("\t", "	") | |
| ) | |
| def escape_json_for_script(value: Any) -> str: | |
| """Serialize JSON for safe embedding inside <script> tags.""" | |
| text = json.dumps(value, ensure_ascii=False) | |
| # Prevent closing tags or HTML entities from breaking script context. | |
| return ( | |
| text.replace("<", "\\u003c") | |
| .replace(">", "\\u003e") | |
| .replace("&", "\\u0026") | |
| ) | |