nx_html_escape.nx
buildroot/runtime/nx_html_escape.nx
about
html_escape.nx -- HTML entity encoder + decoder.
Escapes the five characters that are NEVER safe in HTML text
content or attribute values:
< <
> >
& &
" "
' ' (apostrophe -- ' is XML only, not HTML4)
This is the XSS-prevention primitive. Any user-supplied text
rendered into an HTML template MUST go through this first.
Nishi-pages, every sovereign web handler, and any template
engine built on top all rely on correct escaping.
Decoder handles the five named entities + numeric character
references (&#NN; and &#xHH;) bounded to the ASCII range.
Full Unicode entity decoding is out of scope (would need the
HTML5 named-character-reference table of ~2000 entries);
callers with that need should pre-process server-side into
one of the five safe forms.
Invariants:
HE1 Encoding output is never larger than 6x input (worst
case: every byte is '>' which becomes ">" = 4 bytes).
Caller sizes the output buffer at src_len * 6 + 1.
HE2 Encode is idempotent on already-safe bytes (ASCII alnum,
space, punctuation except the five).
HE3 Decode rejects malformed entities by leaving them
literal (per WHATWG tolerant parsing); unknown named
entities also pass through literal.
HE4 Round trip: decode(encode(x)) == x for any ASCII x.
dependencies 1 imports · 13 importers
diagram shows first 10 each side; +0 more imports, +3 more importers in the complete lists below.
imports: nx_syscalls.nx
imported by: html_escape.nxnx_atom_v1.nxnx_html_to_nishi_page.nxnx_markdown_block.nxnx_markdown_inline.nxnx_nishi_page_emit.nxnx_pipeline_banner.nxnx_pipeline_route.nxnx_rss.nxnx_search_render_html.nxnx_search_snippet_extract.nxnx_sitemap.nxnx_wiki_cite_render.nx
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| none |
consts
| 42 | const HE_ERR_SHORT: i64 = -1 |
functions
| 45 | func he_is_safe(b: i64) -> i64 called by 1: html_escape |
| 56 | func he_put(out: *u8, cap: i64, off: i64, src: *u8, n: i64) -> i64 called by 1: html_escape |
| 67 | func html_escape(out: *u8, cap: i64, src: *u8, n: i64) -> i64 called by 17: atom_put_escapedatom_put_escapedmainnx_hnp_put_escapedmb_put_escmd_put_esc+11 calls 2: he_is_safehe_put |
| 103 | func he_hex_digit(b: i64) -> i64 called by 1: html_unescape |
| 118 | func html_unescape(out: *u8, cap: i64, src: *u8, n: i64) -> i64 |
| 251 | func main() -> i64 |