cheatah
Module

parsers::html

Functions

fn std::string escape(std::string_view s, bool quote=true) source#

Escape the markup-significant characters in s (Python html.escape).

Replaces &&amp;, <&lt;, >&gt; always, and when quote is true (the default) also "&quot; and '&#x27;, so the result is safe to drop into element text and into a quoted attribute value.

Parameters
s

the text to escape.

quote

also escape the quote characters (default true).

Returns

a newly allocated escaped copy.

Complexity

O(n) time in the length of s.

Allocation

one result string on the heap.

Performance77.39 ns/call in cheatah · 355 ns/call in CPython 3.12.3 · ≈4.6× faster
fn std::string unescape(std::string_view s) source#

Resolve character references back to text (Python html.unescape).

Decodes decimal (&#169;) and hexadecimal (&#xA9;) numeric references and a table of the common named entities (&amp;, &lt;, &gt;, &quot;, &apos;, &nbsp;, &copy;, …) to UTF-8. Unknown or malformed references are left verbatim. Inverse of escape for the characters it covers.

Parameters
s

text that may contain character references.

Returns

a newly allocated decoded copy.

Complexity

O(n) time in the length of s.

Allocation

one result string on the heap.

Performance99.76 ns/call in cheatah · 1506 ns/call in CPython 3.12.3 · ≈14.9× faster
fn std::vector< Token > parse(std::string_view html) source#

Tokenize an HTML document into the events a callback parser would dispatch.

Returns one Token per event, in document order. The Token::kind is one of:

  • "starttag"<div class="x">; Token::tag + Token::attrs set.

  • "startendtag" — self-closing <br/>; tag + attrs set.

  • "endtag"</div>; Token::tag set.

  • "data" — text between tags; Token::data set (character references decoded, like Python's convert_charrefs=True).

  • "comment"<!-- … -->; Token::data is the comment body.

  • "decl"<!DOCTYPE html>; Token::data is the text after <!.

  • "pi" — processing instruction <? … >; Token::data the body.

Tag and attribute names are lowercased. <script> / <style> bodies are emitted as a single raw "data" token (references not decoded). Malformed input never throws: unterminated constructs consume to end-of-input.

Parameters
html

the document text.

Returns

the token list (empty for empty input).

Complexity

O(n) time in the length of html.

Allocation

the token vector and its strings on the heap.

Performancereturns a parse structure — not reduced to one scalar here
fn std::string get_attr(const Token &t, std::string_view name) source#

Value of attribute name on t, or "" if absent (or valueless).

Parameters
t

a start/startend token.

name

attribute name (matched case-insensitively).

Returns

the attribute value, or "".

Complexity

O(k) in the attribute count of t.

Allocation

one result string on the heap.

Performancereturns a parse structure — not reduced to one scalar here
fn bool has_attr(const Token &t, std::string_view name) source#

Whether t carries an attribute named name (case-insensitive).

Parameters
t

a start/startend token.

name

attribute name.

Returns

true if present (even if valueless).

Complexity

O(k) in the attribute count of t.

Allocation

none.

Performancereturns a parse structure — not reduced to one scalar here