Skip to content

Expose ammonia's id_prefix option via id_prefix kwarg#134

Merged
messense merged 1 commit into
messense:mainfrom
gghez:expose-id-prefix
Jun 26, 2026
Merged

Expose ammonia's id_prefix option via id_prefix kwarg#134
messense merged 1 commit into
messense:mainfrom
gghez:expose-id-prefix

Conversation

@gghez

@gghez gghez commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Adds an id_prefix keyword argument to clean() and Cleaner() that maps to
ammonia's Builder::id_prefix,
prepending the given string to every allowed id attribute value. This namespaces
user-generated ids so they cannot collide with the ids already present on the
host page.

nh3.clean(
    '<b id="x">hi</b>',
    attributes={"b": {"id"}},
    id_prefix="user-content-",
)
# -> '<b id="user-content-x">hi</b>'

Semantics mirror ammonia:

  • Defaults to None (no prefixing — current behaviour).
  • The tag and the id attribute must still be whitelisted via attributes.
  • Values that already start with the prefix are left unchanged (no double prefix).

Includes tests for clean() and a reusable Cleaner, a doctest example, updated
type stubs, and docstrings (which feed the autodoc-generated docs).

Closes #133

Adds an `id_prefix` keyword argument to `clean()` and `Cleaner()` that maps
to ammonia's `Builder::id_prefix`, prepending the given string to every
allowed `id` attribute value. This namespaces user-generated `id`s so they
cannot collide with the `id`s already present on the host page.

Semantics mirror ammonia:
- Defaults to None (no prefixing).
- The tag and the `id` attribute must still be whitelisted via `attributes`.
- Values already starting with the prefix are left unchanged.

Closes messense#133
@messense
messense merged commit 0573fe4 into messense:main Jun 26, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expose ammonia's id_prefix option

2 participants