htmlx

package
v0.60.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package htmlx is what happens to a rendered template's HTML before anybody sees it: Sanitize (no script can come out of a template), InlineCSS (a mail's stylesheet written into its elements, the way mail programs read it) and Text (a mail's plain-text version, made from its HTML).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InlineCSS

func InlineCSS(src string) (string, error)

InlineCSS writes a mail's stylesheet into its elements' style attributes, the way mail programs read a message: several of them (Gmail's apps, Outlook) drop <style> or most of it.

What it does, and what it leaves:

  • every rule of every <style> in the document whose selector cascadia can match (type, class, id, attribute, descendant/child, :first-child…) is applied to the elements it matches, by specificity and then source order, as a browser would; !important wins over everything but an !important of the element's own style attribute; the element's own style attribute wins over the sheet;
  • rules it cannot apply to an element - @media, @font-face, @supports, and selectors with a state (:hover, :focus, ::before) - stay in a <style> in the head, for the programs that read one (dark mode, a narrow screen);
  • a <style data-inline="keep"> is left exactly as written.

The input is a template filex rendered and Sanitize already read; an unreadable sheet is not an error, its rules are kept for the programs that read <style>.

func Sanitize

func Sanitize(src string) string

Sanitize is the HTML a template rendered, with everything a template is not allowed to say taken out:

  • the elements of `dropped`, with their content;
  • every event handler attribute (on…), srcdoc and formaction;
  • an address that is not http(s), mailto:, tel:, relative, or (for an image) a data:image/…: javascript:, vbscript:, data:text/html…;
  • a <meta http-equiv> (a refresh to elsewhere, a Content-Security-Policy of the template's own);
  • a <form> or <link> pointing to another site (form-action and the page's stylesheets stay filex's).

⚠ It is the second wall, not the first: public pages also carry a Content-Security-Policy whose script-src only filex's own nonce'd scripts satisfy (docs/TEMPLATES.md "Security"), and a mail program runs no script. It is here so that a mail - which has no CSP - and an old browser get no script either, and so the editor's preview shows what was taken out.

The template's own text and markup are kept byte for byte where they are allowed (z.Raw): a <style> block's selectors are not re-escaped.

func SanitizeReport

func SanitizeReport(src string) (string, []string)

SanitizeReport is Sanitize, and what it took out ("<script>", "onclick", "href=javascript:…"), once each - what the editor tells the person who wrote the template.

func Text

func Text(src string) string

Text is the plain-text version of a mail's HTML: the part a mail program shows when it shows no HTML, and what a screen reader of a text-only program reads. Paragraphs and headings are separated by a blank line, a list item starts with "- ", a table row is one line, a link whose text is not its address is followed by the address, an image by its alt text. What a reader never sees - <head>, <style>, an element hidden with display:none (a preheader) - is left out.

Two attributes let a template say what its HTML means in plain text: data-text="skip" leaves an element out (the address under a button, written for a program that shows no buttons, would be said twice), and on a link data-text="href" writes the address alone (a button's label means nothing without it), data-text="label" its label alone (a name that is a link only for those who can click it).

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL