Skip to content

Trusted HTML and rich text

The one Trusted Types policy Capsomer's scripts use, and RichText for stored sanitized HTML.

Anatomy

<div class="cap-rich-text" data-measure="text"><p>Stored, sanitized HTML.</p></div>
<span class="cap-rich-text">Field notes on <em>Quercus alba</em></span>
ClassIs
.cap-rich-textthe root

Data attributes

AttributeValues
data-measuretext

Keyboard

KeyDoes
TabMoves through links in the stored HTML, as any link

API

RichText

PropTypeRequiredDoes
htmlstring | null | undefinedYesHTML the server already sanitized for this origin (a stored abstract, a bio, a statement). Never a visitor's raw input.
asRichTextTagThe element it is rendered into. A div by default; a span for a title inside a line.
measurebooleanKeeps running text to the reading measure (--measure), in place of an inline max-width.
fallbackReactNodeShown in place of the element when html is empty: "Untitled", "No description yet".

And every attribute of <element>.

Functions

FunctionSignatureDoes
trustedHtml(html: string): stringhtml as TrustedHTML under the capsomer policy where the browser has Trusted Types, else the string.
setStaticHtml(el: Element, html: string): voidSets el's children from a constant markup string in our own source (an icon).
copyChildren(dst: Element, src: Element): voidReplaces dst's children with copies of src's: DOM to DOM, no markup string involved.
markup(html: string): { __html: string; }For React's dangerouslySetInnerHTML: { __html } holding html as TrustedHTML. The same object comes back for the same string, because React sets the markup again (and drops what a script attached to it) whenever the object changes.
inertDocument(html: string): DocumentParses html into an inert document: DOMParser's documents run no script and load no image or stylesheet. For HTML a person pasted into their own editor, which a schema then reads node by node; never move its nodes into the page as they are.