Grav v1.14.0

html

import "std/text" (included in import "std") · namespace std.text

HTML text made safe: Html.escape for values you put into a page, Html.unescape for entities, Html.stripTags for the plain text of a fragment, and HtmlSanitizer for HTML that came from users (comments, rich-text editors, imported e-mail): only allowed tags and attributes survive, and links keep only safe schemes.

grav
import "std"

fn main() {
    print(Html.escape("<b>Tom & \"Jerry\"</b>"))
    print(Html.unescape("caf&eacute; &#x263A; &lt;ok&gt;"))
    print(Html.stripTags("<p>Hello <b>there</b></p><script>alert(1)</script><p>A &amp; B</p>"))
    print(Html.sanitize("<p onclick=\"steal()\">Hi <a href=\"javascript:alert(1)\">x</a> <a href=\"https://grav.dev\">docs</a><script>bad()</script></p>"))
    let tables = new HtmlSanitizer().allowTags("table tr td").allowAttrs("td", "colspan").nofollow()
    print(tables.sanitize("<table><tr><td colspan=2 style=x>cell</td></tr></table><a href=\"/x\">y</a>"))
}
Output
&lt;b&gt;Tom &amp; &quot;Jerry&quot;&lt;/b&gt;
café ☺ <ok>
Hello there
A & B
<p>Hi <a>x</a> <a href="https://grav.dev">docs</a></p>
<table><tr><td colspan="2">cell</td></tr></table><a href="/x" rel="nofollow noopener noreferrer">y</a>
  • escape covers & < > " ', so the result is safe as element text and inside either kind of quoted attribute. Text with nothing to escape is returned as it is, without a copy.
  • unescape decodes numeric entities and the named ones people write (&amp;, &nbsp;, &eacute;, &euro;, &mdash; and about ninety more); others are left as they are.
  • The sanitizer drops <script>, <style>, <iframe>, <svg> and similar elements together with their contents, removes every on… handler, decodes entities in href and src before checking the scheme (so jav&#x61;script: is caught), closes elements left open, drops stray closing tags, and writes every attribute quoted and escaped. By default it keeps text formatting, paragraphs, headings, lists, quotes, pre/code, links (href, title) and images (src, alt, title, width, height), with http, https, mailto and relative links.
  • Your own allow-list: allowTags("table tr td"), removeTags("img"), allowAttrs("td", "colspan") ("*" for every tag), allowSchemes("tel"), and nofollow() for rel="nofollow noopener noreferrer" on links. HtmlSanitizer.none() removes every tag and keeps the text.
  • Everything runs as one pass in C over the input, with no regular expressions and no tree.

API

class Html

HTML escaping, entities and tags.

Static members

  • static fn escape(s: string) -> string — s with & < > " ' escaped: safe as element text and inside a quoted attribute. Returns s itself (no copy) when there is nothing to escape.
  • static fn escapeAttr(s: string) -> string — escape, for an attribute value (the same set: both quote styles are covered).
  • static fn unescape(s: string) -> string — Entities decoded: named ones (&amp; &eacute; &nbsp; and the other common ones) and numeric ones (&#233; &#xE9;). Unknown entities are left as they are.
  • static fn stripTags(html: string) -> string — The text of an HTML fragment: tags removed, <script> and <style> dropped with their contents, entities decoded, a line break after block elements (p, div, li, br, ...).
  • static fn sanitize(html: string) -> string — html cleaned with the default HtmlSanitizer (formatting, lists, links, images).

class HtmlSanitizer

Keeps only allowed tags, attributes and link schemes of untrusted HTML.

By default: text formatting (b, strong, i, em, u, s, sub, sup, mark, small, code, kbd, br), blocks (p, div, span, blockquote, pre, h1 to h6, hr), lists (ul, ol, li, dl, dt, dd), links (a href title) and images (img src alt title width height), with http, https and mailto links (and relative ones). class and id are not allowed until asked for.

Constructors

  • new HtmlSanitizer()

Static members

  • static fn shared() -> HtmlSanitizer — The default sanitizer, made once.
  • static fn none() -> HtmlSanitizer — A sanitizer that allows nothing: every tag is removed and the text kept (escaped).

Methods

  • fn allowTags(names: string) -> HtmlSanitizer — Also allows these tags (space-separated: "table tr td").
  • fn removeTags(names: string) -> HtmlSanitizer — Stops allowing these tags (space-separated).
  • fn allowAttrs(tag: string, names: string) -> HtmlSanitizer — Allows these attributes (space-separated) on tag; "*" for every allowed tag. Event handlers (on...) are never allowed.
  • fn allowSchemes(names: string) -> HtmlSanitizer — Also allows these URL schemes (space-separated: "tel ftp") in href and src.
  • fn nofollow(on: bool = true) -> HtmlSanitizer — rel="nofollow noopener noreferrer" on every link (instead of the link's own rel).
  • fn sanitize(html: string) -> string — The cleaned HTML.

Edit this page on GitHub