Classes
Policy
3
▼
Wrapper for Document -> Document transform, supports composition via |.
Policy
3
▼
Wrapper for Document -> Document transform, supports composition via |.
Methods
Internal Methods 3 ▼
__init__
1
▼
__init__
1
▼
def __init__(self, fn: Callable[[Document], Document]) -> None
Parameters
| Name | Type | Description |
|---|---|---|
fn |
— |
def __call__(self, doc: Document) -> Document
Parameters
| Name | Type | Description |
|---|---|---|
doc |
— |
Returns
Document
Functions
_url_scheme
1
str | None
▼
Extract a URL's scheme, robust to obfuscation; None if scheme-less.
Browsers i…
_url_scheme
1
str | None
▼
def _url_scheme(url: str) -> str | None
Extract a URL's scheme, robust to obfuscation; None if scheme-less.
Browsers ignore HTML entities and embedded control/whitespace characters
when resolving a URL scheme, sojavascript:can hide as
javascript: or java	script:. We decode entities and strip
ASCII control/whitespace before reading the scheme so the dangerous-scheme
check cannot be bypassed.
Returns the lowercased scheme (without the trailing colon), or None for
scheme-less URLs (relative paths, fragments, protocol-relative//),
which are always safe.
Parameters
| Name | Type | Description |
|---|---|---|
url |
str |
Returns
str | None
_is_dangerous_url
1
bool
▼
Check if a URL uses a known-dangerous scheme (after de-obfuscation).
_is_dangerous_url
1
bool
▼
def _is_dangerous_url(url: str) -> bool
Parameters
| Name | Type | Description |
|---|---|---|
url |
str |
Returns
bool
_scheme_allowed
2
bool
▼
Check if a URL's scheme is allowed.
Scheme-less/relative URLs (``/path``, ``#f…
_scheme_allowed
2
bool
▼
def _scheme_allowed(url: str, allowed: frozenset[str]) -> bool
Check if a URL's scheme is allowed.
Scheme-less/relative URLs (/path, #frag, //host) are always
allowed; only an explicit, disallowed scheme is rejected.
Parameters
| Name | Type | Description |
|---|---|---|
url |
str |
|
allowed |
frozenset[str] |
Returns
bool
Remove all HtmlBlock and HtmlInline nodes.
Remove HtmlInline nodes where .html starts with <!--.
Remove Link and Image nodes with javascript:, data:, vbscript: URLs.
Strip zero-width characters and bidi overrides from Text nodes.
Replace Image nodes with Text nodes containing alt text.
Remove FencedCode and IndentedCode blocks.
Keep only Link/Image nodes with allowed URL schemes.
Default schemes: https, h…
def allow_url_schemes(*schemes: str) -> Policy
Keep only Link/Image nodes with allowed URL schemes.
Default schemes: https, http, mailto.
Parameters
| Name | Type | Description |
|---|---|---|
*schemes |
str |
Returns
Policy
Prune block content nested deeper than ``max_depth`` container levels.
Depth c…
def limit_depth(max_depth: int = 10) -> Policy
Prune block content nested deeper thanmax_depthcontainer levels.
Depth counts block containers (block quotes, lists, list items, directives). Containers at the limit have their children dropped, protecting downstream consumers from adversarially deep AST nesting.
Only block-level structure is traversed. Inline children (e.g. Emphasis,
Strong, Link text) do not contribute to block nesting depth and are
left untouched, so limit_depth cannot itself recurse into — and crash on —
deeply nested inline content.
Parameters
| Name | Type | Description |
|---|---|---|
max_depth |
int |
Default:10
|
Returns
Policy