striptags
Strip HTML tags from a string — with optional allowed tags and a replacement for
removed tags. A faithful Rust port of the
striptags npm package: the same
plaintext/html/comment state machine, including its handling of comments, quoted
attributes, and stray </>. Zero dependencies and #![no_std].
use ;
assert_eq!;
assert_eq!;
assert_eq!;
Why striptags?
Pulling readable text out of HTML — for search indexing, previews, plain-text
emails, or light sanitization — is a common need, and the canonical JS implementation
handles the tricky bits (comments, > inside quoted attributes, unterminated tags,
a < b). This ports it faithfully.
[]
= "0.1"
Note: like the original, this is a fast tag remover, not a security sanitizer. For untrusted HTML where XSS matters, use a dedicated sanitizer such as
ammonia.
API
| Item | Purpose |
|---|---|
strip_tags(html) |
Remove all tags |
strip_tags_with(html, allowed, replacement) |
Keep allowed tags; replace removed ones |
StripTags |
Streaming stripper that keeps state across feed calls |
Behavior
- Allowed tag names are matched against the lower-cased tag name, so pass them in
lower case (
&["p", "a"]). - HTML comments (
<!-- … -->) are removed entirely (no replacement emitted). - A
>inside a quoted attribute is ignored; a stray<(with a space) is kept as text; an unterminated tag at end of input produces no output.
Contributors ✨
This project follows the all-contributors specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the emoji key for how each contribution is recognized, and open a PR or issue to get involved.
Thanks goes to these wonderful people:
License
Licensed under either of Apache-2.0 or MIT at your option.