CX · Reference guidehtml
PlaygroundDownloadsAboutv0.18.0-pre.1

html

HTML as a first-class document format: parse, sanitize, serialize, and extract text. Lenient WHATWG HTML5 parsing turns real-world tag soup into an ordinary CXDM element tree, queryable with CXPath and transformable with [?modify] — there are no HTML-specific selectors. The sanitizer strips dangerous markup from attacker-controlled HTML via a conservative built-in policy or a customizable allowlist, the primary use being XSS-safe rendering of inbound email.

html:parse

[$html:parse] -> element — Leniently parse an HTML document into a CXDM element tree, recovering from malformed markup.

      [?lib 'cx-stdlib/html']
[?let [= $doc [$html:parse "<a href=\"https://e.com\">x</a>"]] $doc/a@href]
    
      'https://e.com'
    

html:parse-fragment

[$html:parse-fragment] -> [sequence element] — Parse an HTML fragment into its top-level nodes, with no implied document wrapper.

      [?lib 'cx-stdlib/html']
[$count [$html:parse-fragment "<li>a</li><li>b</li>"]]
    
      2
    

html:sanitize

[$html:sanitize] -> string — Strip dangerous markup from HTML using the built-in safe-default policy, returning clean HTML.

      [?lib 'cx-stdlib/html']
[$html:sanitize "<script>alert(1)</script><p>hi</p>"]
    
      '<p>hi</p>'
    

html:sanitize-with-policy

[$html:sanitize-with-policy] -> string — Sanitize HTML against a custom [html-policy …] allowlist layered over the safe default.

      [?lib 'cx-stdlib/html']
[$html:sanitize-with-policy "<script>x</script><p>hi</p>" [html-policy]]
    
      '<p>hi</p>'
    

html:sanitize-tree

[$html:sanitize-tree] -> element — Sanitize an already-parsed element tree with the safe-default policy, returning a tree.

      [?lib 'cx-stdlib/html']
[$html:serialize [$html:sanitize-tree [$html:parse "<script>x</script><p>hi</p>"]]]
    
      '<p>hi</p>'
    

html:sanitize-tree-with-policy

[$html:sanitize-tree-with-policy] -> element — Sanitize an already-parsed element tree against a custom [html-policy …].

      [?lib 'cx-stdlib/html']
[$html:serialize [$html:sanitize-tree-with-policy [$html:parse "<p><img src=\"x\">hi</p>"] [html-policy [deny-tags img]]]]
    
      '<p>hi</p>'
    

html:serialize

[$html:serialize] -> string — Serialize a CXDM element tree back to HTML5.

      [?lib 'cx-stdlib/html']
[$html:serialize [$html:sanitize-tree [$html:parse "<script>x</script><p>hi</p>"]]]
    
      '<p>hi</p>'
    

html:serialize-xhtml

[$html:serialize-xhtml] -> string — Serialize a CXDM element tree to XML-well-formed XHTML (void elements self-close).

      [?lib 'cx-stdlib/html']
[$html:extract-text [$html:parse [$html:serialize-xhtml [$html:parse "<p>hi</p>"]]]]
    
      hi
    

html:emit

[$html:emit] -> string — The codec contract's canonical writer name (core/codec.md §3) — an alias of serialize, so [$<codec>:emit] reaches every format including this one.

      [?lib 'cx-stdlib/html']
[$html:emit [$html:parse "<p>hi</p>"]]
    
      '<p>hi</p>'
    

html:emit-bytes

[$html:emit-bytes] -> bytes — The codec contract's bytes writer — emit, encoded UTF-8.

      [?lib 'cx-stdlib/html']
[?lib 'cx-stdlib/bytes']
[$bytes:length [$html:emit-bytes [$html:parse "<p>hi</p>"]]]
    
      9
    

html:parse-bytes

[$html:parse-bytes] -> element — The codec contract's bytes reader — UTF-8 decoded at the one text-codec encoding boundary (invalid UTF-8 refuses, a leading BOM is dropped), then parse.

      [?lib 'cx-stdlib/html']
[?lib 'cx-stdlib/bytes']
[$html:extract-text [$html:parse-bytes [$bytes:from-hex "3c703e68693c2f703e"]]]
    
      hi
    

html:extract-text

[$html:extract-text] -> string — Strip all tags and decode entities to plain text, skipping script and style content.

      [?lib 'cx-stdlib/html']
[$html:extract-text [$html:parse "<p>a<p>b<li>c"]]
    
      'a b c'