Expose ammonia's url_relative policy via url_relative kwarg#131
Merged
Conversation
Adds a `url_relative` keyword argument to `clean()` and `Cleaner()` that maps
to ammonia's `UrlRelative` enum, so relative URLs in href/src/<object data> can
be passed through (default), denied, rewritten against a base or root URL, or
rewritten by a custom callback.
Accepted values:
- None (default) / "pass_through" -> PassThrough
- "deny" -> Deny
- ("rewrite_with_base", base_url) -> RewriteWithBase
- ("rewrite_with_root", root_url, path) -> RewriteWithRoot
- callable (url) -> str | None -> Custom
Input is validated eagerly at construction (ValueError for bad mode/URL/tuple,
TypeError for unsupported types). A custom callback that raises or returns a
non-str/non-None value strips the URL and reports the error via
sys.unraisablehook, keeping clean() infallible.
Closes messense#129
messense
approved these changes
Jun 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #129.
Adds a
url_relativekeyword argument toclean()andCleaner()mapping toammonia's
UrlRelativeenum, so relative URLs inhref/src/<object data=...>can be passed through (default), denied, rewritten against abase or root URL, or rewritten by a custom callback.
API
None(default) /"pass_through"PassThrough"deny"Deny("rewrite_with_base", base_url)RewriteWithBase("rewrite_with_root", root_url, path)RewriteWithRootNotes
None⇒ ammonia'sPassThrough); no behaviour change for existing callers.ValueErrorfor an unknown modestring, malformed tuple, or unparseable URL;
TypeErrorfor an unsupported type.str/non-Nonevalue) stripsthe URL and reports the error via
sys.unraisablehook, keepingclean()infallible — mirroring
attribute_filter's error handling.Tests
Adds 9 tests (deny / rewrite_with_base / rewrite_with_root / custom replace /
custom strip / custom raising / explicit pass_through / invalid inputs /
reusable
Cleaner), plus doctest examples onclean.nh3.pyistubs updated.