code wiki / (root) / nx_html_escape.nx

nx_html_escape.nx

buildroot/runtime/nx_html_escape.nx

9533 B287 linesdepth 2pulls 2 transitivereach 49 importersview sourcekind tooltopic html
docsdependenciesstructsconstsfunctions

about

html_escape.nx -- HTML entity encoder + decoder. Escapes the five characters that are NEVER safe in HTML text content or attribute values: < &lt; > &gt; & &amp; " &quot; ' &#39; (apostrophe -- &apos; 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 "&gt;" = 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

nx_syscalls.nx nx_html_escape.nx html_escape.nx nx_atom_v1.nx nx_html_to_nishi_page.nx nx_markdown_block.nx nx_markdown_inline.nx nx_nishi_page_emit.nx nx_pipeline_banner.nx nx_pipeline_route.nx nx_rss.nx nx_search_render_html.nx

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

main sys_mmap html_escape he_is_safe he_put html_unescape he_hex_digit

structs

none

consts

42const HE_ERR_SHORT: i64 = -1

functions

45func he_is_safe(b: i64) -> i64
called by 1: html_escape
56func he_put(out: *u8, cap: i64, off: i64, src: *u8, n: i64) -> i64
called by 1: html_escape
67func html_escape(out: *u8, cap: i64, src: *u8, n: i64) -> i64
103func he_hex_digit(b: i64) -> i64
called by 1: html_unescape
118func html_unescape(out: *u8, cap: i64, src: *u8, n: i64) -> i64
called by 1: main calls 1: he_hex_digit
251func main() -> i64