Skip to main content

Module rewrite

Module rewrite 

Available on crate features html and http and std only.
Expand description

Selector-driven HTML rewriting.

The public entry point is HtmlRewriter (and the rewrite_str one-shot). Internally, a streaming selector matcher is fed start/end tags as they are tokenized and reports, at each start tag, which registered selectors match the element being opened — without building a DOM. It maintains an open-element stack and, per selector, two bitmasks:

  • completed — which compound-prefixes are matched ending at this element (a child combinator reads its parent’s completed);
  • desc_ready — which prefixes are available to descendants (a descendant combinator reads its ancestor’s desc_ready).

Ancestry comes from a model of the browser’s stack of open elements (see ScopeMode), which also decides how the tokenizer reads what follows a start tag (raw text, CDATA), so the rewriter’s view of where an element ends cannot drift from the browser’s. Attribute values are compared the way browsers see them: character references decoded, class and id case folded in quirks mode, and the values HTML treats case-insensitively (type, rel, …) folded too.

It underpins the selector-driven HtmlRewriter.

Structs§

AttributeName
A validated HTML attribute name.
Element
A matched element, for inspection and mutation by a handler.
ElementContentHandlers
A builder bundling (selector, closure) pairs — the closure-based escape hatch over the ElementContentHandler trait, for one-off rewrites that don’t need a dedicated state struct.
HtmlRewriter
A streaming, selector-driven HTML rewriter.

Enums§

InvalidAttributeName
Why a string is not a valid AttributeName.
ScopeMode
How the rewriter decides where elements end in markup that leaves end tags out, misnests them, or adds stray ones.

Traits§

ElementContentHandler
Handles matched elements during a rewrite.

Functions§

rewrite_str
One-shot rewrite of a complete HTML string.

Type Aliases§

HandlerResult
The result of an element content handler. An error aborts the rewrite.